共用方式為


使用工作區功能存放區中的功能

注意

本文件涵蓋工作區功能存放區。 Databricks 建議在 Unity 目錄中使用功能工程。 工作區功能存放區未來將會淘汰。

此頁面描述如何在工作區功能存放區中建立和使用功能數據表。

注意

如果您的工作區已啟用 Unity 目錄,則具有主鍵的 Unity 目錄所管理的任何資料表都會自動成為功能數據表,您可以用於模型定型和推斷。 功能數據表會自動提供所有 Unity 目錄功能,例如安全性、譜系、標記和跨工作區存取。 如需在已啟用 Unity 目錄的工作區中使用功能數據表的相關信息,請參閱 Unity 目錄中的功能工程。

如需追蹤特徵譜系和新鮮度的相關信息,請參閱 探索特徵和追蹤特徵譜系

注意

資料庫和特徵數據表名稱只能包含英數位元和底線 (_)。

建立功能數據表的資料庫

建立任何功能數據表之前,您必須先建立 資料庫 來儲存它們。

%sql CREATE DATABASE IF NOT EXISTS <database-name>

功能數據表會儲存為 差異數據表。 當您使用 (Feature Store 用戶端 v0.3.6 和更新版本) 或 create_feature_table (v0.3.5 和以下版本) 建立功能數據表create_table時,您必須指定資料庫名稱。 例如,這個自變數會在資料庫中recommender_system建立名為 customer_features 的 Delta 數據表。

name='recommender_system.customer_features'

當您將功能數據表發佈至在線商店時,預設數據表和資料庫名稱就是您在建立數據表時所指定的數據表;您可以使用 方法指定不同的名稱 publish_table

Databricks 功能存放區 UI 會顯示在線商店中的數據表和資料庫名稱,以及其他元數據。

在 Databricks 功能存放區中建立功能數據表

注意

您也可以將現有的 Delta 數據表 註冊為功能數據表。 請參閱 將現有的 Delta 數據表註冊為功能數據表

建立功能數據表的基本步驟如下:

  1. 撰寫 Python 函式來計算功能。 每個函式的輸出應該是具有唯一主鍵的 Apache Spark DataFrame。 主鍵可以包含一或多個數據行。
  2. 藉由具現化 FeatureStoreClient 和使用 (v0.3.6 和 create_table 更新版本) 或 create_feature_table (v0.3.5 和以下版本) 來建立功能數據表。
  3. 使用 write_table填入功能數據表。

如需下列範例中使用的命令和參數詳細數據,請參閱 功能存放區 Python API 參考

V0.3.6 和更新版本

from databricks.feature_store import feature_table

def compute_customer_features(data):
  ''' Feature computation code returns a DataFrame with 'customer_id' as primary key'''
  pass

# create feature table keyed by customer_id
# take schema from DataFrame output by compute_customer_features
from databricks.feature_store import FeatureStoreClient

customer_features_df = compute_customer_features(df)

fs = FeatureStoreClient()

customer_feature_table = fs.create_table(
  name='recommender_system.customer_features',
  primary_keys='customer_id',
  schema=customer_features_df.schema,
  description='Customer features'
)

# An alternative is to use `create_table` and specify the `df` argument.
# This code automatically saves the features to the underlying Delta table.

# customer_feature_table = fs.create_table(
#  ...
#  df=customer_features_df,
#  ...
# )

# To use a composite key, pass all keys in the create_table call

# customer_feature_table = fs.create_table(
#   ...
#   primary_keys=['customer_id', 'date'],
#   ...
# )

# Use write_table to write data to the feature table
# Overwrite mode does a full refresh of the feature table

fs.write_table(
  name='recommender_system.customer_features',
  df = customer_features_df,
  mode = 'overwrite'
)

V0.3.5 和以下版本

from databricks.feature_store import feature_table

def compute_customer_features(data):
  ''' Feature computation code returns a DataFrame with 'customer_id' as primary key'''
  pass

