實體化特徵檢視

這很重要

這項功能目前處於 公開預覽版。 工作區管理員可以從 「預覽 」頁面控制對此功能的存取。 請參閱 管理 Azure Databricks 預覽。

在建立好功能檢視定義(儲存在 Unity 目錄)後,你可以利用這些特徵定義從來源資料表產生功能資料。 這個過程稱為具象化你的特徵。 Azure Databricks 會建立並管理 Lakeflow 管線,將資料填入 Unity Catalog 中的資料表,供模型訓練、批次評分或線上推論服務使用。

關於提供特徵檢視的資訊,請參見 「服務功能檢視」。

需求

  • 特徵必須以特徵檢視(Feature View)形式建立並儲存在 Unity 目錄中。
  • 關於版本需求,請參見 需求
  • ColumnSelection 功能可以實體化到線上商店。 參見 列選擇實體化
  • RequestSource 特徵無法具體化,因為它們代表推論時提供的資料。

許可

具體化會與此功能上的 MANAGEREAD FEATURE Unity Catalog 權限互動。 完整權限描述請參見 閱讀功能

  • 實作某項功能需要 MANAGE 呼叫 materialize_featuresdelete_materialized_feature 建立並管理後備的 Lakeflow 管線和 Unity 目錄資料表,因此這是一項管理操作。 您必須對該功能具有 MANAGE,並具備 READ FEATURE,才能讀取已具體化的功能定義。
  • 讀取物質化資料需要 READ FEATURE READ FEATURE 啟用於該功能後,會授與您對支援該功能的離線與線上資料表的存取權限,讓您可以使用實體化資料進行模型訓練與推論。 list_materialized_features 也需要 READ FEATURE

如同任何 Unity Catalog 物件一樣,您也需要在父 catalog 上具備 USE CATALOG,並在父 schema 上具備 USE SCHEMA。 在結構描述或目錄上授與的 READ FEATUREMANAGE,適用於其中包含的所有目前及未來項目。

API 資料結構

OfflineStoreConfig

離線商店的配置,用於寫入具象化特徵。 當 materialize_features 呼叫 時,功能庫後端會建立使用此前綴的資料表。 根據具體化排程,每次管道執行都會將最新的特徵值實體化到表格中。

OfflineStoreConfig(
    catalog_name: str,        # Catalog name for the offline table where materialized features will be stored
    schema_name: str,         # Schema name for the offline table
    table_name_prefix: str    # Table name prefix for the offline table. The pipeline may create multiple tables with this prefix, each updated at different cadences
)
from databricks.feature_engineering.entities import OfflineStoreConfig

offline_store = OfflineStoreConfig(
    catalog_name="main",
    schema_name="feature_store",
    table_name_prefix="customer_features"
)

OnlineStoreConfig

線上商店的配置,儲存模型服務所使用的功能。 Materialization 會建立帶有 catalog.schema.table_name_prefix的 Delta 資料表,並將這些資料表串流到同名的線上功能商店。

from databricks.feature_engineering.entities import OnlineStoreConfig

online_store = OnlineStoreConfig(
    catalog_name="main",
    schema_name="feature_store",
    table_name_prefix="customer_features_serving",
    online_store_name="customer_features_store"
)

MaterializedFeature

表示已實體化的特徵檢視,也就是說,在 Unity Catalog 中已有可用的預先計算好的表示形式。 離線表格和線上表格有各自的具體化功能。 通常,使用者不會直接實例化 a MaterializedFeature

API 函式呼叫

materialize_features()

將功能視圖清單具體化為離線的 Delta 表或線上功能商店。 功能必須先在 Unity 目錄中註冊,才能呼叫此函式(例如使用 create_featureregister_feature)。 本地建造且未註冊的特徵將無法運作。

FeatureEngineeringClient.materialize_features(
    features: List[Feature],                                               # List of Feature Views to materialize
    offline_config: Optional[OfflineStoreConfig] = None,                   # Offline store config (aggregation features only)
    online_config: Optional[OnlineStoreConfig] = None,                     # Online store config
    trigger: Union[CronSchedule, TableTrigger, StreamingMode],              # Materialization trigger
) -> List[MaterializedFeature]:

