這很重要
這項功能目前處於 公開預覽版。 工作區管理員可以從 「預覽 」頁面控制對此功能的存取。 請參閱 管理 Azure Databricks 預覽。
您可以透過兩種方式在線上提供特徵視圖:
- 模型服務:部署一個在特徵檢視上訓練過的模型。 端點會利用模型記錄時所追蹤的沿襲關係,自動查詢或計算特徵值。 利用此資料來提供模型預測。
-
功能服務:部署可直接參考功能視圖的
FeatureSpec,無需模型。 端點會回傳所請求的功能輸出。 當應用程式需要特徵值而非模型預測時,請使用此方法。
兩種方式都支援預先計算及隨選功能。 以表格和串流作為後端的葉特徵必須具有 受支援的線上實體化。
RequestSource 從請求中提供數值,並 CustomUDF 按需計算功能。 對於具有 FeatureViewSource 的 CustomUDF,在提供服務時,系統會自動解析上游相依性並評估特徵相依圖。
在 Unity 目錄中註冊所請求的功能及其所有可傳遞的上游功能。 只將由資料表和串流支援的葉節點實體化,不要將請求時或衍生的 CustomUDF 特徵實體化。
許可
要提供一個功能,建立模型服務端點的主體必須在 Unity 目錄表中擁有 SELECT 支持該具象化功能的資料表。 線上查詢會直接從實體化的資料表讀取,因此資料表層 SELECT 級才是賦予服務存取權限的。 關於特權描述,請參見 SELECT。
因為實體化資料表可包含多個特徵,對其授予 SELECT 就會取得該資料表中所有特徵的存取權,而不僅是你打算提供的那個特徵。 在授予服務存取權之前,請確認所有共享資料表的功能都能與主體共享。 為了限制暴露風險,請將敏感特徵分開實體化。
若要在不手動解析資料表的情況下授予此存取權限,請使用 FeatureEngineeringClient.grant_feature_serving_access。 給定模型或特徵規格後,它會將每個特徵解析為其對應的線上資料表,將這些資料表上的 SELECT(以及 USE CATALOG 和 USE SCHEMA)授予您指定的主體,並傳回一份報告,列出每個資料表以及該授權會開放的其他特徵。 傳入 dry_run=True 以在核准前預覽報告。
from databricks.feature_engineering import FeatureEngineeringClient
fe = FeatureEngineeringClient()
# Preview the tables and the features each grant would expose.
report = fe.grant_feature_serving_access(
grant_to=["serving-principal@example.com"],
model_uri="models:/main.ecommerce.fraud_model/1",
dry_run=True,
)
print(report)
# Grant SELECT on the resolved online tables.
fe.grant_feature_serving_access(
grant_to=["serving-principal@example.com"],
model_uri="models:/main.ecommerce.fraud_model/1",
)
部署一個服務端點模型
使用現有的服務端點模型,或使用 Databricks SDK 建立新的端點。 該模型必須在 Unity 目錄中註冊。
如果 CustomUDF 使用會匯入 Python 套件的使用者自訂函式(UDF),則在呼叫 extra_pip_requirements 時,請透過 log_model 明確傳遞這些套件。 請參見 自訂 UDF 相依關係。
以下程式碼說明如何建立新的服務端點模型。 欲了解更多資訊,請參閱 建立自訂模型服務端點。
from databricks.sdk import WorkspaceClient
from databricks.sdk.service.serving import EndpointCoreConfigInput, ServedEntityInput
w = WorkspaceClient()
endpoint_name = "fraud-detection-endpoint"
model_name = "main.ecommerce.fraud_model"
w.serving_endpoints.create(
name=endpoint_name,
config=EndpointCoreConfigInput(
name=endpoint_name,
served_entities=[
ServedEntityInput(
entity_name=model_name,
entity_version=1,
max_provisioned_concurrency=4,
min_provisioned_concurrency=0,
)
],
),
)
查詢端點
from databricks.sdk import WorkspaceClient
w = WorkspaceClient()
response = w.serving_endpoints.query(
name="fraud-detection-endpoint",
dataframe_records=[
{"user_id": "user_123", "transaction_time": "2026-03-01T12:00:00"},
],
)
查詢具備 RequestSource 功能的端點
若模型已使用 RequestSource 特徵進行訓練,則請求的 payload 必須包含所有 RequestSource 欄位。 這些欄位是在log_model的過程中加入到 MLflow 模型簽章中的,因此端點的 API 架構反映了所需的請求欄位。
response = w.serving_endpoints.query(
name="fraud-detection-endpoint",
dataframe_records=[
{
"user_id": "user_123",
"transaction_time": "2026-03-01T12:00:00",
"transaction_amount": 275.30, # RequestSource column
"vendor_id": "v_42", # RequestSource column (also used as entity key)
},
],
)
實體鍵用於從線上商店查詢由表格支持的功能。
ColumnSelection功能可以將 RequestSource值傳遞給模型。
CustomUDF特徵會在模型使用結果前轉換其請求輸入。
您也可以使用 curl:
curl -X POST "https://<workspace>.cloud.databricks.com/serving-endpoints/<endpoint>/invocations" \
-H "Authorization: Bearer $DATABRICKS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"dataframe_records": [
{
"user_id": "user_123",
"transaction_time": "2026-03-01T12:00:00",
"transaction_amount": 275.30,
"vendor_id": "v_42"
}
]
}'
提供功能 FeatureSpec
若要在沒有模型的情況下提供 Feature Views,請建立一個 FeatureSpec 來參照這些特徵,並將其部署至 Feature Serving 端點。 端點只會回傳規格中要求的功能輸出。它會自動計算中間值,除非你同時請求那些功能,否則不會回傳。
在建立端點之前,先在 Unity Catalog 註冊所有功能,並為表格和串流背景的葉節點建立支援的線上實體化。
Note
包含特徵視圖的 A FeatureSpec 也不能包含 FeatureLookup 或 FeatureFunction 定義。 特徵檢視不能與這些定義在同一個 FeatureSpec 中混合使用。
from databricks.feature_engineering import FeatureEngineeringClient
from databricks.feature_engineering.entities.feature_serving_endpoint import (
EndpointCoreConfig,
ServedEntity,
)
fe = FeatureEngineeringClient()
# 1. Retrieve a registered Feature View
agg_feature = fe.get_feature(full_name="main.ecommerce.amount_sum_sliding_7d_1d")
# 2. Create a FeatureSpec that includes the Feature View
feature_spec_name = "main.ecommerce.transaction_feature_spec"
fe.create_feature_spec(name=feature_spec_name, features=[agg_feature])
# 3. Deploy a Feature Serving endpoint backed by the FeatureSpec
fe.create_feature_serving_endpoint(
name="transaction-features",
config=EndpointCoreConfig(
served_entities=ServedEntity(
feature_spec_name=feature_spec_name,
workload_size="Small",
scale_to_zero_enabled=True,
)
),
)
使用用於查找具體化特徵的實體鍵來查詢端點:
from databricks.sdk import WorkspaceClient
w = WorkspaceClient()
response = w.serving_endpoints.query(
name="transaction-features",
dataframe_records=[{"user_id": "user_123"}],
)
欲了解更多關於特徵服務端點的資訊,請參見特徵服務端點。
提供 CustomUDF 功能及其相依性
此範例適用於 CustomUDF 和 FeatureViewSource 範例中定義的交易與保證金功能。 使用 databricks-feature-engineering 版本 0.18.0 或以上來使用這些功能檢視 API。
-
log_transaction_amount使用 NumPy 將main.ecommerce.log_amount_udf套用至請求的transaction_amount值。 -
margin使用FeatureViewSource根據revenue_sum_7d和cost_sum_7d計算邊際。 這些上游特徵使用customer_id作為其實體鍵,並使用event_time作為其時間序列欄位。
在建立規格前,請先註冊這兩個輸出特徵以及每個上游特徵。在這個例子中,線上實體化只會處理 revenue_sum_7d 和 cost_sum_7d 這兩個葉節點。 Serving 會查詢它們的值,並對每個請求進行 margin 計算。
擷取註冊 Feature 物件並傳遞給 create_feature_spec。 即使 NumPy 已列在 UDF 的 ENVIRONMENT 子句中,仍應宣告為線上執行:
from databricks.feature_engineering import FeatureEngineeringClient
fe = FeatureEngineeringClient()
log_transaction_amount = fe.get_feature(
full_name="main.ecommerce.log_transaction_amount"
)
margin = fe.get_feature(full_name="main.ecommerce.margin")
fe.create_feature_spec(
name="main.ecommerce.derived_transaction_features",
features=[log_transaction_amount, margin],
extra_pip_requirements=["numpy==1.26.4"],
)
依照上述端點建立步驟,使用 feature_spec_name="main.ecommerce.derived_transaction_features" 和端點名稱(例如 derived-transaction-features)。 端點準備好後,提供查詢鍵與請求時間值:
from databricks.sdk import WorkspaceClient
w = WorkspaceClient()
response = w.serving_endpoints.query(
name="derived-transaction-features",
dataframe_records=[
{"customer_id": "customer_123", "transaction_amount": 99.0},
],
)
回應僅包含所要求的 log_transaction_amount 和 margin 輸出,不包含中間的營收與成本數值。 如需了解相依性格式、Wheel 權限及安裝疑難排解,請參閱 新增 Python 相依性。