Query and join Databricks Unity Catalog tables

Connecting Databricks Unity Catalog to the Lakehouse runtime catalog lets you query remote Apache Iceberg tables without copying or migrating them.

In this tutorial, you use BigQuery SQL in a retail scenario to join an Iceberg table named dim_products in Databricks Unity Catalog with a local fact_sales table. You then calculate product gross margins across clouds. The steps assume familiarity with Databricks Unity Catalog, BigQuery, and SQL.

Objectives

  • Create a sample Iceberg table in Databricks Unity Catalog and grant access to a Databricks service principal.
  • Create a federated catalog in the Lakehouse runtime catalog by using OpenID Connect (OIDC) token federation.
  • Query the federated Databricks Unity Catalog table from BigQuery and join it with a local BigQuery table in a SQL view.

Costs

In this document, you use the following billable components of Google Cloud:

To generate a cost estimate based on your projected usage, use the pricing calculator.

New Google Cloud users might be eligible for a free trial.

When you finish the tasks that are described in this document, you can avoid continued billing by deleting the resources that you created. For more information, see Clean up.

This tutorial also uses compute, storage, and data transfer (egress) in your Databricks workspace and remote object storage. Databricks and your cloud storage provider bill you for these resources.

Before you begin

  1. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  2. Verify that billing is enabled for your Google Cloud project.

  3. Your Databricks environment must meet the following requirements:

    • A Databricks workspace with Databricks Unity Catalog enabled and an external location on Amazon Web Services (AWS), Microsoft Azure, or Google Cloud. Catalogs that use default workspace storage aren't supported.
    • Serverless compute enabled in your Databricks workspace to create managed Iceberg tables. For requirements, see the Databricks documentation for AWS, Azure, or Google Cloud.
    • A Databricks SQL warehouse (for instructions, see the Databricks documentation for AWS, Azure, or Google Cloud) or a compute cluster running Databricks Runtime 16.4 LTS or later.
    • Permissions to manage catalogs, schemas, and service principals in your Databricks workspace and account console.
  4. Confirm with your Databricks metastore administrator that external data access is enabled on your Databricks Unity Catalog metastore. For instructions, see the Databricks documentation for AWS, Azure, or Google Cloud.

Required roles

To get the permissions that you need to complete the tasks in this tutorial, ask your administrator to grant you the following IAM roles on your project:

For more information about granting roles, see Manage access to projects, folders, and organizations.

You might also be able to get the required permissions through custom roles or other predefined roles.

Prepare your environment

Activate Cloud Shell and prepare your environment:

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

  2. In Cloud Shell, set the following environment variables:

    export PROJECT_ID=$(gcloud config get-value project)
    export REGION="us-central1"
    export FEDERATED_CATALOG_ID="databricks_retail_catalog"
    export BQ_DATASET_ID="retail_analytics"
    export UNITY_INSTANCE_NAME="UNITY_INSTANCE_NAME"
    export UNITY_CATALOG_NAME="UNITY_CATALOG_NAME"
    export UNITY_NAMESPACE="retail_db"

    Set REGION to the Google Cloud region closest to your Databricks storage (for example, us-central1), or the same region if your Databricks workspace is on Google Cloud.

    Replace the following:

    • UNITY_INSTANCE_NAME: your Databricks workspace hostname without the https:// prefix, such as dbc-a1b2c3d4-e5f6.cloud.databricks.com (on AWS), adb-1234567890123456.7.azuredatabricks.net (on Azure), or 1234567890123456.7.gcp.databricks.com (on Google Cloud).
    • UNITY_CATALOG_NAME: the name of your target catalog in Databricks Unity Catalog, such as gcp_federation_catalog.
  3. Enable the following APIs, which provide access to BigQuery and to the Lakehouse runtime catalog:

    gcloud services enable \
        bigquery.googleapis.com \
        biglake.googleapis.com \
        --project="${PROJECT_ID}"

Create an Iceberg table in Databricks Unity Catalog