此方法回傳一個實體化特徵清單,其中包含有關特徵值更新時間的中繼資料,以及 Unity Catalog 中特徵實體化的表格。

若同時提供 a OnlineStoreConfig 與 a OfflineStoreConfig ,則每個功能會回傳兩個具體化功能,分別對應一種商店類型。

trigger 參數控制物質化管線的運行時機。

  • CronSchedule:以固定時程運行。 為批次彙總功能所必需(AggregationFunction 來自 DeltaTableSource)。
  • TableTrigger:當上游 Delta 資料表接收到提交時執行。 需要由 ColumnSelection 支援的 DeltaTableSource 功能。
  • StreamingMode: 以連續串流管線運行。 這是由 StreamSource 支援的功能所必需。

你無法在單一 materialize_features 呼叫中混用需要不同觸發類型的功能。 改為分開來電。

實體化到線下商店

from databricks.feature_engineering import FeatureEngineeringClient
from databricks.feature_engineering.entities import (
    CronSchedule, OfflineStoreConfig,
)

fe = FeatureEngineeringClient()

materialized = fe.materialize_features(
    features=features,
    offline_config=OfflineStoreConfig(
        catalog_name="main",
        schema_name="feature_store",
        table_name_prefix="customer_features"
    ),
    trigger=CronSchedule(
        quartz_cron_expression="0 0 * * * ?",  # Hourly
        timezone_id="UTC",
    ),
)

實體化到線上商店

Note

要將聚合功能部署於線上商店,您也必須將其部署於實體商店。 兩者 offline_configonline_config 均為必需。 必須參考現有的線上特徵庫 online_store_name。 關於建立 Feature Store 的說明,請參見 Databricks 線上功能庫

ColumnSelection 功能不需要 OfflineStoreConfig。 參見 列選擇實體化

from databricks.feature_engineering import FeatureEngineeringClient
from databricks.feature_engineering.entities import (
    CronSchedule, OfflineStoreConfig, OnlineStoreConfig,
)

fe = FeatureEngineeringClient()

materialized = fe.materialize_features(
    features=features,
    offline_config=OfflineStoreConfig(
        catalog_name="main",
        schema_name="feature_store",
        table_name_prefix="customer_features"
    ),
    online_config=OnlineStoreConfig(
        catalog_name="main",
        schema_name="feature_store",
        table_name_prefix="customer_features_serving",
        online_store_name="customer_features_store"
    ),
    trigger=CronSchedule(
        quartz_cron_expression="0 0 * * * ?",  # Hourly
        timezone_id="UTC",
    ),
)

Materialize 串流功能

串流功能只能實體化到線上商店;該 offline_config 參數不被支援。 離線實體化不被支援,因為串流功能需要即時流程以確保亞秒新鮮度。 離線訓練或評估時,特徵工程客戶端會根據每個評估的資料點重新計算特徵值。

串流功能不能與批 materialize_features 次功能在同一通話中混合使用。

from databricks.feature_engineering import FeatureEngineeringClient
from databricks.feature_engineering.entities import (
    OnlineStoreConfig, StreamingMode,
)

fe = FeatureEngineeringClient()

materialized = fe.materialize_features(
    features=[streaming_feature],
    online_config=OnlineStoreConfig(
        catalog_name="my_catalog",
        schema_name="my_schema",
        table_name_prefix="streaming_features_serving",
        online_store_name="feature_store_online"
    ),
    trigger=StreamingMode(),
)

list_materialized_features()

回傳使用者 Unity Catalog 中繼資料庫中所有具象化特徵的列表。

預設情況下,最多回傳 100 個特徵。 你可以用參數 max_results 來改變這個限制。

若要依特徵名稱過濾回傳的物質化特徵,請使用可選 feature_name 參數。

FeatureEngineeringClient.list_materialized_features(
    feature_name: Optional[str] = None,     # Optional feature name to filter by
    max_results: int = 100,                 # Maximum number of features to be returned
) -> List[MaterializedFeature]:

delete_materialized_feature()

在刪除實體化特徵前,移除或更新任何參考該特徵的模型或特徵規格。

