宣言的自動化バンドルを用いた機能ビューの管理

Important

この機能は、プライベート プレビュー段階にあります。 試すには、Azure Databricks の連絡先にお問い合わせください。

宣言的自動化バンドル(旧称Databricks Asset Bundles)は、Feature Viewsやそれを具現化するパイプラインなどのAzure Databricksリソースをプログラム的に検証、デプロイ、実行できるようにします。 「宣言型オートメーション バンドルとは」を参照してください。

このチュートリアルでは、4つのフィーチャービューを宣言し、それらを3通りの方法でマテリアライズします。具体的には、オフラインストアとオンラインストアの両方に書き込まれる2つのバッチ特徴量、オンラインストアに継続的に書き込まれるストリーミング特徴量、そしてソートゥーウィンドウを持つストリーミング特徴量で、これはバッチモードではオフラインストアとオンラインストアに、ストリーミングモードではオンラインストアにマテリアライズされます。 その後、設定を変更し、再デプロイし、すべてを削除します。

バンドルで機能を宣言すると、デプロイするすべてのワークスペースで同じ定義が得られ、プロジェクトの他の部分と一緒にソースとしてレビューされます。 これはノートブックから create_feature や materialize_features を呼び出す宣言的な代替手段です。 「 フィーチャー ビュー」を参照してください。

各ステップはYAMLとPythonの両方の構成を示します。 言語を選べば、ページはその選択に従って表示されます。

Requirements

  • Databricks CLIバージョン1.24.0以上です。 databricks --version を実行して確認します。 「Databricks CLI のインストールまたは更新」を参照してください。
  • ダイレクトデプロイメントエンジン featuresおよびmaterialization_setsリソースはTerraformデプロイメントエンジンによってサポートされていません。 リソースの互換性を参照してください。
  • Unity カタログ対応ワークスペース。 要件を参照してください。
  • 機能を作成し、それらをマテリアライズする権限。 「 アクセス許可」を参照してください。
  • 既存のオンラインストア。 「 オンラインストアを作成する」をご覧ください。
  • エンティティカラムとタイムスタンプカラムを持つ既存のDeltaテーブルで、バッチマテリアライゼーション用です。 Azure Databricks テーブルを参照してください。
  • ストリームのマテリアライズ用の既存のストリーム。 「 ストリームを作成する」をご覧ください。

ソーステーブルとストリームはそれぞれ独自のライフサイクルを持ち、どちらもバンドルリソースではありません。 まず作成し、テーブルの ステップ3 やストリームのステップ 4 のようにバンドル内で名前で参照します。

バンドル内のFeature Storeリソース

このチュートリアルでは、2種類のフィーチャーストアリソースタイプを宣言し、それらの差が展開コストを決定します。

  • 特徴ビュー:特徴の定義で、そのソース、集約、キーを網羅します。 軽量なメタデータです。 特徴表示(Feature Views)を参照し、設定参照については feature を参照してください。
  • マテリアライズセット: マテリアライズド特徴量の集合。 マテリアル化された特徴とは、パイプラインやジョブを通じてテーブルとして具体化された Feature View のことです。つまり、これが重い処理側です。 具現化された特徴を一つのセットにまとめることで、Azure Databricksはそれらを一緒に計算できると判断し、それらを保持する最小限のテーブルやLakeflowパイプラインにまとめます。 Materialize Feature Viewsを参照し、設定参照としてはmaterialization_setおよびmaterialization_set.materialized_featuresを参照してください。

その後のステップでは、バッチ特徴量、ストリーミング特徴量、そしてノコギリ型ウィンドウ付きのストリーミング特徴量を宣言します。 プロジェクトに必要なものだけを宣言してください。 物質化の仕組みについては、「Materialize Feature Views」を参照してください。

手順 1: 認証を設定する

まず、開発マシンのDatabricks CLIとAzure Databricksワークスペース間の認証を設定しましょう。 このページでは、OAuth ユーザー対マシン (U2M) 認証と、 DEFAULT という名前の対応する Azure Databricks 構成プロファイルを認証に使用することを前提としています。

Note

