Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions sdk/schemaregistry/schema-registry-avro/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Release History

## 1.0.0 (Unreleased)

### Features Added

### Breaking Changes

### Bugs Fixed

### Other Changes

## 1.0.0-beta.7 (2022-03-10)

### Breaking Changes
Expand Down
4 changes: 2 additions & 2 deletions sdk/schemaregistry/schema-registry-avro/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@azure/schema-registry-avro",
"version": "1.0.0-beta.7",
"version": "1.0.0",
"description": "Schema Registry Avro Serializer Library with typescript type definitions for node.js and browser.",
"sdk-type": "client",
"main": "dist/index.js",
Expand Down Expand Up @@ -80,7 +80,7 @@
"devDependencies": {
"@azure/dev-tool": "^1.0.0",
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
"@azure/event-hubs": "^5.8.0-beta.1",
"@azure/event-hubs": "^5.8.0-beta.2",
"@azure/identity": "^2.0.1",
"@azure/test-utils": "^1.0.0",
"@azure-tools/test-recorder": "^1.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# Azure Schema Registry client library samples for JavaScript
# Azure Schema Registry client library samples for JavaScript (Beta)

These sample programs show how to use the JavaScript client libraries for Azure Schema Registry in some common scenarios.

| **File Name** | **Description** |
| ------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- |
| [schemaRegistryAvroSample.js][schemaregistryavrosample] | Demonstrates the use of SchemaRegistryAvroSerializer to serialize and deserialize using schema from Schema Registry. |
| **File Name** | **Description** |
| ----------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [schemaRegistryAvroSample.js][schemaregistryavrosample] | Demonstrates the use of AvroSerializer to create messages with avro-serialized payload using schema from Schema Registry. |
| [withEventHubsBufferedProducerClient.js][witheventhubsbufferedproducerclient] | Demonstrates the use of AvroSerializer to create messages with avro-serialized payload using schema from Schema Registry and send them to an Event Hub using the EventHub Buffered Producer Client. |
| [withEventHubsConsumerClient.js][witheventhubsconsumerclient] | Demonstrates the use of AvroSerializer to deserialize messages with avro-serialized payload received from the Event Hub Consumer Client. |
| [withEventHubsProducerClient.js][witheventhubsproducerclient] | Demonstrates the use of AvroSerializer to create messages with avro-serialized payload using schema from Schema Registry and send them to an Event Hub using the EventHub Producer Client. |

## Prerequisites

Expand Down Expand Up @@ -46,7 +49,10 @@ npx cross-env SCHEMA_REGISTRY_ENDPOINT="<schema registry endpoint>" SCHEMA_REGIS

Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients.

[schemaregistryavrosample]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/schemaregistry/schema-registry-avro/samples/v1/javascript/schemaRegistryAvroSample.js
[schemaregistryavrosample]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/schemaregistry/schema-registry-avro/samples/v1-beta/javascript/schemaRegistryAvroSample.js
[witheventhubsbufferedproducerclient]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/schemaregistry/schema-registry-avro/samples/v1-beta/javascript/withEventHubsBufferedProducerClient.js
[witheventhubsconsumerclient]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/schemaregistry/schema-registry-avro/samples/v1-beta/javascript/withEventHubsConsumerClient.js
[witheventhubsproducerclient]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/schemaregistry/schema-registry-avro/samples/v1-beta/javascript/withEventHubsProducerClient.js
[apiref]: https://docs.microsoft.com/javascript/api/@azure/schema-registry-avro
[freesub]: https://azure.microsoft.com/free/
[createinstance_azureschemaregistryresource]: https://aka.ms/schemaregistry
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "azure-schema-registry-avro-samples-js",
"name": "azure-schema-registry-avro-samples-js-beta",
"private": true,
"version": "1.0.0",
"description": "Azure Schema Registry client library samples for JavaScript",
"description": "Azure Schema Registry client library samples for JavaScript (Beta)",
"engines": {
"node": ">=12.0.0"
},
Expand All @@ -26,6 +26,7 @@
"@azure/schema-registry-avro": "next",
"dotenv": "latest",
"@azure/identity": "^2.0.1",
"@azure/schema-registry": "1.0.1"
"@azure/schema-registry": "1.0.2",
"@azure/event-hubs": "^5.8.0-beta.1"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ SCHEMA_REGISTRY_GROUP=<Group name for schemas in registry>
AZURE_TENANT_ID=<AD tenant id or name>
AZURE_CLIENT_ID=<ID of the user/service principal to authenticate as>
AZURE_CLIENT_SECRET=<client secret used to authenticate to Azure AD>

# Used in samples that use Event Hubs. Retrieve these values from an Event Hub in the Azure portal.
EVENTHUB_CONNECTION_STRING=<Event Hub connection string>
EVENTHUB_NAME=<Event Hub name>
CONSUMER_GROUP_NAME=<Event Hub Group name>

# Used in samples that use Event Grid. Retrieve these values from an Event Grid topic in the Azure portal
EVENT_GRID_TOPIC_ENDPOINT=<Event Grid topic endpoint>
EVENT_GRID_TOPIC_API_KEY=<Event Grid topic API key>
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
* @summary Demonstrates the use of AvroSerializer to create messages with avro-serialized payload using schema from Schema Registry.
*/

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

// Load the .env file if it exists
require("dotenv").config();

// The fully qualified namespace for schema registry
const schemaRegistryFullyQualifiedNamespace =
process.env["SCHEMA_REGISTRY_ENDPOINT"] || "<endpoint>";

// The schema group to use for schema registeration or lookup
const groupName = process.env["SCHEMA_REGISTRY_GROUP"] || "AzureSdkSampleGroup";

// Sample Avro Schema for user with first and last names
const schemaObject = {
type: "record",
name: "User",
namespace: "com.azure.schemaregistry.samples",
fields: [
{
name: "firstName",
type: "string",
},
{
name: "lastName",
type: "string",
},
],
};

const schema = JSON.stringify(schemaObject);

// Description of the schema for registration
const schemaDescription = {
name: `${schemaObject.namespace}.${schemaObject.name}`,
groupName,
format: "Avro",
definition: schema,
};

async function main() {
// Create a new client
const client = new SchemaRegistryClient(
schemaRegistryFullyQualifiedNamespace,
new DefaultAzureCredential()
);

// Register the schema. This would generally have been done somewhere else.
// You can also skip this step and let `serializeMessageData` automatically register
// schemas using autoRegisterSchemas=true, but that is NOT recommended in production.
await client.registerSchema(schemaDescription);

// Create a new serializer backed by the client
const serializer = new AvroSerializer(client, { groupName });

// serialize an object that matches the schema and put it in a message
const value = { firstName: "Jane", lastName: "Doe" };
const message = await serializer.serializeMessageData(value, schema);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must have missed this in the PR to switch to using Serializer, but we actually renamed this to just serialize in .NET. We also renamed MessageWithMetadata to BinaryContent. Is there a reason not to do this same change in JS?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I will talk to @bterlson about those renames.

console.log("Created message:");
console.log(JSON.stringify(message));

// deserialize the message back to an object
const deserializedObject = await serializer.deserializeMessageData(message);
console.log("Deserialized object:");
console.log(JSON.stringify(deserializedObject));
}

main().catch((err) => {
console.error("The sample encountered an error:", err);
});

module.exports = { main };
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
* @summary Demonstrates the use of AvroSerializer to create messages with avro-serialized payload using schema from Schema Registry and send them to an Event Hub using the EventHub Buffered Producer Client.
*/

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

// Load the .env file if it exists
require("dotenv").config();

// The fully qualified namespace for schema registry
const schemaRegistryFullyQualifiedNamespace =
process.env["SCHEMA_REGISTRY_ENDPOINT"] || "<endpoint>";

// The schema group to use for schema registeration or lookup
const groupName = process.env["SCHEMA_REGISTRY_GROUP"] || "AzureSdkSampleGroup";

// The connection string for Event Hubs
const eventHubsConnectionString = process.env["EVENTHUB_CONNECTION_STRING"] || "";

// Sample Avro Schema for user with first and last names
const schemaObject = {
type: "record",
name: "User",
namespace: "com.azure.schemaregistry.samples",
fields: [
{
name: "firstName",
type: "string",
},
{
name: "lastName",
type: "string",
},
],
};

const schema = JSON.stringify(schemaObject);

// Description of the schema for registration
const schemaDescription = {
name: `${schemaObject.namespace}.${schemaObject.name}`,
groupName,
format: "Avro",
definition: schema,
};

async function handleError() {
console.log("An error occured when sending a message");
}

async function main() {
// Create a new client
const schemaRegistryClient = new SchemaRegistryClient(
schemaRegistryFullyQualifiedNamespace,
new DefaultAzureCredential()
);

// Register the schema. This would generally have been done somewhere else.
// You can also skip this step and let `serializeMessageData` automatically register
// schemas using autoRegisterSchemas=true, but that is NOT recommended in production.
await schemaRegistryClient.registerSchema(schemaDescription);

// Create a new serializer backed by the client
const serializer = new AvroSerializer(schemaRegistryClient, {
groupName,
messageAdapter: createEventDataAdapter(),
});

const eventHubsBufferedProducerClient = new EventHubBufferedProducerClient(
eventHubsConnectionString,
{
onSendEventsErrorHandler: handleError,
}
);

// serialize an object that matches the schema
const value = { firstName: "Jane", lastName: "Doe" };
const message = await serializer.serializeMessageData(value, schema);
console.log("Created message:");
console.log(message);

await eventHubsBufferedProducerClient.enqueueEvent(message);
console.log(`Message was added to the queue and is about to be sent`);

// Wait for a bit before cleaning up the sample
setTimeout(async () => {
await eventHubsBufferedProducerClient.close({ flush: true });
console.log(`Exiting sample`);
}, 30 * 1000);
}

main().catch((err) => {
console.error("The sample encountered an error:", err);
});

module.exports = { main };
Loading