刪除一個實體化的特徵。 要通過的特徵取決於特徵類型:

  • 聚合功能:跳過離線實體化功能。 如果同一功能有線上實體化功能,兩者都會被刪除。
  • ColumnSelection 功能:跳過線上實體化功能。 ColumnSelection 特徵僅實體化到線上商店(參見 ColumnSelection 實體化),因此沒有配對離線功能。

作為物質化的一部分,特徵會依資料來源與聚合視窗分組以提高效率。 ColumnSelection 特徵沒有彙整視窗,因此僅依資料來源分組。 物化管線、離線資料表和線上資料表只有在所有分組特徵都被刪除後才會被刪除。 當群組中最後一個實體化的特徵被刪除時,特徵存放區會將相關資源排入由背景程序執行的自動清理作業。 參見 背景資源清理

要清理物質化特徵,請查看與物質化特徵相關的表格。 表格中的每個功能(每欄一個)必須先刪除,才能清理計算與 Delta 資料表資源。

list_materialized_features() 來取得 materialized_feature 論點。

FeatureEngineeringClient.delete_materialized_feature(
    materialized_feature: MaterializedFeature,  # Required: The materialized feature to delete
) -> None
from databricks.feature_engineering import FeatureEngineeringClient
from databricks.feature_engineering.entities import ColumnSelection

fe = FeatureEngineeringClient()

feature_names = [
    "main.feature_store.amount_sum_sliding_7d_1d",
    "main.feature_store.amount_sum_sliding_30d_1d",
    "main.feature_store.transaction_count_sliding_7d_1d",
    "main.feature_store.latest_transaction_amount",
    "main.feature_store.latest_user_tier",
]

for name in feature_names:
    feature = fe.get_feature(full_name=name)
    for mf in fe.list_materialized_features(feature_name=name):
        if isinstance(feature.function, ColumnSelection):
            # ColumnSelection features only have online materializations. Delete the online materialized feature directly.
            fe.delete_materialized_feature(materialized_feature=mf)
        elif not mf.is_online:
            # Aggregation features have both offline and online materializations. Delete the offline materialized feature to delete both.
            fe.delete_materialized_feature(materialized_feature=mf)
        # Online materialized aggregation features cannot be deleted directly. They are deleted via their paired offline materialized features.

柱選擇實現

ColumnSelection 功能是選擇每個實體鍵的單一欄位最新值,不需匯總。 它們只能在網路商店中實現。 對於離線使用情境(訓練與批次推論), ColumnSelection 特徵會在查詢時直接從來源資料擷取,因此不需要離線實體化。

物質化行為

  • 資料管線會將每個實體鍵對應的最新的列寫入即時資料表,且沒有聚合窗口。
  • 線上具現化會以最新的每個實體鍵值填滿線上表格。

範例

from databricks.feature_engineering import FeatureEngineeringClient
from databricks.feature_engineering.entities import (
    DeltaTableSource, Feature, ColumnSelection, TableTrigger, OnlineStoreConfig,
)

fe = FeatureEngineeringClient()

delta_source = DeltaTableSource(
    catalog_name="catalog",
    schema_name="schema",
    table_name="transactions",
)

amount_feature = Feature(
    source=delta_source,
    function=ColumnSelection("amount"),
    entity=["user_id"],
    timeseries_column="transaction_time",
    name="latest_transaction_amount",
)

# Register before materializing
amount_feature = fe.register_feature(
    feature=amount_feature,
    catalog_name="catalog",
    schema_name="schema",
)

mfs = fe.materialize_features(
    features=[amount_feature],
    online_config=OnlineStoreConfig(
        catalog_name="catalog",
        schema_name="feats_online",
        table_name_prefix="txn_",
        online_store_name="lb_usw2"
    ),
    trigger=TableTrigger(),
)

ColumnSelection 特性使用 TableTrigger,當來源 Delta 資料表收到新的提交時,該管線便會運行。 不需要 offline_config ,因為 ColumnSelection 功能是直接從來源讀取,用於離線使用(訓練和批次推論)。

Note

