你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

适用于 JavaScript 的 Azure 架构注册表客户端库 - 版本 1.2.0

Azure 架构注册表是由 Azure 事件中心 托管的架构存储库服务,提供架构存储、版本控制和管理。 序列化程序利用注册表来减小有效负载大小,同时使用架构标识符(而不是完整架构)描述有效负载结构。

关键链接:

入门

先决条件

安装 @azure/schema-registry

使用 npm安装适用于 JavaScript 的 Azure 文本分析 客户端库:

npm install @azure/schema-registry

创建 SchemaRegistryClient 并对其进行身份验证

若要创建客户端对象以访问架构注册表 API,需要架构注册表资源的完全限定命名空间和 credential。 架构注册表客户端使用 Azure Active Directory 凭据进行身份验证。

可以使用 Azure 标识库对 Azure Active Directory 进行身份验证。 若要使用如下所示的 DefaultAzureCredential 提供程序或 Azure SDK 提供的其他凭据提供程序,请安装包 @azure/identity

npm install @azure/identity

将 AAD 应用程序的客户端 ID、租户 ID 和客户端机密的值设置为环境变量: AZURE_CLIENT_IDAZURE_TENANT_IDAZURE_CLIENT_SECRET

const { DefaultAzureCredential } = require("@azure/identity");
const { SchemaRegistryClient } = require("@azure/schema-registry");

const client = new SchemaRegistryClient("<fullyQualifiedNamespace>", new DefaultAzureCredential());

关键概念

SchemaRegistryClient

SchemaRegistryClient 提供用于在架构注册表中存储和检索架构的 API。

示例

注册架构

const { DefaultAzureCredential } = require("@azure/identity");
const { SchemaRegistryClient } = require("@azure/schema-registry");

const client = new SchemaRegistryClient("<fullyQualifiedNamespace>", new DefaultAzureCredential());

const description = {
  name: "<name>",
  groupName: "<group name>",
  format: "<schema format>",
  definition: "<schema definition>"
}

const registered = await client.registerSchema(description);
console.log(registered.id);

获取现有架构的 ID

const { DefaultAzureCredential } = require("@azure/identity");
const { SchemaRegistryClient } = require("@azure/schema-registry");

const client = new SchemaRegistryClient("<fullyQualifiedNamespace>", new DefaultAzureCredential());

const description = {
  name: "<name>",
  groupName: "<group name>",
  format: "<schema format>",
  definition: "<schema definition>"
}

const found = await client.getSchemaProperties(description);
if (found) {
  console.log(`Got schema ID=${found.id}`);
}

按 ID 获取现有架构的定义

const { DefaultAzureCredential } = require("@azure/identity");
const { SchemaRegistryClient } = require("@azure/schema-registry");

const client = new SchemaRegistryClient("<fullyQualifiedNamespace>", new DefaultAzureCredential());
const foundSchema = await client.getSchema("<id>");
if (foundSchema) {
  console.log(`Got schema definition=${foundSchema.definition}`);
}

按版本获取现有架构的定义

const { DefaultAzureCredential } = require("@azure/identity");
const { SchemaRegistryClient } = require("@azure/schema-registry");

const client = new SchemaRegistryClient("<fullyQualifiedNamespace>", new DefaultAzureCredential());
const foundSchema = await client.getSchema("<schema name>", "<group name>", version);
if (foundSchema) {
  console.log(`Got schema definition=${foundSchema.definition}`);
}

疑难解答

日志记录

启用日志记录可能有助于发现有关故障的有用信息。 若要查看 HTTP 请求和响应的日志,请将 AZURE_LOG_LEVEL 环境变量设置为 info。 或者,可以在运行时通过调用 @azure/logger 中的 setLogLevel 来启用日志记录:

const { setLogLevel } = require("@azure/logger");

setLogLevel("info");

后续步骤

请查看 示例 目录,获取有关如何使用此库的详细示例。

贡献

本项目欢迎贡献和建议。 大多数贡献要求你同意贡献者许可协议 (CLA),并声明你有权(并且确实有权)授予我们使用你的贡献的权利。 有关详细信息,请访问 https://cla.microsoft.com

提交拉取请求时,CLA 机器人将自动确定你是否需要提供 CLA,并相应地修饰 PR(例如标签、注释)。 直接按机器人提供的说明操作。 只需使用 CLA 对所有存储库执行一次这样的操作。

此项目采用了 Microsoft 开放源代码行为准则。 有关详细信息,请参阅行为准则常见问题解答,或如果有任何其他问题或意见,请与 联系。

若要为此库做出贡献,请阅读贡献指南,详细了解如何生成和测试代码。

曝光数