您可以通过两种方式在线提供功能视图:
- 模型服务:部署一个在特征视图上训练过的模型。 端点自动查找或计算特征值,利用模型记录时追踪的谱系。 使用它来提供模型预测。
-
功能服务:部署一个直接引用功能视图的
FeatureSpec,无需模型。 端点返回请求的功能输出。 当应用程序需要特征值而不是模型预测时,请使用此功能。
这两种方法都支持预计算和按需功能。 基于表和流的叶节点特征必须具有 支持的在线物化。
RequestSource 从请求中提供数值,并且 CustomUDF 按需计算特征。 对于带 FeatureViewSource 的 CustomUDF,提供服务时会自动解析上游依赖项并评估特征依赖图。
在 Unity Catalog 中注册所请求的特征及其所有传递性上游特征。 只物化由表和流支持的叶子节点,不要物化请求时或派生的 CustomUDF 特征。
权限
要提供特征,创建模型服务终结点的主体必须对支撑该具体化特征的 Unity Catalog 表具有 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 特征训练的,则请求有效负载还必须包含所有 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 View 提供服务,请创建一个引用这些特征的 FeatureSpec,并将其部署到特征服务端点。 端点只返回规范中请求的功能输出。它会自动计算中间值,除非你同时请求这些功能,否则不会返回中间值。
在创建端点之前,先在 Unity Catalog 注册所有功能,并为表和流背叶创建支持的在线实体化。
若要改为在捆绑包中声明特征视图FeatureSpec和端点,请参见《使用声明式自动化捆绑包提供特征视图》。
注释
包含功能视图的一个 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根据cost_sum_7d和revenue_sum_7d计算边距。 这些上游特征使用customer_id作为其实体键,使用event_time作为其时间序列列。
在创建规范之前,注册两个输出特征和每个上游特征。在此示例中,仅对 revenue_sum_7d 和 cost_sum_7d 叶节点特征进行在线物化。 服务层会查找其值,并针对每个请求计算 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 输出,不包含中间的收入和成本值。 关于依赖格式、轮子权限和安装故障排除,请参见添加 Python 依赖。