You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过HTTP客户端上传/检索Azure Schema Registry中的Schema

解决方案:Azure Schema Registry 上传/检索 Schema 的 HTTP 方法

你之前使用的是 Azure ARM 管理 API(management.azure.com),这个 API 仅用于管理 Schema Group 这类资源实体,无法直接操作具体的 Schema。要上传或检索 Schema,需要使用Azure Schema Registry 专用 REST API,Endpoint 格式为 https://<event-hub-namespace>.servicebus.windows.net/$schemagroups/<schema-group-name>/schemas。

关键前提:正确的身份验证令牌

需要获取针对 Event Hubs 服务的 Azure AD 令牌(受众为 https://eventhubs.azure.net/,而非 ARM 服务的 https://management.azure.com/),可以通过 Azure CLI 快速获取:

az account get-access-token --resource https://eventhubs.azure.net/ --query accessToken -o tsv

1. 上传 Schema(以 Avro 为例)

使用 PUT 请求指定 Schema 名称上传,或用 POST 自动生成 Schema ID:

指定 Schema 名称上传

curl -X PUT "https://evh-ns-cancellation-service-ab-dev.servicebus.windows.net/$schemagroups/schgp-cancellation-service-ab-dev/schemas/MyTestSchema?api-version=2023-07-01" \
  -H "Authorization: Bearer $(az account get-access-token --resource https://eventhubs.azure.net/ --query accessToken -o tsv)" \
  -H "Content-Type: application/json" \
  -d '{
    "schemaType": "Avro",
    "schemaDefinition": "{\"type\": \"record\", \"name\": \"TestEvent\", \"fields\": [{\"name\": \"orderId\", \"type\": \"string\"}, {\"name\": \"timestamp\", \"type\": \"long\"}]}"}'

从文件读取 Schema 定义上传

如果 Schema 定义保存在本地文件(如 order-schema.avsc),可以简化命令:

curl -X PUT "https://evh-ns-cancellation-service-ab-dev.servicebus.windows.net/$schemagroups/schgp-cancellation-service-ab-dev/schemas/MyTestSchema?api-version=2023-07-01" \
  -H "Authorization: Bearer $(az account get-access-token --resource https://eventhubs.azure.net/ --query accessToken -o tsv)" \
  -H "Content-Type: application/json" \
  -d @order-schema.avsc

2. 检索 Schema

按 Schema 名称检索

curl "https://evh-ns-cancellation-service-ab-dev.servicebus.windows.net/$schemagroups/schgp-cancellation-service-ab-dev/schemas/MyTestSchema?api-version=2023-07-01" \
  -H "Authorization: Bearer $(az account get-access-token --resource https://eventhubs.azure.net/ --query accessToken -o tsv)"

按 Schema ID 检索

上传后会返回 Schema ID,可通过 ID 直接检索:

curl "https://evh-ns-cancellation-service-ab-dev.servicebus.windows.net/$schemagroups/schgp-cancellation-service-ab-dev/schemas/ids/<your-schema-id>?api-version=2023-07-01" \
  -H "Authorization: Bearer $(az account get-access-token --resource https://eventhubs.azure.net/ --query accessToken -o tsv)"

注意事项

  • 务必使用正确的 API 版本(推荐使用稳定版如 2023-07-01,不要沿用 ARM API 的版本)。
  • 确保令牌的受众是 https://eventhubs.azure.net/,否则会返回权限不足错误。
  • Schema 定义需符合指定类型(如 Avro)的语法规范,JSON 格式要正确转义或通过文件读取避免转义问题。

内容的提问来源于stack exchange,提问作者themathmagician

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 20:17:21