U2M 認証は、これらの手順をリアルタイムで試す場合に適しています。 完全に自動化されたワークフローの場合、Databricks では代わりに OAuth マシン間 (M2M) 認証を使用することをお勧めします。 OAuth を使用した Azure Databricks へのサービス プリンシパル アクセスの承認に関するページの M2M 認証のセットアップ手順を参照してください。

  1. Databricks CLI を使用して、ターゲット ワークスペースごとに次のコマンドを実行し、OAuth トークン管理をローカルで開始します。

    次のコマンドでは、 <workspace-url> をワークスペースごとの Azure Databricks URL に置き換えます (例: https://adb-1234567890123456.7.azuredatabricks.net)。

    databricks auth login --host <workspace-url>
    
  2. Databricks CLI では、入力した情報を Azure Databricks 構成プロファイルとして保存するように求められます。 Enter キーを押して提案されたプロファイル名を受け入れるか、新規または既存のプロファイル名を入力します。 同じ名前の既存のプロファイルは、入力した情報で上書きされます。 プロファイルを使用すると、複数のワークスペース間で認証コンテキストをすばやく切り替えることができます。

    既存のプロファイルの一覧を取得するには、別のターミナルまたはコマンド プロンプト内で、Databricks CLI を使用してコマンド databricks auth profiles を実行します。 特定のプロファイルの既存の設定を表示するには、コマンド databricks auth env --profile <profile-name>を実行します。

  3. Web ブラウザーで、画面の指示に従って、Azure Databricks ワークスペースにログインします。

  4. プロファイルの現在の OAuth トークン値とトークンの今後の有効期限のタイムスタンプを表示するには、次のいずれかのコマンドを実行します。

    • databricks auth token --host <workspace-url>
    • databricks auth token -p <profile-name>
    • databricks auth token --host <workspace-url> -p <profile-name>

    同じ --host 値を持つ複数のプロファイルがある場合は、Databricks CLI が正しく一致する OAuth トークン情報を見つけるのに役立つ --host と -p のオプションを一緒に指定することが必要になる場合があります。

手順 2: バンドルを作成する

バンドル用の空のディレクトリを作成し、ルートに databricks.yml ファイル、隣に resources ディレクトリを置きます。

YAML

以下の各ステップで、バンドルリソースを宣言するための resources/ の下に1つのファイルが追加されます:

databricks.yml
resources/
  batch_feature.yml
  streaming_feature.yml
  streaming_feature_sawtooth.yml

databricks.yml には3つのものがあります。

  • バンドル名。
  • includeエントリ。バンドルがどのファイルからリソースを読み込むかを決めるもの。
  • 変数は、カタログ名、スキーマ名、ソース名など、リソースが参照できる値を保持します。
bundle:
  name: feature-store-quickstart

include:
  - resources/*.yml

variables:
  catalog:
    description: The catalog that features are written to.
    default: main
  schema:
    description: The schema that features are written to.
    default: features_demo
  source_table:
    description: The three-level name of the source Delta table.
    default: main.features_demo.transactions
  stream:
    description: The three-level name of a Stream registered in Unity Catalog.
    default: main.features_demo.events
  online_store:
    description: The online store that serves the features.
    default: my_online_store

代わりにすべてを databricks.yml で宣言するのも機能します。 includeを省略し、各ステップの宣言を1つのresourcesマッピングにまとめます。

bundle:
  name: feature-store-quickstart

variables:
  # The variables above.

resources:
  features:
    # The Feature Views from the following steps.
  materialization_sets:
    # The materialization sets from the following steps.

Python

Pythonで宣言された資源は、python内のdatabricks.ymlマッピングを通じてのみ見つかります。 これがなければバンドルは検証とデプロイに成功しますが、リソースは含まれていないので、今すぐ追加してください:

bundle:
  name: feature-store-quickstart

python:
  venv_path: .venv
  resources:
    - 'resources:load_resources'

variables:
  catalog:
    description: The catalog that features are written to.
    default: main
  schema:
    description: The schema that features are written to.
    default: features_demo
  source_table:
    description: The three-level name of the source Delta table.
    default: main.features_demo.transactions
  stream:
    description: The three-level name of a Stream registered in Unity Catalog.
    default: main.features_demo.events
  online_store:
    description: The online store that serves the features.
    default: my_online_store

venv_path はDatabricks CLIがこのコードを実行する仮想環境です。 resourcesの各エントリは関数を<module>:<function>として指定し、Databricks CLIはバンドルのリソースを収集するためにこれを呼び出します。 resourcesで複数の<module>:<function>エントリが宣言されている場合、DatabricksのCLIはすべての関数を呼び出し、それぞれが返すリソースを収集します。

次にプロジェクトとその仮想環境を作成し、パッケージを追加します:

uv init --bare
uv venv .venv
uv add databricks-bundles

resources/__init__.pyを、databricks.ymlという名前の関数で作成します:

from databricks.bundles.core import Bundle, Resources


def load_resources(bundle: Bundle) -> Resources:
    resources = Resources()
    # The following steps add resources here.
    return resources

プロジェクトテンプレートやミューテーターを含むPythonでのバンドル作成についての詳細は、「Pythonのバンドル設定」をご覧ください。

ステップ3: バッチ機能の追加

これら2つのフィーチャービューは、ユーザーあたりの取引を過去7日間および直近30日間にわたってカウントし、いずれも毎日再計算され、オフラインストアとオンラインストアの両方にまとめて現れます。

フィーチャービュー自体は定義に過ぎません。 マテリアライゼーションセットがそこからデータを生成するのです。

各リソースには2つの名前があります。 transaction_count は リソースキーです。バンドル内で一意な名前で、 databricks bundle コマンドがリソースをアドレスに使い、他の設定参照がどれかを扱います。 例えば、transaction_count の、集合 ${resources.features.transaction_count.full_name} 内にある2つの具現化された特徴は、Feature View の名前を transactions として参照し、これにより、その集合が Feature View transaction_count に依存していることがバンドルに伝えられます。

この依存関係のため、両方が作成されるときには、transactions は集合 transaction_count より先に作成され、両方が破棄されるときには、その後で削除されます。 ${}なしで書かれたmain.features_demo.transaction_count_7dのようなハードコード名は、その依存関係を省略し、予測不能な順序でデプロイされます。

full_name はUnityカタログに登録するフィーチャービューのために選ぶ3レベル名であり、後でクエリする名前です。 この二つの名前は独立しています。

持続時間とは、s の接尾辞が付いた秒数のことです。例えば、7日間を表す 604800s のように。 「 ウィンドウ、ソース、関数の持続時間の指定」を参照してください。

materialized_featuresの各エントリーは1つのマテリアライズされたフィーチャーメンバーです。1つのフィーチャービューが1つの目的地にマテリアライズされます。 これらのフィーチャービューはオフラインとオンラインの両方の目的地に具現化されるため、それぞれ2つの具現化されたフィーチャーメンバーを生成し、それぞれのメンバーが独自のトリガーを持ちます。 両方のフィーチャービューは、セットにはオフライン宛先が1つあり、オンライン宛先は最大1つしか持てないため、同じオフライン宛先とオンライン宛先に書き込みます。

YAML

resources/batch_feature.ymlを作成します。

resources:
  features:
    transaction_count:
      full_name: ${var.catalog}.${var.schema}.transaction_count_7d
      description: Transactions per user over a trailing seven days
      source:
        delta_table_source:
          full_name: ${var.source_table}
      function:
        aggregation_function:
          count_function:
            input: transaction_id
          time_window:
            sliding:
              window_duration: 604800s
              slide_duration: 86400s
      entities:
        - name: user_id
      timeseries_column:
        name: transaction_time

    transaction_count_30d:
      full_name: ${var.catalog}.${var.schema}.transaction_count_30d
      description: Transactions per user over a trailing 30 days
      source:
        delta_table_source:
          full_name: ${var.source_table}
      function:
        aggregation_function:
          count_function:
            input: transaction_id
          time_window:
            sliding:
              window_duration: 2592000s
              slide_duration: 86400s
      entities:
        - name: user_id
      timeseries_column:
        name: transaction_time

  materialization_sets:
    transactions:
      materialized_features:
        # Batch offline materialized feature of transaction_count.
        - feature_name: ${resources.features.transaction_count.full_name}
          offline_store_config:
            catalog_name: ${var.catalog}
            schema_name: ${var.schema}
            table_name_prefix: transactions_offline
          cron_schedule_trigger:
            mode: DERIVED
        # Batch online materialized feature of transaction_count.
        - feature_name: ${resources.features.transaction_count.full_name}
          online_store_config:
            online_store_name: ${var.online_store}
            catalog_name: ${var.catalog}
            schema_name: ${var.schema}
            table_name_prefix: transactions_online
          cron_schedule_trigger:
            mode: DERIVED
        # Batch offline materialized feature of transaction_count_30d.
        - feature_name: ${resources.features.transaction_count_30d.full_name}
          offline_store_config:
            catalog_name: ${var.catalog}
            schema_name: ${var.schema}
            table_name_prefix: transactions_offline
          cron_schedule_trigger:
            mode: DERIVED
        # Batch online materialized feature of transaction_count_30d.
        - feature_name: ${resources.features.transaction_count_30d.full_name}
          online_store_config:
            online_store_name: ${var.online_store}
            catalog_name: ${var.catalog}
            schema_name: ${var.schema}
            table_name_prefix: transactions_online
          cron_schedule_trigger:
            mode: DERIVED

Python

インポートを resources/__init__.pyに追加してください:

from databricks.bundles.features import (
    AggregationFunction,
    CountFunction,
    DataSource,
    DeltaTableSource,
    EntityColumn,
    Feature,
    Function,
    SlidingWindow,
    TimeWindow,
    TimeseriesColumn,
)
from databricks.bundles.materialization_sets import (
    CronSchedule,
    CronScheduleMode,
    MaterializationSet,
    MaterializedFeature,
    OfflineStoreConfig,
    OnlineStoreConfig,
)

そして、load_resources の本文に以下を加えます。

resources.add_feature(
    'transaction_count',
    Feature(
        full_name='${var.catalog}.${var.schema}.transaction_count_7d',
        description='Transactions per user over a trailing seven days',
        source=DataSource(delta_table_source=DeltaTableSource(full_name='${var.source_table}')),
        function=Function(
            aggregation_function=AggregationFunction(
                count_function=CountFunction(input='transaction_id'),
                time_window=TimeWindow(
                    sliding=SlidingWindow(window_duration='604800s', slide_duration='86400s')
                ),
            )
        ),
        entities=[EntityColumn(name='user_id')],
        timeseries_column=TimeseriesColumn(name='transaction_time'),
    ),
)

resources.add_feature(
    'transaction_count_30d',
    Feature(
        full_name='${var.catalog}.${var.schema}.transaction_count_30d',
        description='Transactions per user over a trailing 30 days',
        source=DataSource(delta_table_source=DeltaTableSource(full_name='${var.source_table}')),
        function=Function(
            aggregation_function=AggregationFunction(
                count_function=CountFunction(input='transaction_id'),
                time_window=TimeWindow(
                    sliding=SlidingWindow(window_duration='2592000s', slide_duration='86400s')
                ),
            )
        ),
        entities=[EntityColumn(name='user_id')],
        timeseries_column=TimeseriesColumn(name='transaction_time'),
    ),
)

resources.add_materialization_set(
    'transactions',
    MaterializationSet(
        materialized_features=[
            # Batch offline materialized feature of transaction_count.
            MaterializedFeature(
                feature_name='${resources.features.transaction_count.full_name}',
                offline_store_config=OfflineStoreConfig(
                    catalog_name='${var.catalog}',
                    schema_name='${var.schema}',
                    table_name_prefix='transactions_offline',
                ),
                cron_schedule_trigger=CronSchedule(mode=CronScheduleMode.DERIVED),
            ),
            # Batch online materialized feature of transaction_count.
            MaterializedFeature(
                feature_name='${resources.features.transaction_count.full_name}',
                online_store_config=OnlineStoreConfig(
                    online_store_name='${var.online_store}',
                    catalog_name='${var.catalog}',
                    schema_name='${var.schema}',
                    table_name_prefix='transactions_online',
                ),
                cron_schedule_trigger=CronSchedule(mode=CronScheduleMode.DERIVED),
            ),
            # Batch offline materialized feature of transaction_count_30d.
            MaterializedFeature(
                feature_name='${resources.features.transaction_count_30d.full_name}',
                offline_store_config=OfflineStoreConfig(
                    catalog_name='${var.catalog}',
                    schema_name='${var.schema}',
                    table_name_prefix='transactions_offline',
                ),
                cron_schedule_trigger=CronSchedule(mode=CronScheduleMode.DERIVED),
            ),
            # Batch online materialized feature of transaction_count_30d.
            MaterializedFeature(
                feature_name='${resources.features.transaction_count_30d.full_name}',
                online_store_config=OnlineStoreConfig(
                    online_store_name='${var.online_store}',
                    catalog_name='${var.catalog}',
                    schema_name='${var.schema}',
                    table_name_prefix='transactions_online',
                ),
                cron_schedule_trigger=CronSchedule(mode=CronScheduleMode.DERIVED),
            ),
        ]
    ),
)

ステップ4: ストリーミング機能を追加

このフィーチャービューは、過去90分間のイベントをキーごとにカウントし、オンラインストアに継続的に現れます。 1つの宛先に書き込むため、そのセットは1つの要素を宣言します。

ストリーミングで具現化されたフィーチャーメンバーは streaming_mode を持ち、 cron_schedule_triggerはありません。 モードやそれぞれの意味については、 materialization_set.materialized_features.streaming_mode をご覧ください。

これは ステップ3 で定義されたセットに追加されたマテリアライズド フィーチャーではなく、2 つ目のマテリアライゼーション セットです。これは、マテリアライゼーション セットでは、ストリーミング モードでマテリアライズされたフィーチャー ビューと、バッチでマテリアライズされたフィーチャー ビューを混在させることができないためです。 ノコギリ型ウィンドウ付きのストリーミング Feature View は例外です。 そのバッチ版とストリーミング版の実体化された特徴量も、Step 5と同じセットに属します。

フィーチャービューは名前でストリームを読み取ります。 ストリームはバンドル外で単独で作成・バージョン管理されます。これはバッチ機能が読み取るデルタテーブルと同じ方法です。 ストリームを作成するには、「 ストリームを作成」をご覧ください。

ストリーミング機能ビューは、入力列を単なる列名ではなくストリームレコードへのパスとして扱うため、例が value.* を示している箇所では、Stream のスキーマで使われている名前に置き換えてください。

YAML

resources/streaming_feature.ymlを作成します。

resources:
  features:
    event_count_90m:
      full_name: ${var.catalog}.${var.schema}.event_count_90m
      description: Events per key over a trailing 90 minutes
      source:
        stream_source:
          full_name: ${var.stream}
      function:
        aggregation_function:
          count_function:
            input: value.amount
          time_window:
            rolling:
              window_duration: 5400s
      entities:
        - name: value.key
      timeseries_column:
        name: value.event_timestamp

  materialization_sets:
    events_online:
      materialized_features:
        - feature_name: ${resources.features.event_count_90m.full_name}
          online_store_config:
            online_store_name: ${var.online_store}
            catalog_name: ${var.catalog}
            schema_name: ${var.schema}
            table_name_prefix: event_count_90m_online
          streaming_mode:
            mode: STREAMING_MODE_TYPE_RTM

Python

databricks.bundles.featuresからのインポートにRollingWindowとStreamSourceを加え、databricks.bundles.materialization_setsからのインポートにStreamingModeとStreamingModeStreamingModeTypeを加え、これをload_resourcesに加えます:

resources.add_feature(
    'event_count_90m',
    Feature(
        full_name='${var.catalog}.${var.schema}.event_count_90m',
        description='Events per key over a trailing 90 minutes',
        source=DataSource(stream_source=StreamSource(full_name='${var.stream}')),
        function=Function(
            aggregation_function=AggregationFunction(
                count_function=CountFunction(input='value.amount'),
                time_window=TimeWindow(rolling=RollingWindow(window_duration='5400s')),
            )
        ),
        entities=[EntityColumn(name='value.key')],
        timeseries_column=TimeseriesColumn(name='value.event_timestamp'),
    ),
)

resources.add_materialization_set(
    'events_online',
    MaterializationSet(
        materialized_features=[
            MaterializedFeature(
                feature_name='${resources.features.event_count_90m.full_name}',
                online_store_config=OnlineStoreConfig(
                    online_store_name='${var.online_store}',
                    catalog_name='${var.catalog}',
                    schema_name='${var.schema}',
                    table_name_prefix='event_count_90m_online',
                ),
                streaming_mode=StreamingMode(
                    mode=StreamingModeStreamingModeType.STREAMING_MODE_TYPE_RTM
                ),
            )
        ]
    ),
)

ステップ5: ノコギリ型ウィンドウ付きのストリーミングフィーチャーを追加する

集約にソートゥースウィンドウを使用するストリーミング フィーチャービューをマテリアライズすると、3 つのマテリアライズドフィーチャー(オフラインのバッチ マテリアライズドフィーチャー、オンラインのバッチ マテリアライズドフィーチャー、オンラインのストリーミング マテリアライズドフィーチャー)が生成されます。 サーバーは3つすべてを必要とし、マテリアライズされた特徴量の一部を宣言することは拒否されます。 2 つのバッチ マテリアライズド フィーチャはDERIVEDをcron_schedule_triggerとして使用する必要があり、Azure Databricks がフィーチャ定義からスケジュールを選択できるようにします。 バッチオンラインのマテリアライズド機能とストリーミングオンラインのマテリアライズド機能は同じオンラインストア構成を共有しなければなりません。 これらの要件を満たさないマテリアル化セットはデプロイ時に却下されます。 完全なリストについては、マテリアライゼーション セットが満たさなければならないルールを参照してください。

YAML

resources/streaming_feature_sawtooth.ymlを作成します。

resources:
  features:
    amount_sum:
      full_name: ${var.catalog}.${var.schema}.amount_sum_3d
      description: Sum of amount per key over a sawtooth window
      source:
        stream_source:
          full_name: ${var.stream}
      function:
        aggregation_function:
          sum:
            input: value.amount
          time_window:
            sawtooth:
              window_duration: 259200s
      entities:
        - name: value.key
      timeseries_column:
        name: value.event_timestamp

  materialization_sets:
    amounts:
      materialized_features:
        # Batch offline materialized feature.
        - feature_name: ${resources.features.amount_sum.full_name}
          offline_store_config:
            catalog_name: ${var.catalog}
            schema_name: ${var.schema}
            table_name_prefix: amount_sum_offline
          cron_schedule_trigger:
            mode: DERIVED
        # Batch online materialized feature.
        - feature_name: ${resources.features.amount_sum.full_name}
          online_store_config:
            online_store_name: ${var.online_store}
            catalog_name: ${var.catalog}
            schema_name: ${var.schema}
            table_name_prefix: amount_sum_online
          cron_schedule_trigger:
            mode: DERIVED
        # Streaming online materialized feature.
        - feature_name: ${resources.features.amount_sum.full_name}
          online_store_config:
            online_store_name: ${var.online_store}
            catalog_name: ${var.catalog}
            schema_name: ${var.schema}
            table_name_prefix: amount_sum_online
          streaming_mode:
            mode: STREAMING_MODE_TYPE_RTM

Python

SumFunctionからのインポートにdatabricks.bundles.featuresとSawtoothWindowを追加し、次にload_resourcesにこれを追加します:

resources.add_feature(
    'amount_sum',
    Feature(
        full_name='${var.catalog}.${var.schema}.amount_sum_3d',
        description='Sum of amount per key over a sawtooth window',
        source=DataSource(stream_source=StreamSource(full_name='${var.stream}')),
        function=Function(
            aggregation_function=AggregationFunction(
                sum=SumFunction(input='value.amount'),
                time_window=TimeWindow(sawtooth=SawtoothWindow(window_duration='259200s')),
            )
        ),
        entities=[EntityColumn(name='value.key')],
        timeseries_column=TimeseriesColumn(name='value.event_timestamp'),
    ),
)

resources.add_materialization_set(
    'amounts',
    MaterializationSet(
        materialized_features=[
            # Batch offline materialized feature.
            MaterializedFeature(
                feature_name='${resources.features.amount_sum.full_name}',
                offline_store_config=OfflineStoreConfig(
                    catalog_name='${var.catalog}',
                    schema_name='${var.schema}',
                    table_name_prefix='amount_sum_offline',
                ),
                cron_schedule_trigger=CronSchedule(mode=CronScheduleMode.DERIVED),
            ),
            # Batch online materialized feature.
            MaterializedFeature(
                feature_name='${resources.features.amount_sum.full_name}',
                online_store_config=OnlineStoreConfig(
                    online_store_name='${var.online_store}',
                    catalog_name='${var.catalog}',
                    schema_name='${var.schema}',
                    table_name_prefix='amount_sum_online',
                ),
                cron_schedule_trigger=CronSchedule(mode=CronScheduleMode.DERIVED),
            ),
            # Streaming online materialized feature.
            MaterializedFeature(
                feature_name='${resources.features.amount_sum.full_name}',
                online_store_config=OnlineStoreConfig(
                    online_store_name='${var.online_store}',
                    catalog_name='${var.catalog}',
                    schema_name='${var.schema}',
                    table_name_prefix='amount_sum_online',
                ),
                streaming_mode=StreamingMode(
                    mode=StreamingModeStreamingModeType.STREAMING_MODE_TYPE_RTM
                ),
            ),
        ]
    ),
)

ステップ6:検証と計画

databricks.yml と、バンドルが読み込むすべてのリソースファイルを読み込む構成を確認してください。

databricks bundle validate

validate はまた、そのフィールドが受け入れられているかどうかを知る最も安価な方法でもあります。 バンドルが認識できないフィールドを、そのパスと、元のファイル名および行番号とともに示します:

Warning: unknown field: bogus_field
  at resources.features.transaction_count.function.aggregation_function.time_window.sliding
  in resources/batch_feature.yml:16:15

認識されないフィールドは警告として報告され、その値は削除されるため、出口コードだけでなく出力も読み取ってください。 警告をエラーとして扱うには --strict を指定します。 各ウィンドウ、関数、ソースが受け入れるフィールドについては、featureを参照してください。

サーバーが強制するルール、例えばノコギリ型ウィンドウを持つフィーチャービューは3つの物質化された特徴すべてを宣言しなければならないというルールはここでチェックされず、 databricks bundle deployに表面化されます。

その後、デプロイメントが実行前に何をするのかを確認しましょう:

databricks bundle plan

最初のデプロイでは、7つのリソース(4つのフィーチャービューと3つのマテリアライゼーションセット)すべてが createを報告します。

create features.amount_sum
create features.event_count_90m
create features.transaction_count
create features.transaction_count_30d
create materialization_sets.amounts
create materialization_sets.events_online
create materialization_sets.transactions

Plan: 7 to add, 0 to change, 0 to delete, 0 unchanged

その後の変更点でも plan を実行して、どのような対応が計画されているか確認してください。

ステップ7:デプロイ

ワークスペースにバンドルをデプロイします。

databricks bundle deploy

デプロイでは、各リソースが作成されるたびに表示されます:

Uploading bundle files to /Workspace/Users/someone@example.com/.bundle/feature-store-quickstart/default/files...
Created features.transaction_count_30d
Created features.transaction_count
Created features.amount_sum
Created features.event_count_90m
Created materialization_sets.transactions
Created materialization_sets.events_online
Created materialization_sets.amounts
Files: 4 uploaded, 0 deleted
Resources: 7 created, 0 changed, 0 deleted, 0 unchanged

ステップ8:作成内容を確認する

databricks bundle summary -o json 前回の展開時に記録されたリソース識別子(マテリアライゼーションセットIDなど)とともにバンドル構成を報告します。 ステップ5で定義された物質化集合について:

{
  "id": "1234567890abcdef1234567890abcdef",
  "materialized_features": [
    {
      "cron_schedule_trigger": {
        "mode": "DERIVED"
      },
      "feature_name": "${resources.features.amount_sum.full_name}",
      "offline_store_config": {
        "catalog_name": "main",
        "schema_name": "features_demo",
        "table_name_prefix": "amount_sum_offline"
      }
    },
    {
      "cron_schedule_trigger": {
        "mode": "DERIVED"
      },
      "feature_name": "${resources.features.amount_sum.full_name}",
      "online_store_config": {
        "catalog_name": "main",
        "online_store_name": "my_online_store",
        "schema_name": "features_demo",
        "table_name_prefix": "amount_sum_online"
      }
    },
    {
      "feature_name": "${resources.features.amount_sum.full_name}",
      "online_store_config": {
        "catalog_name": "main",
        "online_store_name": "my_online_store",
        "schema_name": "features_demo",
        "table_name_prefix": "amount_sum_online"
      },
      "streaming_mode": {
        "mode": "STREAMING_MODE_TYPE_RTM"
      }
    }
  ]
}

サーバーに何が保存されているかを見るには、databricks bundle summary出力の id を使って Feature Engineering コマンドでセットを読み戻してください。

databricks feature-engineering get-materialization-set <materialization-set-id>

応答は、サーバーが同じ集合に対して保存した内容と、それが生成するフィールドも含みます。

{
  "materialization_set_id": "1234567890abcdef1234567890abcdef",
  "materialized_features": [
    {
      "cron_schedule_trigger": {
        "cron_expression": "0 0 2 * * ?",
        "mode": "DERIVED",
        "timezone_id": "UTC"
      },
      "feature_name": "main.features_demo.amount_sum_3d",
      "is_online": true,
      "job_id": 123456789012345,
      "materialization_set_id": "1234567890abcdef1234567890abcdef",
      "materialized_feature_id": "22222222222222222222222222222222",
      "online_store_config": {
        "catalog_name": "",
        "online_store_name": "my_online_store",
        "schema_name": "",
        "table_name_prefix": ""
      },
      "pipeline_schedule_state": "ACTIVE",
      "table_name": "main.features_demo.amount_sum_online_ceq88t"
    },
    {
      "feature_name": "main.features_demo.amount_sum_3d",
      "is_online": true,
      "job_id": 234567890123456,
      "materialization_set_id": "1234567890abcdef1234567890abcdef",
      "materialized_feature_id": "33333333333333333333333333333333",
      "online_store_config": {
        "catalog_name": "",
        "online_store_name": "my_online_store",
        "schema_name": "",
        "table_name_prefix": ""
      },
      "pipeline_id": "12345678-1234-1234-1234-123456789012",
      "pipeline_schedule_state": "ACTIVE",
      "streaming_mode": {
        "mode": "STREAMING_MODE_TYPE_RTM"
      },
      "table_name": "main.features_demo.amount_sum_online_qiohun"
    },
    {
      "cron_schedule_trigger": {
        "cron_expression": "0 0 2 * * ?",
        "mode": "DERIVED",
        "timezone_id": "UTC"
      },
      "feature_name": "main.features_demo.amount_sum_3d",
      "is_online": false,
      "job_id": 123456789012345,
      "materialization_set_id": "1234567890abcdef1234567890abcdef",
      "materialized_feature_id": "11111111111111111111111111111111",
      "pipeline_schedule_state": "ACTIVE",
      "table_name": "main.features_demo.amount_sum_offline_ceq88t"
    }
  ]
}

ステップ9:構成の変更

以下の例は、以前のステップのフィーチャービューやマテリアライゼーションセットを変更し、各変更がデプロイ時に何をするかを示しています。 例が異なる出発点を挙げない限り、それぞれは ステップ7の展開から始まります。 それぞれがリソースを宣言するファイルの編集であり、databricks.ymlのファイルですべてを宣言した場合はresourcesマッピングの編集です。 Pythonタブでは、編集はresources/__init__.pyに入ります。 どの変更がその場で適用され、どれがリソースを置き換え、どれが却下されるかについては、フィーチャー ビューの変更 および マテリアライゼーション セットの変更 を参照してください。

フィーチャービューの具現化された特徴をすべて削除する

1つのフィーチャービューのすべてのマテリアル化されたフィーチャーをマテリアル化セットから除去する操作はその場で適用されるため、フィーチャーデータが再計算されることはありません。 「 物質化集合の変更」を参照してください。

以下では、Feature View と transactions のために transaction_count_30d で定義されたセット transaction_count を使用し、Feature View transaction_count_30d のマテリアライズされたすべての特徴を削除します。 同じセット内の transaction_count のマテリアライズされた特徴量は手つかずです:

YAML

 materialization_sets:
   transactions:
     materialized_features:
       # Batch offline materialized feature of transaction_count.
       - feature_name: ${resources.features.transaction_count.full_name}
         offline_store_config: # As in Step 3.
         cron_schedule_trigger:
           mode: DERIVED
       # Batch online materialized feature of transaction_count.
       - feature_name: ${resources.features.transaction_count.full_name}
         online_store_config: # As in Step 3.
         cron_schedule_trigger:
           mode: DERIVED
-      # Batch offline materialized feature of transaction_count_30d.
-      - feature_name: ${resources.features.transaction_count_30d.full_name}
-        offline_store_config: # As in Step 3.
-        cron_schedule_trigger:
-          mode: DERIVED
-      # Batch online materialized feature of transaction_count_30d.
-      - feature_name: ${resources.features.transaction_count_30d.full_name}
-        online_store_config: # As in Step 3.
-        cron_schedule_trigger:
-          mode: DERIVED

Python

 resources.add_materialization_set(
     'transactions',
     MaterializationSet(
         materialized_features=[
             # Batch offline materialized feature of transaction_count.
             MaterializedFeature(...),  # As in Step 3.
             # Batch online materialized feature of transaction_count.
             MaterializedFeature(...),  # As in Step 3.
-            # Batch offline materialized feature of transaction_count_30d.
-            MaterializedFeature(...),  # As in Step 3.
-            # Batch online materialized feature of transaction_count_30d.
-            MaterializedFeature(...),  # As in Step 3.
         ]
     ),
 )

databricks bundle plan セットを更新として報告します:

update materialization_sets.transactions

Plan: 0 to add, 1 to change, 0 to delete, 6 unchanged

特徴ビューの不変フィールドを変更する

マテリアライゼーションセットがフィーチャービューに依存している場合、フィーチャービューの不変フィールドの変更はデプロイ時に失敗します。 フィーチャービューを置き換えると、まずそれが削除され、フィーチャービューにマテリアライズされた特徴量がある間は削除は拒否されます。 どのフィールドが不変か、また推奨される変更方法については「 フィーチャービューの変更」を参照してください。

以下では、transaction_count で定義したフィーチャービューを取り上げ、その不変フィールドであるスライディングウィンドウ期間を拡大します。

YAML

   features:
     transaction_count:
       # The other keys, as in Step 3.
       function:
         aggregation_function:
           count_function:
             input: transaction_id
           time_window:
             sliding:
-              window_duration: 604800s
+              window_duration: 1209600s
               slide_duration: 86400s

Python

 resources.add_feature(
     'transaction_count',
     Feature(
         # The other arguments, as in Step 3.
         function=Function(
             aggregation_function=AggregationFunction(
                 count_function=CountFunction(input='transaction_id'),
                 time_window=TimeWindow(
-                    sliding=SlidingWindow(window_duration='604800s', slide_duration='86400s')
+                    sliding=SlidingWindow(window_duration='1209600s', slide_duration='86400s')
                 ),
             )
         ),
     ),
 )

databricks bundle plan 再作成を報告する:

recreate features.transaction_count

Plan: 1 to add, 0 to change, 1 to delete, 6 unchanged

databricks bundle deploy 確認を求め、そのメッセージには変更が現在の記述のままでは成立しない理由が記載されています:

This action will result in the deletion or recreation of the following Feature Views.
A Feature View that has materialized features in a materialization set cannot be deleted or
recreated. Remove its materialized features from the set in a separate deploy first, then delete
or recreate the Feature View. Deleting or recreating a Feature View that a FeatureSpec serves is
not blocked, and leaves the FeatureSpec stale:
  recreate resources.features.transaction_count

承認すると、セット transactions にまだフィーチャービューの2つの具現化機能が保持されているため、デプロイは失敗します。

Error: cannot recreate resources.features.transaction_count: deleting old id=main.features_demo.transaction_count_7d: Cannot delete feature 'main.features_demo.transaction_count_7d' because it has 2 materialized feature(s). Delete all materialized features first before deleting the feature. (400 BAD_REQUEST)

変更を行うには、 Changing a Feature Viewで説明されている通り、2回デプロイしてください:

  1. フィーチャービューのマテリアライズド フィーチャをセットから削除し、デプロイします。 セットは、Feature View のマテリアライズド特徴をすべて削除する場合と同様に、インプレースで更新されます。

  2. フィーチャービューを変更し、そのマテリアライズド フィーチャをセットに戻し、デプロイしてください。 計画では、変更されたウィンドウの Feature View を再作成し、セットも再作成します。これは、セットにマテリアライズド features を追加すると、そのセットが置き換えられるためです:

    Warn: planning resources.materialization_sets.transactions: the member for feature "main.features_demo.transaction_count_7d" is not in the deployed set, and members cannot be added to an existing set, so the whole set will be recreated: every member is dropped and recomputed
    Warn: planning resources.materialization_sets.transactions: the member for feature "main.features_demo.transaction_count_7d" is not in the deployed set, and members cannot be added to an existing set, so the whole set will be recreated: every member is dropped and recomputed
    recreate features.transaction_count
    recreate materialization_sets.transactions
    
    Plan: 2 to add, 0 to change, 2 to delete, 5 unchanged
    

    承認後、デプロイは機能ビューを再作成し、その後セットを再作成します:

    Recreated features.transaction_count
    Recreated materialization_sets.transactions
    Files: 3 uploaded, 0 deleted
    Resources: 2 created, 0 changed, 2 deleted, 5 unchanged
    

マテリアライゼーション セットの不変フィールドを変更する

セット内の任意のマテリアライズド フィーチャのフィールドを変更すると、編集で触れないマテリアライズド フィーチャを含むマテリアライゼーション セット全体が置き換えられます。 「 物質化集合の変更」を参照してください。

以下では、transactionsで定義された集合を取り上げ、不変であるそのオンライン宛先のtable_name_prefixを変更します。 両方のオンライン マテリアライズド フィーチャには、セットにオンライン送信先が 1 つあるため、接頭辞が付けられます。

YAML

 materialization_sets:
   transactions:
     materialized_features:
       # Batch offline materialized feature of transaction_count.
       - feature_name: ${resources.features.transaction_count.full_name}
         offline_store_config: # As in Step 3.
         cron_schedule_trigger:
           mode: DERIVED
       # Batch online materialized feature of transaction_count.
       - feature_name: ${resources.features.transaction_count.full_name}
         online_store_config:
           online_store_name: ${var.online_store}
           catalog_name: ${var.catalog}
           schema_name: ${var.schema}
-          table_name_prefix: transactions_online
+          table_name_prefix: transactions_online_v2
         cron_schedule_trigger:
           mode: DERIVED
       # Batch offline materialized feature of transaction_count_30d.
       - feature_name: ${resources.features.transaction_count_30d.full_name}
         offline_store_config: # As in Step 3.
         cron_schedule_trigger:
           mode: DERIVED
       # Batch online materialized feature of transaction_count_30d.
       - feature_name: ${resources.features.transaction_count_30d.full_name}
         online_store_config:
           online_store_name: ${var.online_store}
           catalog_name: ${var.catalog}
           schema_name: ${var.schema}
-          table_name_prefix: transactions_online
+          table_name_prefix: transactions_online_v2
         cron_schedule_trigger:
           mode: DERIVED

Python

 resources.add_materialization_set(
     'transactions',
     MaterializationSet(
         materialized_features=[
             # Batch offline materialized feature of transaction_count.
             MaterializedFeature(...),  # As in Step 3.
             # Batch online materialized feature of transaction_count.
             MaterializedFeature(
                 feature_name='${resources.features.transaction_count.full_name}',
                 online_store_config=OnlineStoreConfig(
                     online_store_name='${var.online_store}',
                     catalog_name='${var.catalog}',
                     schema_name='${var.schema}',
-                    table_name_prefix='transactions_online',
+                    table_name_prefix='transactions_online_v2',
                 ),
                 cron_schedule_trigger=CronSchedule(mode=CronScheduleMode.DERIVED),
             ),
             # Batch offline materialized feature of transaction_count_30d.
             MaterializedFeature(...),  # As in Step 3.
             # Batch online materialized feature of transaction_count_30d.
             MaterializedFeature(
                 feature_name='${resources.features.transaction_count_30d.full_name}',
                 online_store_config=OnlineStoreConfig(
                     online_store_name='${var.online_store}',
                     catalog_name='${var.catalog}',
                     schema_name='${var.schema}',
-                    table_name_prefix='transactions_online',
+                    table_name_prefix='transactions_online_v2',
                 ),
                 cron_schedule_trigger=CronSchedule(mode=CronScheduleMode.DERIVED),
             ),
         ]
     ),
 )

databricks bundle plan セットが再作成されるため、その2つのバッチオフライン マテリアライズド特徴量も削除され、再計算されます。

recreate materialization_sets.transactions

Plan: 1 to add, 0 to change, 1 to delete, 6 unchanged

既存のセットにマテリアライズド フィーチャを追加する

すでに展開されているマテリアライゼーションセットにマテライズド特徴を追加すると、そのセットが置き換えられるため、その中のすべてのマテリアル化された特徴が再計算されます。 「 物質化集合の変更」を参照してください。

以下では、transaction_count_30dで Feature View のすべてのマテリアライズド特徴を削除した後のセットtransactionsを取り上げ、Feature View transaction_count_30d のマテリアライズド特徴を追加し直します。

YAML

 materialization_sets:
   transactions:
     materialized_features:
       # Batch offline materialized feature of transaction_count.
       - feature_name: ${resources.features.transaction_count.full_name}
         offline_store_config: # As in Step 3.
         cron_schedule_trigger:
           mode: DERIVED
       # Batch online materialized feature of transaction_count.
       - feature_name: ${resources.features.transaction_count.full_name}
         online_store_config: # As in Step 3.
         cron_schedule_trigger:
           mode: DERIVED
+      # Batch offline materialized feature of transaction_count_30d.
+      - feature_name: ${resources.features.transaction_count_30d.full_name}
+        offline_store_config: # As in Step 3.
+        cron_schedule_trigger:
+          mode: DERIVED
+      # Batch online materialized feature of transaction_count_30d.
+      - feature_name: ${resources.features.transaction_count_30d.full_name}
+        online_store_config: # As in Step 3.
+        cron_schedule_trigger:
+          mode: DERIVED

Python

 resources.add_materialization_set(
     'transactions',
     MaterializationSet(
         materialized_features=[
             # Batch offline materialized feature of transaction_count.
             MaterializedFeature(...),  # As in Step 3.
             # Batch online materialized feature of transaction_count.
             MaterializedFeature(...),  # As in Step 3.
+            # Batch offline materialized feature of transaction_count_30d.
+            MaterializedFeature(...),  # As in Step 3.
+            # Batch online materialized feature of transaction_count_30d.
+            MaterializedFeature(...),  # As in Step 3.
         ]
     ),
 )

databricks bundle plan 追加された各 materialized feature について警告し、再作成が必要であることを報告します:

Warn: planning resources.materialization_sets.transactions: the member for feature "main.features_demo.transaction_count_30d" is not in the deployed set, and members cannot be added to an existing set, so the whole set will be recreated: every member is dropped and recomputed
Warn: planning resources.materialization_sets.transactions: the member for feature "main.features_demo.transaction_count_30d" is not in the deployed set, and members cannot be added to an existing set, so the whole set will be recreated: every member is dropped and recomputed
recreate materialization_sets.transactions

Plan: 1 to add, 0 to change, 1 to delete, 6 unchanged

databricks bundle deploy はセットを再作成する前に確認を求めます:

This action will result in the deletion or recreation of the following materialization sets.
Every materialized feature in a recreated set is dropped and recomputed from scratch, including
the members whose configuration did not change:
  recreate resources.materialization_sets.transactions

フィーチャービューのマテリアライズドフィーチャの一部を削除する

フィーチャービューのマテリアル化された特徴の一部のみで、すべてではないものをマテリアライゼーションセットから削除することは拒否されます。 「 物質化集合の変更」を参照してください。

以下は、ソートゥーウィンドウを持つフィーチャービューamountsについて、ステップ5で定義されたセットamount_sumを取り上げ、そのバッチオフラインのマテリアライズド特徴量のみを削除します。

YAML

 materialization_sets:
   amounts:
     materialized_features:
-      # Batch offline materialized feature.
-      - feature_name: ${resources.features.amount_sum.full_name}
-        offline_store_config: # As in Step 5.
-        cron_schedule_trigger:
-          mode: DERIVED
       # Batch online materialized feature.
       - feature_name: ${resources.features.amount_sum.full_name}
         online_store_config: # As in Step 5.
         cron_schedule_trigger:
           mode: DERIVED
       # Streaming online materialized feature.
       - feature_name: ${resources.features.amount_sum.full_name}
         online_store_config: # As in Step 5.
         streaming_mode:
           mode: STREAMING_MODE_TYPE_RTM

Python

 resources.add_materialization_set(
     'amounts',
     MaterializationSet(
         materialized_features=[
-            # Batch offline materialized feature.
-            MaterializedFeature(...),  # As in Step 5.
             # Batch online materialized feature.
             MaterializedFeature(...),  # As in Step 5.
             # Streaming online materialized feature.
             MaterializedFeature(...),  # As in Step 5.
         ]
     ),
 )

編集はamount_sumのバッチ オフライン マテリアライズド フィーチャーを削除し、その削除により、同じFeature View内の他の2つのマテリアライズド フィーチャーも削除されますが、編集ではそれは求められていません。 配備は却下されます:

Error: cannot update resources.materialization_sets.amounts: updating id=1234567890abcdef1234567890abcdef: Deleting materialized features '11111111111111111111111111111111' of feature 'main.features_demo.amount_sum_3d' also deletes materialized features '22222222222222222222222222222222', '33333333333333333333333333333333', which the request does not delete. Delete the unrequested materialized features as well. (400 INVALID_PARAMETER_VALUE)

ステップ10:片付け

マテリゼーションパイプラインはセットが削除されるまで、設定されたスケジュール通りに実行され続けます。 作業が終わったらバンドルを削除します:

databricks bundle destroy

これにより、バンドルに含まれるすべてのファイルで宣言されたマテリアル化セットやフィーチャービューが削除されます。 マテリアライズド特徴量の背後にあるテーブル、パイプライン、ジョブは、それらを共有する最後のマテリアライズド特徴量がなくなった後、バックグラウンドプロセスによって削除されるため、関連コストの発生が止まるまでに短い遅延があります。 マテリアライズされた機能を削除できるのはユーザーだけなので、バンドルをデプロイした同じIDで databricks bundle destroy 実行してください。 delete_materialized_feature()を参照してください。

マテリアライゼーションコストおよびその帰属方法については、 フィーチャーストアコスト管理を参照してください。

Limitations

  • ストリームはバンドルリソースではありません。 バンドルの外で作成し、名前で参照してください。
  • 物質化特徴を既存の物質化セットに追加すると、そのセットが再作成されるため、その中のすべての物質化特徴が再計算されます。
  • request_sourceやfeature_view_source、またはcustom_udf関数を用いたフィーチャービューは、具現化できません。 「 フィーチャ ビューを具体化する」を参照してください。

次のステップ