public void Multiple_ISpecificRecords() { var serializer = new AvroSerializer <ISpecificRecord>(schemaRegistryClient); var deserializerUser = new AvroDeserializer <User>(schemaRegistryClient); var deserializerCar = new AvroDeserializer <Car>(schemaRegistryClient); var user = new User { favorite_color = "blue", favorite_number = 100, name = "awesome" }; var car = new Car { color = "blue", name = "great_brand" }; var bytesUser = serializer.SerializeAsync(user, new SerializationContext(MessageComponentType.Value, testTopic)).Result; var resultUser = deserializerUser.DeserializeAsync(bytesUser, false, new SerializationContext(MessageComponentType.Value, testTopic)).Result as User; Assert.NotNull(resultUser); Assert.Equal(user.name, resultUser.name); Assert.Equal(user.favorite_color, resultUser.favorite_color); Assert.Equal(user.favorite_number, resultUser.favorite_number); var bytesCar = serializer.SerializeAsync(car, new SerializationContext(MessageComponentType.Value, testTopic)).Result; var resultCar = deserializerCar.DeserializeAsync(bytesCar, false, new SerializationContext(MessageComponentType.Value, testTopic)).Result as Car; Assert.NotNull(resultCar); Assert.Equal(car.name, resultCar.name); Assert.Equal(car.color, resultCar.color); }
public void Incompatible() { var avroSerializer = new AvroSerializer <string>(schemaRegistryClient); var avroDeserializer = new AvroDeserializer <int>(schemaRegistryClient); var bytes = avroSerializer.SerializeAsync("hello world", new SerializationContext(MessageComponentType.Value, testTopic)).Result; Assert.Throws <System.AggregateException>(() => avroDeserializer.DeserializeAsync(bytes, false, new SerializationContext(MessageComponentType.Value, testTopic)).Result); }
public void Poco_Serialize() { var serializer = new AvroSerializer <Dictionary <string, string> >(schemaRegistryClient); Assert.Throws <System.InvalidOperationException>(() => serializer.SerializeAsync(new Dictionary <string, string> { { "cat", "dog" } }, new SerializationContext(MessageComponentType.Key, testTopic)).GetAwaiter().GetResult()); }
public void Incompatible() { var avroSerializer = new AvroSerializer <string>(schemaRegistryClient); var avroDeserializer = new AvroDeserializer <int>(schemaRegistryClient); var bytes = avroSerializer.SerializeAsync("hello world", false, new MessageMetadata(), new TopicPartition("topic", Partition.Any)).Result; Assert.Throws <System.AggregateException>(() => avroDeserializer.DeserializeAsync(bytes, false, false, new MessageMetadata(), new TopicPartition("topic", Partition.Any)).Result); }
public void DoubleSerDe() { var avroSerializer = new AvroSerializer <double>(schemaRegistryClient); var avroDeserializer = new AvroDeserializer <double>(schemaRegistryClient); byte[] bytes; bytes = avroSerializer.SerializeAsync(123d, new SerializationContext(MessageComponentType.Value, testTopic)).Result; Assert.Equal(123d, avroDeserializer.DeserializeAsync(bytes, false, new SerializationContext(MessageComponentType.Value, testTopic)).Result); }
public void BoolSerDe() { var avroSerializer = new AvroSerializer <bool>(schemaRegistryClient); var avroDeserializer = new AvroDeserializer <bool>(schemaRegistryClient); byte[] bytes; bytes = avroSerializer.SerializeAsync(true, new SerializationContext(MessageComponentType.Value, testTopic)).Result; Assert.True(avroDeserializer.DeserializeAsync(bytes, false, new SerializationContext(MessageComponentType.Value, testTopic)).Result); }
public void DoubleSerDe() { var avroSerializer = new AvroSerializer <double>(schemaRegistryClient); var avroDeserializer = new AvroDeserializer <double>(schemaRegistryClient); byte[] bytes; bytes = avroSerializer.SerializeAsync(123d, false, new MessageMetadata(), new TopicPartition(testTopic, Partition.Any)).Result; Assert.Equal(123d, avroDeserializer.DeserializeAsync(bytes, false, false, new MessageMetadata(), new TopicPartition(testTopic, Partition.Any)).Result); }
public void BytesSerDe() { var avroSerializer = new AvroSerializer <byte[]>(schemaRegistryClient); var avroDeserializer = new AvroDeserializer <byte[]>(schemaRegistryClient); byte[] bytes; bytes = avroSerializer.SerializeAsync(new byte[] { 2, 3, 4 }, false, new MessageMetadata(), new TopicPartition(testTopic, Partition.Any)).Result; Assert.Equal(new byte[] { 2, 3, 4 }, avroDeserializer.DeserializeAsync(bytes, false, false, new MessageMetadata(), new TopicPartition(testTopic, Partition.Any)).Result); }
public void NullString() { var serializer = new AvroSerializer <string>(schemaRegistryClient); var deserializer = new AvroDeserializer <string>(schemaRegistryClient); var bytes = serializer.SerializeAsync(null, new SerializationContext(MessageComponentType.Value, testTopic)).Result; var result = deserializer.DeserializeAsync(bytes, isNull: true, new SerializationContext(MessageComponentType.Value, testTopic)).Result; Assert.Null(bytes); Assert.Null(result); }
public void ISpecificRecord() { var serializer = new AvroSerializer <User>(schemaRegistryClient); var deserializer = new AvroDeserializer <User>(schemaRegistryClient); var user = new User { favorite_color = "blue", favorite_number = 100, name = "awesome" }; var bytes = serializer.SerializeAsync(user, new SerializationContext(MessageComponentType.Value, testTopic)).Result; var result = deserializer.DeserializeAsync(bytes, false, new SerializationContext(MessageComponentType.Value, testTopic)).Result; Assert.Equal(user.name, result.name); Assert.Equal(user.favorite_color, result.favorite_color); Assert.Equal(user.favorite_number, result.favorite_number); }
public void ISpecificRecord() { var serializer = new AvroSerializer <User>(schemaRegistryClient); var deserializer = new AvroDeserializer <User>(schemaRegistryClient); var user = new User { favorite_color = "blue", favorite_number = 100, name = "awesome" }; var bytes = serializer.SerializeAsync(user, false, new MessageMetadata(), new TopicPartition("topic", Partition.Any)).Result; var result = deserializer.DeserializeAsync(bytes, false, false, new MessageMetadata(), new TopicPartition("topic", Partition.Any)).Result; Assert.Equal(user.name, result.name); Assert.Equal(user.favorite_color, result.favorite_color); Assert.Equal(user.favorite_number, result.favorite_number); }
public async Task HandleTopicAsync(string topic, CancellationToken token) { var types = GetTypes(topic); using var schemaRegistry = new CachedSchemaRegistryClient(_schemaRegistryConfig); using var consumer = new ConsumerBuilder <string, GenericRecord>(_consumerConfig) .SetKeyDeserializer(new AvroDeserializer <string>(schemaRegistry).AsSyncOverAsync()) .SetValueDeserializer(new AvroDeserializer <GenericRecord>(schemaRegistry).AsSyncOverAsync()) .SetErrorHandler((_, e) => Console.WriteLine($"Error: {e.Reason}")) .Build(); consumer.Subscribe(topic); try { while (true) { try { var cr = consumer.Consume(token); if (cr.Message != null) { var type = types.FirstOrDefault(x => x.FullName == cr.Message.Value.Schema.Fullname); var serializer = new AvroSerializer <GenericRecord>(schemaRegistry); var bytes = await serializer.SerializeAsync(cr.Message.Value, new SerializationContext()); var handler = (IAvroEventHandler)_serviceProvider.GetService(typeof(AvroEventHandler <>).MakeGenericType(type)); await handler.Handle(bytes, schemaRegistry, token); consumer.Commit(cr); } } catch (ConsumeException ex) { Debug.WriteLine(ex.Error.Reason); } } } catch (OperationCanceledException) { consumer.Close(); } }
public void ISpecificRecordComplex() { var serializer = new AvroSerializer <Parent>(schemaRegistryClient); var deserializer = new AvroDeserializer <Parent>(schemaRegistryClient); var user = new Parent { children = new List <Child> { new Child { name = "test" } } }; var bytes = serializer.SerializeAsync(user, new SerializationContext(MessageComponentType.Value, testTopic)).Result; var result = deserializer.DeserializeAsync(bytes, false, new SerializationContext(MessageComponentType.Value, testTopic)).Result; //Assert.Equal(user.name, result.name); //Assert.Equal(user.favorite_color, result.favorite_color); //Assert.Equal(user.favorite_number, result.favorite_number); }
public async Task <byte[]> SerializeAsync(GenericRecord genericRecord, string topic) => await _avroSerializer.SerializeAsync(genericRecord, new SerializationContext(MessageComponentType.Value, topic));