RequestSource 特徵無法實體化,因為它們代表呼叫者在推論時提供的資料(或在訓練時從標記資料框中擷取)。 沒有來源表可供閱讀。 這些值僅存在於請求有效載荷或訓練資料框架中。

背景資源清理

當你刪除實體化的特徵時,Databricks 會立即移除該特徵的元資料。 相關的基礎設施(資料表、管線和作業)會由背景程序以非同步方式清理。

由於多個實體化特徵可能共享相同的資料表和管線,這些共享資源不會被移除,直到所有參考它們的實體化特徵都被刪除。 當共用同一組資料表的最後一個實體化特徵被刪除時,背景程序會自動刪除下列資源:

  • 離線的 Delta 表格包含具體化特徵資料
  • 如果這些功能能實體化到線上商店,線上表格
  • 物質化管線
  • 協調作業

這個背景流程會使用由 Databricks 管理的系統服務主體,代表你執行這些清理動作,包括刪除工作區中的資料表、管線和工作。 你不需要採取任何行動。 清理工作完全由功能商店負責。

Note

在刪除群組中最後一個實體化特徵與移除相關資料表及其他資源之間,可能會有短暫的延遲。

查看實體化狀態

若要在 Databricks UI 中檢視特徵檢視的實體化狀態(包括對實體化錯誤進行偵錯),請參閱 在 Unity Catalog 中探索特徵檢視

局限性

批次功能

  • 批次物質化管線以無伺服器 Lakeflow 管線形式運作。
  • 批次滑動窗口特性無法具體化。 由於其時間正確性極高,離線訓練或批次推斷的滾動視窗功能會即時為每個資料點產生。
  • ColumnSelection 功能只能在線上商店實現。
  • RequestSource 特徵無法具體化。
  • 具現化特性只能在其建立的工作區中才能被刪除。
  • 對於實體化聚合功能,線上實體化功能無法直接刪除。 刪除配對離線物質化功能,變更會同時傳達到兩者。
  • 對於 2026 年 4 月 20 日之前建立的實體化聚合特徵,物質化管線會持續產生新的特徵值,直到管線中所有實體化特徵被刪除,觸發資源清理。 若要建立能支援逐個功能刪除的更新管線,請刪除並重新實體化該功能。
  • 對於實體化 ColumnSelection 特徵,物質化管線會持續產生新的特徵值,直到管線中所有實體化特徵被刪除,這才會觸發資源清理。

串流功能

  • 串流功能只能在線上商店實現。 不需要進行離線實體化,因為用於訓練時的串流特徵在設計上,就是要針對每個資料點根據歷史事件重新計算,以達到毫秒等級的精確度。
  • 串流功能不能在單一 materialize_features 通話中與批次功能混合使用。
  • compute_features 不支援串流功能。
  • 工作區必須位於支援 Lakebase 實例的區域。
  • 僅支援 JSON 序列化的 Kafka 訊息。 訊息結構必須直接以 JSON 架構格式提供。 在預覽期間,尚未正式支援結構描述登錄(Confluent、Glue);但如果您直接提供結構描述,管線便可從受結構描述登錄管理的主題讀取資料。
  • RollingWindow 支援串流聚合功能。 TumblingWindow SlidingWindow並應與批次特徵一起使用。
  • 串流功能僅支援 CountAvgSumStddevPopMaxMinLast 彙總函式。
  • 串流來源的欄位選擇功能無法處理順序不符的訊息。 即使時間序列欄位值早於先前接收的事件,仍會顯示 Kafka 串流中的最新事件。
  • 串流管線每週重啟兩次。 每次重啟都可能導致處理延遲和啟動時間長達 1 分鐘。 不含重新啟動時,p99 新鮮度為 200 毫秒。
  • 不支援實體化的功能回填。 當一個特徵被物質化時,它會從該點開始計算。 線上商店中新建立的彙總資料,在其時間範圍結束前都會不準確。
  • 僅支援 Databricks Online 功能商店
  • Unity Catalog 中只支援在你自己雲端物件儲存中建立的標準目錄。 預設 儲存 空間中建立的目錄無法使用。
  • 串流實體化管線以無伺服器 Lakeflow 管線形式運作。
  • 僅限企業級工作空間。