public static void GetSchemaById(string server) { var topicName = Guid.NewGuid().ToString(); var testSchema1 = "{\"type\":\"record\",\"name\":\"User\",\"namespace\":\"Confluent.Kafka.Examples.AvroSpecific" + "\",\"fields\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"favorite_number\",\"type\":[\"i" + "nt\",\"null\"]},{\"name\":\"favorite_color\",\"type\":[\"string\",\"null\"]}]}"; var sr = new CachedSchemaRegistryClient(new Dictionary <string, object> { { "schema.registry.url", server } }); var subject = sr.ConstructValueSubjectName(topicName); var id = sr.RegisterSchemaAsync(subject, testSchema1).Result; var schema = sr.GetSchemaAsync(id).Result; Assert.Equal(schema, testSchema1); }
public static void GetSchemaBySubjectAndVersion(Config config) { var topicName = Guid.NewGuid().ToString(); var testSchema1 = "{\"type\":\"record\",\"name\":\"User\",\"namespace\":\"Confluent.Kafka.Examples.AvroSpecific" + "\",\"fields\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"favorite_number\",\"type\":[\"i" + "nt\",\"null\"]},{\"name\":\"favorite_color\",\"type\":[\"string\",\"null\"]}]}"; var sr = new CachedSchemaRegistryClient(new SchemaRegistryConfig { Url = config.Server }); var subject = sr.ConstructValueSubjectName(topicName); var id = sr.RegisterSchemaAsync(subject, testSchema1).Result; var schema = sr.GetLatestSchemaAsync(subject).Result; var schemaString = sr.GetSchemaAsync(subject, schema.Version).Result; Assert.Equal(schemaString, testSchema1); }
/// <summary> /// Creates a deserializer. /// </summary> /// <param name="registryConfiguration"> /// Schema Registry configuration. Using the <see cref="SchemaRegistryConfig" /> class is /// highly recommended. /// </param> /// <param name="deserializerBuilder"> /// A deserializer builder (used to build deserialization functions for C# types). If none /// is provided, the default deserializer builder will be used. /// </param> /// <param name="schemaReader"> /// A JSON schema reader (used to convert schemas received from the registry into abstract /// representations). If none is provided, the default schema reader will be used. /// </param> /// <exception cref="ArgumentNullException"> /// Thrown when the registry configuration is null. /// </exception> public AsyncSchemaRegistryDeserializer( IEnumerable <KeyValuePair <string, string> > registryConfiguration, IBinaryDeserializerBuilder deserializerBuilder = null, IJsonSchemaReader schemaReader = null ) : this( deserializerBuilder, schemaReader ) { if (registryConfiguration == null) { throw new ArgumentNullException(nameof(registryConfiguration)); } _resolve = async id => { using (var registry = new CachedSchemaRegistryClient(registryConfiguration)) { return(await registry.GetSchemaAsync(id).ConfigureAwait(false)); } }; }
public static void GetSchemaById(Config config) { var topicName = Guid.NewGuid().ToString(); var testSchema1 = "{\"type\":\"record\",\"name\":\"User\",\"namespace\":\"Confluent.Kafka.Examples.AvroSpecific" + "\",\"fields\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"favorite_number\",\"type\":[\"i" + "nt\",\"null\"]},{\"name\":\"favorite_color\",\"type\":[\"string\",\"null\"]}]}"; var sr = new CachedSchemaRegistryClient(new SchemaRegistryConfig { Url = config.Server }); var subject = SubjectNameStrategy.Topic.ConstructKeySubjectName(topicName, null); var id = sr.RegisterSchemaAsync(subject, testSchema1).Result; var schema = sr.GetSchemaAsync(id).Result; Assert.Equal(schema.SchemaString, testSchema1); Assert.Empty(schema.References); Assert.Equal(schema.SchemaType, SchemaType.Avro); }
public static void Protobuf(Config config) { var srInitial = new CachedSchemaRegistryClient(new SchemaRegistryConfig { Url = config.Server }); var sr = new CachedSchemaRegistryClient(new SchemaRegistryConfig { Url = config.Server }); var testSchemaBase64 = Confluent.Kafka.Examples.Protobuf.User.Descriptor.File.SerializedData.ToBase64(); var topicName = Guid.NewGuid().ToString(); var subjectInitial = SubjectNameStrategy.Topic.ConstructValueSubjectName(topicName, null); var subject = SubjectNameStrategy.Topic.ConstructValueSubjectName(topicName + "2", null); // check that registering a base64 protobuf schema works. var id1 = srInitial.RegisterSchemaAsync(subjectInitial, new Schema(testSchemaBase64, SchemaType.Protobuf)).Result; var schema1 = sr.GetSchemaAsync(id1, "serialized").Result; // use a different sr instance to ensure a cached value is not read. Assert.Equal(SchemaType.Protobuf, schema1.SchemaType); Assert.NotNull(schema1.SchemaString); // SR slightly munges the schema as a result of moving to a text representation and back so can't check for equality. // check that the id of the schema just registered can be retrieved. var id = sr.GetSchemaIdAsync(subjectInitial, new Schema(schema1.SchemaString, SchemaType.Protobuf)).Result; Assert.Equal(id1, id); // re-register the munged schema (to a different subject) and check that it is not re-munged. var id2 = sr.RegisterSchemaAsync(subject, schema1).Result; var schema2 = sr.GetSchemaAsync(id2, "serialized").Result; Assert.Equal(schema1.SchemaString, schema2.SchemaString); Assert.Equal(schema1.SchemaType, schema2.SchemaType); // This sequence of operations is designed to test caching behavior (and two alternat schema getting methods). var schemaAsText = sr.GetSchemaAsync(id2).Result; var schemaAsSerialized = sr.GetSchemaAsync(id2, "serialized").Result; var schemaAsText2 = sr.GetSchemaAsync(id2).Result; var schemaAsSerialized2 = sr.GetSchemaAsync(id2, "serialized").Result; var latestSchema = sr.GetLatestSchemaAsync(subject).Result; // should come back as text. var schemaAsSerialized3 = sr.GetSchemaAsync(id2, "serialized").Result; var latestSchema2 = sr.GetRegisteredSchemaAsync(subject, latestSchema.Version).Result; // should come back as text. Assert.Equal(schema1.SchemaString, schemaAsSerialized.SchemaString); Assert.Equal(SchemaType.Protobuf, schemaAsSerialized.SchemaType); Assert.Empty(schemaAsSerialized.References); Assert.Equal(schema1.SchemaString, schemaAsSerialized2.SchemaString); Assert.Equal(schema1.SchemaString, schemaAsSerialized3.SchemaString); Assert.NotEqual(schema1.SchemaString, schemaAsText.SchemaString); Assert.Equal(schemaAsText.SchemaString, schemaAsText2.SchemaString); Assert.Equal(schemaAsText.SchemaString, latestSchema.SchemaString); Assert.Equal(schemaAsText.SchemaString, latestSchema2.SchemaString); Assert.Equal(SchemaType.Protobuf, schemaAsText.SchemaType); Assert.Empty(schemaAsText2.References); // compatibility var compat = sr.IsCompatibleAsync(subject, schema2).Result; Assert.True(compat); var avroSchema = "{\"type\":\"record\",\"name\":\"User\",\"namespace\":\"Confluent.Kafka.Examples.AvroSpecific" + "\",\"fields\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"favorite_number\",\"type\":[\"i" + "nt\",\"null\"]},{\"name\":\"favorite_color\",\"type\":[\"string\",\"null\"]}]}"; var compat2 = sr.IsCompatibleAsync(subject, avroSchema).Result; Assert.False(compat2); var compat3 = sr.IsCompatibleAsync(subject, new Schema(avroSchema, SchemaType.Avro)).Result; Assert.False(compat3); // invalid type Assert.ThrowsAny <Exception>(() => { sr.RegisterSchemaAsync(SubjectNameStrategy.Topic.ConstructKeySubjectName(topicName + "3", null), new Schema(avroSchema, SchemaType.Protobuf)).Wait(); }); }