Search code examples
google-bigquerydbt

DBT select Big Query table from different Google Project


I am using DBT to read and write tables in Big Query, all running in my Google project X.
I have one table which I want to read in from a different Google project Y and put in a DBT model (which will then be saved as a table in project X).
Is it possible to do? And if yes, where do I define the different project in FROM {{ source('dataset_project_y', 'table_to_read')}}?


Solution

  • first, you need to declare the source in a source.yml file.

    https://docs.getdbt.com/docs/building-a-dbt-project/using-sources#declaring-a-source

    for example, create a source_y.yml

    sources:
      - name: dataset_project_y
        schema: dataset_y
        database: 'project_y'
        tables:
    
          - name: table_to_read
            identifier: table_to_read
    
    

    after that, you could refer to source table_to_read in any dbt model, select from it in any of the dbt models' SQL satements.

    https://docs.getdbt.com/docs/building-a-dbt-project/using-sources#selecting-from-a-source

    For example, to use table_to_read in dbt_model_x.sql

    {{
      config(
        materialized = "view",
      )
    }}
    
    SELECT * FROM {{ source('dataset_project_y', 'table_to_read')}}