Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Schema Registry] Add getSchemaByVersion #23162

Merged
merged 4 commits into from
Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions sdk/schemaregistry/schema-registry/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Release History

## 1.1.0 (Unreleased)

### Features Added

- `getSchemaByVersion` is added to query schemas by their version.
- `version` property is added to `SchemaProperties`.

## 1.1.0 (2022-05-10)

### Features Added
Expand Down
13 changes: 13 additions & 0 deletions sdk/schemaregistry/schema-registry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ if (foundSchema) {
}
```

### Get definition of existing schema by version

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

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

## Troubleshooting

### Logging
Expand Down
2 changes: 1 addition & 1 deletion sdk/schemaregistry/schema-registry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"//metadata": {
"constantPaths": [
{
"path": "src/generated/generatedSchemaRegistryClientContext.ts",
"path": "src/generated/generatedSchemaRegistryClient.ts",
"prefix": "packageDetails"
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { CommonClientOptions } from '@azure/core-client';
import { OperationOptions } from '@azure/core-client';
import { TokenCredential } from '@azure/core-auth';

// @public
export interface GetSchemaByVersionOptions extends OperationOptions {
}

// @public
export interface GetSchemaOptions extends OperationOptions {
}
Expand Down Expand Up @@ -40,6 +44,7 @@ export interface SchemaProperties {
groupName: string;
id: string;
name: string;
version: number;
}

// @public
Expand All @@ -54,6 +59,7 @@ export class SchemaRegistryClient implements SchemaRegistry {
constructor(fullyQualifiedNamespace: string, credential: TokenCredential, options?: SchemaRegistryClientOptions);
readonly fullyQualifiedNamespace: string;
getSchema(schemaId: string, options?: GetSchemaOptions): Promise<Schema>;
getSchemaByVersion(schemaVersion: SchemaVersion, options?: GetSchemaByVersionOptions): Promise<Schema>;
getSchemaProperties(schema: SchemaDescription, options?: GetSchemaPropertiesOptions): Promise<SchemaProperties>;
registerSchema(schema: SchemaDescription, options?: RegisterSchemaOptions): Promise<SchemaProperties>;
}
Expand All @@ -63,6 +69,13 @@ export interface SchemaRegistryClientOptions extends CommonClientOptions {
apiVersion?: string;
}

// @public
export interface SchemaVersion {
groupName: string;
name: string;
version: number;
}

// (No @packageDocumentation comment for this package)

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
* @summary Demonstrates the use of a SchemaRegistryClient to register and retrieve schema.
*/

import { DefaultAzureCredential } from "@azure/identity";
import { SchemaRegistryClient, SchemaDescription } from "@azure/schema-registry";

// Load the .env file if it exists
import * as dotenv from "dotenv";
dotenv.config();

// Set these environment variables or edit the following values
const fullyQualifiedNamespace =
process.env["SCHEMA_REGISTRY_ENDPOINT"] || "<fullyQualifiedNamespace>";
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 name = `${schemaObject.namespace}-${schemaObject.name}`;

// Description of the schema for registration
const schemaDescription: SchemaDescription = {
name,
groupName,
format: "Avro",
definition: JSON.stringify(schemaObject),
};

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

// Register a schema and get back its ID.
swathipil marked this conversation as resolved.
Show resolved Hide resolved
const { id, version } = await client.registerSchema(schemaDescription);
swathipil marked this conversation as resolved.
Show resolved Hide resolved
console.log(
`Registered schema with the following properties:\n- ID=${id}\n- Version: ${version}`
);

// Get definition of existing schema by its version
const foundSchema = await client.getSchemaByVersion({
groupName,
name,
version,
});
if (foundSchema) {
console.log(`Got schema definition=${foundSchema.definition}`);
}
}

main().catch((err) => {
console.error("The sample encountered an error:", err);
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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";
const groupName = process.env["SCHEMA_REGISTRY_GROUP"] || "AzureSdkSampleGroup";

// Sample Avro Schema for user with first and last names
const schemaObject = {
Expand All @@ -34,10 +34,12 @@ const schemaObject = {
],
};

const name = `${schemaObject.namespace}-${schemaObject.name}`;

// Description of the schema for registration
const schemaDescription: SchemaDescription = {
name: `${schemaObject.namespace}-${schemaObject.name}`,
groupName: group,
name,
groupName,
format: "Avro",
definition: JSON.stringify(schemaObject),
};
Expand All @@ -47,18 +49,13 @@ export async function main() {
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}`);
}
const { id, version } = await client.registerSchema(schemaDescription);
console.log(
`Registered schema with the following properties:\n- ID=${id}\n- Version: ${version}`
);

// Get definition of existing schema by its ID
const foundSchema = await client.getSchema(registered.id);
const foundSchema = await client.getSchema(id);
if (foundSchema) {
console.log(`Got schema definition=${foundSchema.definition}`);
}
Expand Down
6 changes: 4 additions & 2 deletions sdk/schemaregistry/schema-registry/src/conversions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { SchemaProperties, Schema } from "./models";
import { Schema, SchemaProperties } from "./models";

import {
SchemaGetByIdResponse,
SchemaRegisterResponse,
SchemaQueryIdByContentResponse as SchemaQueryIdByDefinitionResponse,
SchemaRegisterResponse,
} from "./generated/models";
import { getSchemaDefinition } from "./getSchemaDefinition";

Expand Down Expand Up @@ -34,6 +34,7 @@ export async function convertSchemaResponse(response: GeneratedSchemaResponse):
format: mapContentTypeToFormat(response.contentType!),
groupName: response.schemaGroupName!,
name: response.schemaName!,
version: response.schemaVersion!,
},
};
}
Expand All @@ -54,6 +55,7 @@ export function convertSchemaIdResponse(
format: schemaFormat,
groupName: response.schemaGroupName!,
name: response.schemaName!,
version: response.schemaVersion!,
};
};
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Loading