-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
schemaRegistrySample.js
70 lines (58 loc) · 2 KB
/
schemaRegistrySample.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* @summary Demonstrates the use of a SchemaRegistryClient to register and retrieve schema.
*/
const { DefaultAzureCredential } = require("@azure/identity");
const { SchemaRegistryClient } = require("@azure/schema-registry");
// Load the .env file if it exists
require("dotenv").config();
// Set these environment variables or edit the following values
const fullyQualifiedNamespace =
process.env["SCHEMA_REGISTRY_ENDPOINT"] || "<fullyQualifiedNamespace>";
const group = 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",
},
],
};
// Description of the schema for registration
const schemaDescription = {
name: `${schemaObject.namespace}-${schemaObject.name}`,
groupName: group,
format: "Avro",
definition: JSON.stringify(schemaObject),
};
async function main() {
// Create a new client
const client = new SchemaRegistryClient(fullyQualifiedNamespace, new DefaultAzureCredential());
// Register a schema and get back its ID.
const registered = await client.registerSchema(schemaDescription);
console.log(`Registered schema with ID=${registered.id}`);
// Get ID for existing schema by its description.
// Note that this would throw if it had not been previously registered.
const found = await client.getSchemaProperties(schemaDescription);
if (found) {
console.log(`Got schema ID=${found.id}`);
}
// Get definition of existing schema by its ID
const foundSchema = await client.getSchema(registered.id);
if (foundSchema) {
console.log(`Got schema definition=${foundSchema.definition}`);
}
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});
module.exports = { main };