azure-sdk-for-js/sdk/schemaregistry/schema-registry-avro
Jeremy Meng 8dc3e561db
[EngSys] fix `schema-registry-avro` build failure on Windows (#31607)
After moving to vendored rimraf, this particular "clean" script is
failing likely due to the escaped quoted argument before it and how
dev-tool handls arguments. Moving `*.tgz *.log` fixes it.
2024-11-01 18:35:43 -07:00
..
review [eslint-plugin] add rule "@typescript-eslint/consistent-type-imports": "warn" 2024-10-30 15:48:52 +00:00
samples [EngSys] move to vendored version of cross-env via dev-tool 2024-11-02 00:48:06 +00:00
samples-dev [SR Json and Avro] Samples Fix (#31399) 2024-10-14 15:09:14 -07:00
src [eslint-plugin] add rule "@typescript-eslint/consistent-type-imports": "warn" 2024-10-30 15:48:52 +00:00
test [eslint-plugin] add rule "@typescript-eslint/consistent-type-imports": "warn" 2024-10-30 15:48:52 +00:00
types [Schema Registry Avro] Move the shim file to types (#17704) 2021-09-16 17:07:14 +00:00
CHANGELOG.md Post release automated changes for schemaregistry releases (#27550) 2023-10-25 10:47:19 -07:00
LICENSE [schema-registry] Implement avro-based serializer (#10890) 2020-09-03 15:06:10 -07:00
README.md [engsys] fix broken nodejs lts schedule link 2022-09-19 16:37:05 -04:00
api-extractor.json [Schema Registry Avro] ESM Migrate (#31131) 2024-10-11 11:23:55 -07:00
package.json [EngSys] fix `schema-registry-avro` build failure on Windows (#31607) 2024-11-01 18:35:43 -07:00
sample.env [Schema Registry] Migrate Test Suite Authentication (#29934) 2024-07-02 12:04:34 -07:00
test.browser.config.json [Schema Registry Avro] ESM Migrate (#31131) 2024-10-11 11:23:55 -07:00
tests.yml [SR Json and Avro] Samples Fix (#31399) 2024-10-14 15:09:14 -07:00
tsconfig.browser.config.json [Schema Registry Avro] ESM Migrate (#31131) 2024-10-11 11:23:55 -07:00
tsconfig.json [Schema Registry Avro] ESM Migrate (#31131) 2024-10-11 11:23:55 -07:00
vitest.browser.config.ts [Schema Registry Avro] ESM Migrate (#31131) 2024-10-11 11:23:55 -07:00
vitest.config.ts [Schema Registry Avro] ESM Migrate (#31131) 2024-10-11 11:23:55 -07:00

README.md

Azure Schema Registry Avro Serializer client library for JavaScript

Azure Schema Registry is a schema repository service hosted by Azure Event Hubs, providing schema storage, versioning, and management. This package provides an Avro serializer capable of serializing and deserializing payloads containing Avro-serialized data.

Key links:

Getting started

Prerequisites

Install the @azure/schema-registry-avro package

Install the Azure Text Analytics client library for JavaScript with npm:

npm install @azure/schema-registry-avro

Key concepts

AvroSerializer

Provides API to serialize to and deserialize from Avro Binary Encoding wrapped in a message with a content type field containing the schema ID. Uses SchemaRegistryClient from the @azure/schema-registry package to get schema IDs from schema definition or vice versa. The provided API has internal cache to avoid calling the schema registry service when possible.

Messages

By default, the serializer will create messages structured as follows:

  • data: a byte array containing data in the Avro Binary Encoding. Note that it is NOT Avro Object Container File. The latter includes the schema and creating it defeats the purpose of using this serializer to move the schema out of the message payload and into the schema registry.

  • contentType: a string of the following format avro/binary+<Schema ID> where the avro/binary part signals that this message has an Avro-serialized payload and the <Schema Id> part is the Schema ID the Schema Registry service assigned to the schema used to serialize this payload.

Not all messaging services are supporting the same message structure. To enable integration with such services, the serializer can act on custom message structures by setting the messageAdapter option in the constructor with a corresponding message producer and consumer. Azure messaging client libraries export default adapters for their message types.

Examples

Serialize and deserialize an @azure/event-hubs's EventData

const { DefaultAzureCredential } = require("@azure/identity");
const { createEventDataAdapter } = require("@azure/event-hubs");
const { SchemaRegistryClient } = require("@azure/schema-registry");
const { AvroSerializer } = require("@azure/schema-registry-avro");

const client = new SchemaRegistryClient(
  "<fully qualified namespace>",
  new DefaultAzureCredential()
);
const serializer = new AvroSerializer(client, {
  groupName: "<group>",
  messageAdapter: createEventDataAdapter(),
});

// Example Avro schema
const schema = JSON.stringify({
  type: "record",
  name: "Rating",
  namespace: "my.example",
  fields: [{ name: "score", type: "int" }],
});

// Example value that matches the Avro schema above
const value = { score: 42 };

// Serialize value to a message
const message = await serializer.serialize(value, schema);

// Deserialize a message to value
const deserializedValue = await serializer.deserialize(message);

Troubleshooting

The Avro serializer communicates with the Schema Registry service as needed to register or query schemas and those service calls could throw a RestError. Furthermore, errors of type Error will be thrown when serialization or deserialization fails. The cause property will contain the underlying error that was thrown from the Avro implementation library.

Logging

Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the AZURE_LOG_LEVEL environment variable to info. Alternatively, logging can be enabled at runtime by calling setLogLevel in the @azure/logger:

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

setLogLevel("info");

Next steps

Please take a look at the samples directory for detailed examples on how to use this library.

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

If you'd like to contribute to this library, please read the contributing guide to learn more about how to build and test the code.

Impressions