提供特征视图

重要

此功能目前以公共预览版提供。 工作区管理员可以从 预览 页控制对此功能的访问。 请参阅 Manage Azure Databricks 预览版

您可以通过两种方式在线提供功能视图:

  • 模型服务:部署在功能视图上训练的模型。 该终结点会利用在记录模型时跟踪的世系,自动从联机存储中查找特征值。 使用它来提供模型预测。
  • 功能服务:部署 FeatureSpec 直接引用功能视图,而不部署模型。 终结点返回查找的功能值。 当应用程序需要特征值而不是模型预测时,请使用此功能。

这两种方法从在线商店读取预计算的功能值,因此必须首先 将这些功能具体化到在线商店

使用 Databricks 特征进行训练的模型会自动追踪其训练所用特征的世系。 模型作为模型服务终结点进行部署时,这些模型使用 Unity Catalog 从联机存储中查找特征。

权限

要提供特征,创建模型服务终结点的主体必须对支撑该具体化特征的 Unity Catalog 表具有 SELECT 权限。 联机查找直接从具体化表中读取,因此表级别的 SELECT 即授予了服务访问权限。 有关特权说明,请参阅 SELECT

由于物化表可以包含多个特征,因此,对其授予 SELECT 权限将使你获得对该表中所有特征的访问权限,而不仅仅是你打算提供的那个特征。 在授予服务访问权限之前,请确认共享该表的每个功能都可以与该主体共享。 若要限制曝光,请单独具体化敏感特征。

若要在不手动解析表的情况下授予此访问权限,请使用 FeatureEngineeringClient.grant_feature_serving_access。 给定模型或特性规范后,它会将每个特性解析为其对应的在线表,在这些表上向您指定的主体授予 SELECT(以及 USE CATALOGUSE 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 目录中注册。

以下代码演示如何创建新的模型服务终结点。 有关详细信息,请参阅 创建自定义模型服务终结点

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 特征训练的,则请求有效负载还必须包含所有 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)
        },
    ],
)

实体键用于从在线存储中查找基于表的特征。 RequestSource 列直接传递到模型。

也可使用 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 View 提供服务,请创建一个引用这些特征的 FeatureSpec,并将其部署到特征服务端点。 终结点返回请求中实体键的查找特征值。

在创建终结点之前,必须在 Unity 目录中注册这些功能并 具体化到在线商店

注释

包含功能视图的一个 FeatureSpec 不能包含 FeatureLookupFeatureFunction 定义。 功能视图不能与这些定义在同一个 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"}],
)

有关功能服务终结点的详细信息,请参阅 功能服务终结点