共用方式為


在計量檢視中使用聯結

計量檢視中的聯結支援從事實數據表到維度數據表(星型架構)的直接聯結,以及從事實數據表周游到維度數據表的聯結,然後聯結到細分數據表,允許跨正規化維度數據表的多躍點聯結(雪花式架構)。 此頁面說明如何在計量檢視的 YAML 定義中定義聯結。

備註

聯結的數據表不能包含 MAP 類型數據行。 若要瞭解如何從 MAP 類型的資料行提取值,請參閱 從映射或陣列分解巢狀元素

模型星狀結構描述

在星型架構中,是事實數據表, source 並使用 聯結一或多個維度數據表 LEFT OUTER JOIN。 度量視圖會根據選取的維度和量值,結合特定查詢所需的事實和維度表格。

使用 ON 子句或 USING 子句,在計量檢視中指定聯結數據行。

  • ON 子句:使用布爾表達式來定義聯結條件。
  • USING 子句:列出父數據表和聯結數據表中具有相同名稱的數據行。 對於第一層聯結,父系是計量檢視的來源。 對於雪花式架構中的巢狀聯結,父系是直接上游聯結。

聯結應該遵循多對一關聯性。 如果是多對多,則會選取聯結維度數據表中的第一個相符數據列。

備註

YAML 1.1 解析器(例如 PyYAML)可能會將某些未加引號的鍵(例如 onoffyesno、 、 或 NO)誤解為布林值。 這可能會導致聯結錯誤。若要避免此問題,請將這些金鑰括在引號中。 例如:'on': source.dim_fk = dim.pk

source: catalog.schema.fact_table

joins:

  # The on clause supports a boolean expression
  - name: dimension_table_1
    source: catalog.schema.dimension_table_1
    on: source.dimension_table_1_fk = dimension_table_1.pk

  # The using clause supports an array of columns
  # found in both of the tables being joined.
  - name: dimension_table_2
    source: catalog.schema.dimension_table_2
    using:
      - dimension_table_2_key_a
      - dimension_table_2_key_b

dimensions:

  # Dimension referencing a join column from dimension_table_1 using dot notation
  - name: Dimension table 1 key
    expr: dimension_table_1.pk

measures:

  # Measure referencing a join column from dimension_table_1
  - name: Count of dimension table 1 keys
    expr: COUNT(dimension_table_1.pk)

備註

source命名空間會參考指標檢視來源中的資料行,而聯結name會參考聯結資料表中的資料行。 例如,在聯結條件 source.dimension_table_1_fk = dimension_table_1.pk中, source 參照指標檢視的來源表格(fact_table),並 dimension_table_1 參照聯結的表格。 如果 子句中未提供前置詞,則聯結數據表的參考預設為聯結 on 數據表。

模型雪花結構描述

雪花式架構會藉由正規化維度數據表並將其聯機到細分,來擴充星型架構。 這會建立多層級聯結結構,以符合數據模型的深度。

備註

Snowflake 聯結需要 Databricks Runtime 計算 17.1 和更新版本。

若要定義模型雪花式架構的聯結:

  1. 建立計量檢視。
  2. 新增第一層 (星型架構) 聯結。
  3. 與其他維度數據表聯結。
  4. 藉由在檢視中新增維度來公開巢狀維度。

下列範例會使用 TPCH 數據集來說明如何建立雪花式架構的模型。 您可以在 Azure Databricks 工作區的 samples 目錄中存取 TPCH 資料集。

source: samples.tpch.orders

joins:
  - name: customer
    source: samples.tpch.customer
    on: source.o_custkey = customer.c_custkey
    joins:
      - name: nation
        source: samples.tpch.nation
        on: customer.c_nationkey = nation.n_nationkey
        joins:
          - name: region
            source: samples.tpch.region
            on: nation.n_regionkey = region.r_regionkey

dimensions:
  - name: clerk
    expr: o_clerk
  - name: customer
    expr: customer # returns the full customer row as a struct
  - name: customer_name
    expr: customer.c_name
  - name: nation
    expr: customer.nation
  - name: nation_name
    expr: customer.nation.n_name