Create a sample dim_products Iceberg table:

  1. In your Databricks workspace, open the SQL Editor. For instructions, see the Databricks documentation for AWS, Azure, or Google Cloud.

  2. If you don't already have a catalog backed by an external storage location, create one:

    CREATE CATALOG IF NOT EXISTS UNITY_CATALOG_NAME
    MANAGED LOCATION 'EXTERNAL_STORAGE_URI';

    Replace the following:

    • UNITY_CATALOG_NAME: the name of your target catalog in Databricks Unity Catalog, such as gcp_federation_catalog.
    • EXTERNAL_STORAGE_URI: the URI of your external storage location, such as s3://example-bucket/unity-catalog/, abfss://example-container@exampleaccount.dfs.core.windows.net/unity-catalog/, or gs://example-bucket/unity-catalog/.
  3. Create a schema named retail_db and the dim_products table, and insert sample product records:

    CREATE SCHEMA IF NOT EXISTS UNITY_CATALOG_NAME.retail_db;
    
    CREATE OR REPLACE TABLE UNITY_CATALOG_NAME.retail_db.dim_products (
      product_id STRING,
      product_name STRING,
      category STRING,
      brand STRING,
      unit_cost DOUBLE
    ) USING iceberg;
    
    INSERT INTO UNITY_CATALOG_NAME.retail_db.dim_products VALUES
      ('P101', 'Trail Running Shoes', 'Footwear', 'Cymbal Trail', 58.00),
      ('P102', 'Insulated Water Bottle', 'Accessories', 'Cymbal Gear', 12.50),
      ('P103', 'Ultralight Daypack', 'Gear', 'Cymbal Pack', 42.00),
      ('P104', 'Merino Wool Socks', 'Apparel', 'Cymbal Trail', 8.25);

Configure a Databricks service principal

Create a Databricks service principal and grant it privileges to read your target catalog and schema and to vend storage credentials:

  1. Create a Databricks service principal (such as lakehouse-federation-sp) and copy its Application ID UUID. For instructions, see the Databricks documentation for AWS, Azure, or Google Cloud.

  2. In the Databricks SQL Editor, grant the service principal access to read the target catalog and schema and vend storage credentials:

    GRANT USE CATALOG ON CATALOG UNITY_CATALOG_NAME
      TO `SERVICE_PRINCIPAL_APPLICATION_ID`;
    
    GRANT USE SCHEMA, SELECT, EXTERNAL USE SCHEMA
      ON SCHEMA UNITY_CATALOG_NAME.retail_db
      TO `SERVICE_PRINCIPAL_APPLICATION_ID`;

    Replace SERVICE_PRINCIPAL_APPLICATION_ID with the Application ID UUID of your Databricks service principal. Keep the surrounding backticks.

Create a federated catalog in the Lakehouse runtime catalog

Create a read-only federated catalog in the Lakehouse runtime catalog and configure OIDC token federation so Lakehouse can authenticate by using OIDC without storing static secrets. For more information about Databricks token federation, see the Databricks documentation for AWS, Azure, or Google Cloud:

  1. In Cloud Shell, set an environment variable for the Application ID of your Databricks service principal. Then, create the catalog with cross-cloud caching enabled:

    export UNITY_SP_APP_ID="SERVICE_PRINCIPAL_APPLICATION_ID"
    
    gcloud biglake iceberg catalogs create ${FEDERATED_CATALOG_ID} \
        --project="${PROJECT_ID}" \
        --primary-location="${REGION}" \
        --catalog-type="federated" \
        --federated-catalog-type="unity" \
        --unity-service-principal-application-id="${UNITY_SP_APP_ID}" \
        --unity-instance-name="${UNITY_INSTANCE_NAME}" \
        --unity-catalog-name="${UNITY_CATALOG_NAME}" \
        --namespace-filters="${UNITY_NAMESPACE}" \
        --cross-cloud-cache="enabled"

    Replace SERVICE_PRINCIPAL_APPLICATION_ID with the Application ID UUID of your Databricks service principal.

  2. Retrieve the numeric unique ID of the service account that the Lakehouse runtime catalog provisioned for your catalog:

    gcloud biglake iceberg catalogs describe ${FEDERATED_CATALOG_ID} \
        --project="${PROJECT_ID}" \
        --format='value(biglake-service-account-id)'

    Copy the ID from the command output.

  3. In your Databricks account console, create a service principal federation policy with the following values:

    • Issuer: https://accounts.google.com.
    • Subject: the numeric unique ID from the preceding step.
    • Audiences: https://accounts.cloud.databricks.com. Use this exact value even if your Databricks workspace is on Azure or Google Cloud. Lakehouse uses this OIDC token audience for all clouds, so don't replace it with a cloud-specific hostname.

    For instructions, see the Databricks documentation for AWS, Azure, or Google Cloud.

  4. In Cloud Shell, enable background metadata refresh on the federated catalog:

    gcloud biglake iceberg catalogs update ${FEDERATED_CATALOG_ID} \
        --project="${PROJECT_ID}" \
        --refresh-interval="300s"