# create feature table keyed by customer_id
# take schema from DataFrame output by compute_customer_features
from databricks.feature_store import FeatureStoreClient

customer_features_df = compute_customer_features(df)

fs = FeatureStoreClient()

customer_feature_table = fs.create_feature_table(
  name='recommender_system.customer_features',
  keys='customer_id',
  schema=customer_features_df.schema,
  description='Customer features'
)

# An alternative is to use `create_feature_table` and specify the `features_df` argument.
# This code automatically saves the features to the underlying Delta table.

# customer_feature_table = fs.create_feature_table(
#  ...
#  features_df=customer_features_df,
#  ...
# )

# To use a composite key, pass all keys in the create_feature_table call

# customer_feature_table = fs.create_feature_table(
#   ...
#   keys=['customer_id', 'date'],
#   ...
# )

# Use write_table to write data to the feature table
# Overwrite mode does a full refresh of the feature table

fs.write_table(
  name='recommender_system.customer_features',
  df = customer_features_df,
  mode = 'overwrite'
)from databricks.feature_store import feature_table

def compute_customer_features(data):
  ''' Feature computation code returns a DataFrame with 'customer_id' as primary key'''
  pass

# create feature table keyed by customer_id
# take schema from DataFrame output by compute_customer_features
from databricks.feature_store import FeatureStoreClient

customer_features_df = compute_customer_features(df)

fs = FeatureStoreClient()

customer_feature_table = fs.create_feature_table(
  name='recommender_system.customer_features',
  keys='customer_id',
  schema=customer_features_df.schema,
  description='Customer features'
)

# An alternative is to use `create_feature_table` and specify the `features_df` argument.
# This code automatically saves the features to the underlying Delta table.

# customer_feature_table = fs.create_feature_table(
#  ...
#  features_df=customer_features_df,
#  ...
# )

# To use a composite key, pass all keys in the create_feature_table call

# customer_feature_table = fs.create_feature_table(
#   ...
#   keys=['customer_id', 'date'],
#   ...
# )

# Use write_table to write data to the feature table
# Overwrite mode does a full refresh of the feature table

fs.write_table(
  name='recommender_system.customer_features',
  df = customer_features_df,
  mode = 'overwrite'
)

將現有的 Delta 數據表註冊為功能數據表

使用 v0.3.8 和更新版本,您可以將現有的 Delta 數據表 註冊為功能數據表。 Delta 數據表必須存在於中繼存放區中。

fs.register_table(
  delta_table='recommender.customer_features',
  primary_keys='customer_id',
  description='Customer features'
)

控制功能數據表的存取

請參閱 控制功能數據表的存取權。

更新功能數據表

您可以藉由新增功能,或修改以主鍵為基礎的特定數據列,來更新功能數據表。

無法更新下列功能資料表元資料:

  • 主要金鑰
  • 分割區索引鍵
  • 現有功能的名稱或類型

將新功能新增至現有的功能數據表

您可以使用下列兩種方式之一,將新功能新增至現有的功能資料表:

  • 更新現有的功能計算函式,並使用傳回的 DataFrame 執行 write_table 。 這會更新功能數據表架構,並根據主鍵合併新的特徵值。
  • 建立新的特徵計算函式來計算新的特徵值。 這個新計算函式傳回的 DataFrame 必須包含功能數據表的主鍵和數據分割索引鍵(如果已定義)。 使用 DataFrame 執行 write_table ,以使用相同的主鍵,將新功能寫入現有的功能數據表。

僅更新功能數據表中的特定數據列

在中使用mode = "merge"write_table。 在呼叫中 write_table 傳送之 DataFrame 中,主鍵不存在的數據列保持不變。

fs.write_table(
  name='recommender.customer_features',
  df = customer_features_df,
  mode = 'merge'
)

排程作業以更新功能數據表

