🦈

【dbt cloud】Exposure・Metric・Semantic Layerの「使いどころ」

に公開

Exposure・Metric・Semantic Layerの「使いどころ」

初めに

dbtにはデータの付随情報、いわゆるメタデータを強化をする仕組みがいくつかあります。
今回は代表的なExposure・Metric・Semantic Layerを比較して使いどころをまとめました。

用語の確認

Exposureとは

下流の使われ方(type: dashboard / analysis / application / notebook / ml)を定義する機能です。

dbtはデータソースからtransformationまで自動でDAG(dbt内のリネージの呼称)が作成されます。一方で、最終週出力先を明示させたい場合はexposureを利用する必要があります。

例:最終出力先(今回はダッシュボード)とデータフローを結び付ける

作成する際は以下のようにymlファイルで定義します。
ymlファイルにはtype(最終出力先)の他にデータオーナー、連絡先を指定できます。

例:ダッシュボード、アプリなど最終アウトプットの情報を定義

version: 2

exposures:

# (A) ダッシュボード(モデル依存)
- name: orders_dashboard
  type: dashboard
  url: https://bi.example.com/dashboards/orders
  maturity: high
  owner:
    name: Data Team
    email: data@example.com
  depends_on:
    - ref('fct_orders')        # ダッシュボードの上流モデル
    - ref('stg_customers')

# (B) KPIタイル(メトリクス依存)
- name: monthly_orders_kpi
  type: dashboard
  url: https://bi.example.com/dashboards/orders#kpi
  owner:
    name: BizOps
    email: bizops@example.com
  depends_on:
    - metric('orders')         # Semantic Layer の metric を直接依存に
  description: "月次の注文数KPI(Semantic Layer由来)"

# (C) アプリケーション(モデル+メトリクスの複合)
- name: customer_portal_app
  type: application
  url: https://portal.example.com/
  owner:
    name: App Team
    email: app@example.com
  depends_on:
    - ref('fct_orders')
    - metric('orders_per_active_customer')
  maturity: medium

Metricとは

Semantic Layer上で宣言するビジネス指標。semantic model(entities/dimensions/measures)を土台に、simple/ratio/derived/cumulative/...型で定義し、共通の意味でクエリ可能。

例:active_customers、orders_per_active_customerを定義

metrics:
  - name: orders
    label: "Orders"
    description: "Count of orders"
    type: simple
    type_params:
      measure: orders

  - name: active_customers
    label: "Active Customers"
    description: "Distinct customers placing orders"
    type: simple
    type_params:
      measure: customers

  - name: orders_per_active_customer
    label: "Orders per Active Customer"
    description: "orders / active_customers"
    type: ratio
    type_params:
      numerator: orders
      denominator: active_customers

orders_per_active_customerのようにmetrics同士の計算も可能

Semantic Layerとは

MetricFlowが提供する指標の抽象化/合成/配信レイヤ指標→上流モデルの意味的つながり(セマンティックグラフ)を管理し、一貫したセルフサービスを実現。

例:モデルの意味を定義する。

例:各指標のディメンション、メジャーを定義

version: 2

semantic_models:
  - name: orders_semantic
    model: ref('fct_orders')
    defaults:
      agg_time_dimension: order_date

    entities:
      - name: order_id
        type: primary
      - name: customer_id
        type: foreign
        expr: customer_id

    dimensions:
      - name: order_date
        type: time
        type_params:
          time_granularity: day
      - name: status
        type: categorical

    measures:
      - name: orders
        agg: count
        expr: order_id
      - name: customers
        agg: count_distinct
        expr: customer_id

注意点として、Measuresを含む場合がデフォルトで適用する時間ベースの属性を指定する必要がある。

使いどころ

各特長を考慮して以下組み合わせを考えました。

1) Exposureを使う場面

最終アウトプットとデータフローを繋げる役割に留まります。

  • データ影響範囲可視化:ダッシュボード、アプリなど必要な上流一式を可視化できる。
  • 責任範囲の明確化:ownerが誰か明記しているので、問い合わせ先が分かる。

2) MetricFlow(Metrics × Semantic Layer)を使う場面

Semantic Layerを作成する際に、主流な組み合わせ。
定義を固めるだけではなく、外部連携もできるのでより広範に使用できます。
exposureと組み合わせて、よりリッチにすることも可能です。

  • 「同じ指標なのに数値が合わない」問題の解消:定義を中央管理してツール横断で一貫提供。
  • 指標の再利用・保守性向上:型付きで宣言し、CLI/APIから同一定義を呼び出すことが可能

例:semanticからmetricsで定義を固め、最終アウトプット情報までつなげる


まとめ

Exposure・Metric・Semantic Layerの用途と使いどころをまとめました。
以下触ってみた所感です。

良いと感じた点
・ymlで簡単にリネージ作れるのが素敵
・SemanticLayerとMetricsの配合便利

今後に期待したい機能
・exposure接続先の拡張

調べてみて、AI-readyが期待される昨今、今後動向を追っていきたい機能だなと思いました。
今後もdbt×Semantic Layerの情報は随時追っていきます。

参考文献

https://docs.getdbt.com/docs/build/about-metricflow
https://docs.getdbt.com/docs/build/exposures

truestarテックブログ
設定によりコメント欄が無効化されています