Verify background metadata refresh

Before you join tables across clouds, check that your federated catalog has synchronized metadata from Databricks Unity Catalog and run a test query on dim_products:

  1. In Cloud Shell, inspect the federated catalog details:

    gcloud biglake iceberg catalogs describe ${FEDERATED_CATALOG_ID} \
        --project="${PROJECT_ID}"

    The output is similar to the following:

    ...
    federated-catalog-options:
      refresh-options:
        refresh-schedule:
          refresh-interval: 300s
        refresh-scope:
          namespace-filters:
          - retail_db
      refresh-status:
        end-time: '2026-03-01T12:05:12Z'
        start-time: '2026-03-01T12:05:08Z'
        status:
          message: Refresh completed. Check the audit logs for any namespace
            and table synchronization errors.
    ...
    

    In the federated-catalog-options.refresh-status field, confirm that end-time is populated, status.message reports completion, and no error code field appears. If the first refresh is still in progress, wait a few moments and rerun the command.

  2. Confirm that the retail_db namespace is synchronized in the federated catalog:

    gcloud biglake iceberg namespaces list \
        --project="${PROJECT_ID}" \
        --catalog="${FEDERATED_CATALOG_ID}"

    The output is similar to the following:

    NAME                                                                             NAMESPACE-ID
    projects/example-project/catalogs/databricks_retail_catalog/namespaces/retail_db retail_db
    
  3. Use BigQuery to query the federated dim_products table with the four-part P.C.N.T. (Project.Catalog.Namespace.Table) identifier:

    bq query \
        --project_id="${PROJECT_ID}" \
        --location="${REGION}" \
        --use_legacy_sql=false \
        "SELECT product_id, product_name, category, brand, unit_cost
         FROM \`${PROJECT_ID}.${FEDERATED_CATALOG_ID}.${UNITY_NAMESPACE}.dim_products\`
         ORDER BY product_id;"

    The output is similar to the following:

    +------------+------------------------+-------------+--------------+-----------+
    | product_id |      product_name      |  category   |    brand     | unit_cost |
    +------------+------------------------+-------------+--------------+-----------+
    | P101       | Trail Running Shoes    | Footwear    | Cymbal Trail |      58.0 |
    | P102       | Insulated Water Bottle | Accessories | Cymbal Gear  |      12.5 |
    | P103       | Ultralight Daypack     | Gear        | Cymbal Pack  |      42.0 |
    | P104       | Merino Wool Socks      | Apparel     | Cymbal Trail |      8.25 |
    +------------+------------------------+-------------+--------------+-----------+
    

Join Databricks Unity Catalog data with BigQuery tables

To analyze revenue and gross margin across clouds, create a local sales transaction table in BigQuery and join it with your dim_products table from Databricks Unity Catalog:

  1. In Cloud Shell, create a BigQuery dataset in the same region as your catalog in the Lakehouse runtime catalog:

    bq --location="${REGION}" mk --dataset "${PROJECT_ID}:${BQ_DATASET_ID}"
  2. Create the fact_sales table in BigQuery and populate it with sample sales transactions:

    bq query \
        --project_id="${PROJECT_ID}" \
        --location="${REGION}" \
        --use_legacy_sql=false << EOF
    CREATE OR REPLACE TABLE \`${PROJECT_ID}.${BQ_DATASET_ID}.fact_sales\` (
      transaction_id STRING,
      sale_date DATE,
      product_id STRING,
      store_region STRING,
      quantity INT64,
      sale_amount NUMERIC
    );
    
    INSERT INTO \`${PROJECT_ID}.${BQ_DATASET_ID}.fact_sales\` VALUES
      ('T1001', '2026-03-01', 'P101', 'North America', 3, 360.00),
      ('T1002', '2026-03-01', 'P102', 'North America', 5, 125.00),
      ('T1003', '2026-03-02', 'P103', 'Europe', 2, 190.00),
      ('T1004', '2026-03-02', 'P101', 'Europe', 2, 240.00),
      ('T1005', '2026-03-03', 'P104', 'Asia Pacific', 10, 180.00),
      ('T1006', '2026-03-03', 'P103', 'North America', 4, 380.00);
    EOF
  3. Create a view in BigQuery that joins fact_sales with the dim_products table, and query the view to calculate revenue and gross margin by product:

    bq query \
        --project_id="${PROJECT_ID}" \
        --location="${REGION}" \
        --use_legacy_sql=false << EOF
    CREATE OR REPLACE VIEW \`${PROJECT_ID}.${BQ_DATASET_ID}.v_cross_cloud_sales_margins\` AS
    SELECT
      s.transaction_id,
      s.sale_date,
      s.store_region,
      p.product_id,
      p.product_name,
      p.category,
      p.brand,
      s.quantity,
      s.sale_amount,
      ROUND(s.quantity * CAST(p.unit_cost AS NUMERIC), 2) AS total_cost,
      ROUND(s.sale_amount - (s.quantity * CAST(p.unit_cost AS NUMERIC)), 2) AS gross_margin
    FROM \`${PROJECT_ID}.${BQ_DATASET_ID}.fact_sales\` AS s
    INNER JOIN \`${PROJECT_ID}.${FEDERATED_CATALOG_ID}.${UNITY_NAMESPACE}.dim_products\` AS p
      ON s.product_id = p.product_id;
    
    SELECT
      product_name,
      category,
      SUM(quantity) AS units_sold,
      ROUND(SUM(sale_amount), 2) AS total_revenue,
      ROUND(SUM(gross_margin), 2) AS total_gross_margin
    FROM \`${PROJECT_ID}.${BQ_DATASET_ID}.v_cross_cloud_sales_margins\`
    GROUP BY product_name, category
    ORDER BY total_gross_margin DESC;
    EOF

    The output is similar to the following:

    +------------------------+-------------+------------+---------------+--------------------+
    |      product_name      |  category   | units_sold | total_revenue | total_gross_margin |
    +------------------------+-------------+------------+---------------+--------------------+
    | Ultralight Daypack     | Gear        |          6 |           570 |                318 |
    | Trail Running Shoes    | Footwear    |          5 |           600 |                310 |
    | Merino Wool Socks      | Apparel     |         10 |           180 |               97.5 |
    | Insulated Water Bottle | Accessories |          5 |           125 |               62.5 |
    +------------------------+-------------+------------+---------------+--------------------+
    

    Because you enabled cross-cloud caching, which has no additional charge, Lakehouse caches retrieved file blocks in your catalog's region. Repeated queries can read from the cache. Caching helps reduce egress costs, and actual savings depend on your query patterns. To view cache metrics, see Monitor cache usage and egress cost savings.

Clean up

To avoid incurring charges to your Google Cloud and Databricks accounts for the resources used in this tutorial, delete the Google Cloud resources (by deleting the project or by deleting the individual resources) and then delete the Databricks resources.

Delete the Google Cloud project

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

Delete individual Google Cloud resources

To keep your Google Cloud project, run the following commands in Cloud Shell to delete the BigQuery dataset and the federated catalog:

bq rm -r -f -d "${PROJECT_ID}:${BQ_DATASET_ID}"

gcloud biglake iceberg catalogs delete ${FEDERATED_CATALOG_ID} \
    --project="${PROJECT_ID}" \
    --quiet

Delete Databricks resources

Delete the resources that you created in Databricks:

  1. In the Databricks SQL Editor, drop the sample table and schema. If you created the catalog specifically for this tutorial, also drop the catalog:

    DROP TABLE IF EXISTS UNITY_CATALOG_NAME.retail_db.dim_products;
    DROP SCHEMA IF EXISTS UNITY_CATALOG_NAME.retail_db;
    -- Run the following statement only if you created this catalog for the tutorial:
    DROP CATALOG IF EXISTS UNITY_CATALOG_NAME CASCADE;

    Replace UNITY_CATALOG_NAME with the name of your target catalog in Databricks Unity Catalog.

  2. In your Databricks account console, delete the service principal federation policy and the Databricks service principal that you created for this tutorial. For instructions, see the Databricks documentation for federation policies (AWS, Azure, Google Cloud) and service principals (AWS, Azure, Google Cloud).

  3. If you created a Databricks SQL warehouse or compute cluster for this tutorial, stop or delete it. For instructions on managing SQL warehouses, see the Databricks documentation for AWS, Azure, or Google Cloud.

What's next