為了確保功能數據表中的功能一律具有最新的值,Databricks 建議您 建立一個作業 來執行筆記本,定期更新功能數據表,例如每天。 如果您已建立非排程工作,您可以將它 轉換成排程工作 ,以確保功能值一律為最新狀態。

更新功能數據表的程式代碼會使用 mode='merge',如下列範例所示。

fs = FeatureStoreClient()

customer_features_df = compute_customer_features(data)

fs.write_table(
  df=customer_features_df,
  name='recommender_system.customer_features',
  mode='merge'
)

儲存每日功能的過去值

使用複合主鍵定義功能數據表。 在主鍵中包含日期。 例如,針對功能數據表 store_purchases,您可以使用複合主鍵 (dateuser_id) 和數據分割索引鍵 date ,以有效率地讀取。

fs.create_table(
  name='recommender_system.customer_features',
  primary_keys=['date', 'customer_id'],
  partition_columns=['date'],
  schema=customer_features_df.schema,
  description='Customer features'
)

然後,您可以建立程式代碼,以從功能數據表篩選 date 讀取到感興趣的時間週期。

您也可以使用 timestamp_keys 自變數,將數據行指定date為時間戳索引鍵,藉以建立時間序列功能數據表

fs.create_table(
  name='recommender_system.customer_features',
  primary_keys=['date', 'customer_id'],
  timestamp_keys=['date'],
  schema=customer_features_df.schema,
  description='Customer timeseries features'
)

當您使用 create_training_setscore_batch時,這會啟用時間點查閱。 系統會使用 timestamp_lookup_key 您指定的 來執行時間戳聯結的 as-of timestamp join。

若要讓功能數據表保持最新狀態,請設定定期排程的作業,以將功能寫入功能,或將新的特徵值串流至功能數據表。

建立串流功能計算管線以更新功能

若要建立串流功能計算管線,請將串流 DataFrame 當做自變數傳遞至 write_table。 此方法會傳回 StreamingQuery 物件。

def compute_additional_customer_features(data):
  ''' Returns Streaming DataFrame
  '''
  pass  # not shown

customer_transactions = spark.readStream.load("dbfs:/events/customer_transactions")
stream_df = compute_additional_customer_features(customer_transactions)

fs.write_table(
  df=stream_df,
  name='recommender_system.customer_features',
  mode='merge'
)

從功能數據表讀取

用來 read_table 讀取特徵值。

fs = feature_store.FeatureStoreClient()
customer_features_df = fs.read_table(
  name='recommender.customer_features',
)

搜尋和瀏覽功能數據表

使用功能存放區 UI 來搜尋或瀏覽功能數據表。

  1. 在提要欄位中,選取 [機器學習 > 功能存放區] 以顯示功能存放區 UI。

  2. 在搜尋方塊中,輸入功能數據表、功能或用於特徵計算之數據源的所有或部分名稱。 您也可以輸入標記的所有或部分 索引鍵或值。 搜尋文字不區分大小寫。

    功能搜尋範例

取得功能數據表元數據

取得功能數據表元數據的 API 取決於您所使用的 Databricks 執行時間版本。 使用 v0.3.6 和更新版本, 請使用 get_table。 使用 v0.3.5 和以下版本,請使用 get_feature_table

# this example works with v0.3.6 and above
# for v0.3.5, use `get_feature_table`
from databricks.feature_store import FeatureStoreClient
fs = FeatureStoreClient()
fs.get_table("feature_store_example.user_feature_table")

使用功能數據表標籤

卷標是索引鍵/值組,您可以建立及用來 搜尋功能數據表。 您可以使用功能存放區 UI 或 功能存放區 Python API 來建立、編輯和刪除標記。

在UI中使用功能數據表標籤

使用功能存放區 UI 來搜尋或瀏覽功能數據表。 若要存取 UI,請在提要欄中選取 [機器學習 > 功能存放區]。

使用功能存放區 UI 新增標記

  1. 如果尚未開啟,請按兩下 標記圖示 它。 標記數據表隨即出現。

    標記數據表

  2. 按兩下 [ 名稱 ] 和 [值 ] 字段,然後輸入標記的索引鍵和值。

  3. 按一下新增

使用功能存放區 UI 編輯或刪除標記

若要編輯或刪除現有的標記,請使用 [動作] 資料行中的圖示。

標籤動作

使用功能存放區 Python API 處理功能數據表標籤

在執行 v0.4.1 和更新版本之叢集上,您可以使用功能存放區 Python API 來建立、編輯和刪除標籤

需求

Feature Store 用戶端 v0.4.1 和更新版本

使用Feature Store Python API 建立具有標籤的功能數據表

from databricks.feature_store import FeatureStoreClient
fs = FeatureStoreClient()

customer_feature_table = fs.create_table(
  ...
  tags={"tag_key_1": "tag_value_1", "tag_key_2": "tag_value_2", ...},
  ...
)

使用功能存放區 Python API 新增、更新和刪除標記

from databricks.feature_store import FeatureStoreClient
fs = FeatureStoreClient()

# Upsert a tag
fs.set_feature_table_tag(table_name="my_table", key="quality", value="gold")

# Delete a tag
fs.delete_feature_table_tag(table_name="my_table", key="quality")

更新功能數據表的數據源

功能存放區會自動追蹤用來計算功能的數據源。 您也可以使用 功能存放區 Python API 手動更新數據源。

需求

Feature Store 用戶端 v0.5.0 和更新版本

使用功能存放區 Python API 新增數據源

以下是一些範例命令。 如需詳細資訊,請參閱 API 檔

from databricks.feature_store import FeatureStoreClient
fs = FeatureStoreClient()

# Use `source_type="table"` to add a table in the metastore as data source.
fs.add_data_sources(feature_table_name="clicks", data_sources="user_info.clicks", source_type="table")

# Use `source_type="path"` to add a data source in path format.
fs.add_data_sources(feature_table_name="user_metrics", data_sources="dbfs:/FileStore/user_metrics.json", source_type="path")

# Use `source_type="custom"` if the source is not a table or a path.
fs.add_data_sources(feature_table_name="user_metrics", data_sources="user_metrics.txt", source_type="custom")

使用功能存放區 Python API 刪除數據源

如需詳細資訊,請參閱 API 檔

注意

下列命令會刪除符合來源名稱的所有類型數據源 (“table”、“path” 和 “custom”。

from databricks.feature_store import FeatureStoreClient
fs = FeatureStoreClient()
fs.delete_data_sources(feature_table_name="clicks", sources_names="user_info.clicks")

刪除功能數據表

您可以使用功能存放區 UI 或 功能存放區 Python API 來刪除功能資料表。

注意

  • 刪除功能數據表可能會導致上游生產者和下游取用者發生非預期的失敗(模型、端點和排程作業)。 您必須使用雲端提供者刪除已發佈的在線商店。
  • 當您使用 API 刪除功能數據表時,也會卸除基礎 Delta 數據表。 當您從 UI 刪除功能數據表時,必須個別卸除基礎 Delta 數據表。

使用UI刪除功能資料表

  1. 在功能數據表頁面上,按下 按鈕向下 功能數據表名稱右邊的 ,然後選取 [ 刪除]。 如果您沒有功能數據表的 CAN MANAGE 許可權,則不會看到此選項。

    從下拉選單中選取 [刪除]

  2. 在 [刪除功能數據表] 對話框中,按兩下 [ 刪除 ] 以確認。

  3. 如果您也想要 卸除基礎 Delta 數據表,請在筆記本中執行下列命令。

    %sql DROP TABLE IF EXISTS <feature-table-name>;
    

使用功能存放區 Python API 刪除功能數據表

透過Feature Store用戶端 v0.4.1和更新版本,您可以使用 drop_table 來刪除功能資料表。 當您使用 drop_table刪除數據表時,也會卸除基礎 Delta 數據表。

fs.drop_table(
  name='recommender_system.customer_features'
)