public static async Task UserAsBytes(
            [KafkaTrigger("LocalBroker", "users", ValueType = typeof(byte[]), ConsumerGroup = "azfunc_bytes")] byte[][] kafkaEvents,
            ILogger logger)
        {
            foreach (var kafkaEvent in kafkaEvents)
            {
                var desUserRecord = await myCustomDeserialiser.DeserializeAsync(kafkaEvent, false, Confluent.Kafka.SerializationContext.Empty);

                logger.LogInformation($"Custom deserialised user from batch: {JsonConvert.SerializeObject(desUserRecord)}");
            }
        }
예제 #2
0
        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", 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);
        }
예제 #4
0
        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);
        }
예제 #5
0
        public void NullInt()
        {
            var deserializer = new AvroDeserializer <int>(schemaRegistryClient);

            var exception = Assert.Throws <AggregateException>(() => deserializer.DeserializeAsync(ReadOnlyMemory <byte> .Empty, isNull: true, new SerializationContext(MessageComponentType.Value, testTopic)).Result);

            Assert.Equal("Cannot deserialize null to a Value Type", exception.InnerException.Message);
        }
        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);
        }
예제 #8
0
        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);
        }
예제 #9
0
        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);
        }
예제 #10
0
        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);
        }
예제 #11
0
        public async Task Handle(byte[] message, ISchemaRegistryClient schemaRegistry, CancellationToken token)
        {
            var deserializer     = new AvroDeserializer <TEvent>(schemaRegistry);
            var mySpecificRecord = await deserializer.DeserializeAsync(message, false, new SerializationContext());

            using (var scope = _services.CreateScope())
            {
                var serviceBus = scope.ServiceProvider.GetRequiredService <IServiceBus>();

                await serviceBus.PublishAsync(mySpecificRecord, token);
            }
        }
예제 #12
0
        public static async void RunAvro(
            [KafkaTrigger("my-confluent-oss-cp-kafka-headless.default.svc:9092", "cars", ConsumerGroup = "cars-saver")] byte[][] kafkaEvents,
            [Kafka(BrokerList = "my-confluent-oss-cp-kafka-headless.default.svc:9092")] IAsyncCollector <KafkaEventData <string, string> > events)
        {
            foreach (var kafkaEvent in kafkaEvents)
            {
                var car = await Deserializer.DeserializeAsync(kafkaEvent, false, SerializationContext.Empty);

                var test = $"Custom deserialised car from batch: {JsonConvert.SerializeObject(car)}";

                await events.AddAsync(new KafkaEventData <string, string>()
                {
                    Value = test,
                    Key   = Guid.NewGuid().ToString(),
                    Topic = "cars.reply"
                });
            }
        }
        private static string DeserializeAvro(byte[] data, string topic, bool isKey)
        {
            if (_cachedSchemaRegistryClient == null)
            {
                _cachedSchemaRegistryClient = new CachedSchemaRegistryClient(new SchemaRegistryConfig {
                    Url = _schemaRegistryUrl
                });
            }
            if (_avroDeserializer == null)
            {
                _avroDeserializer = new AvroDeserializer <GenericRecord>(_cachedSchemaRegistryClient);
            }

            var deserializationTask = _avroDeserializer.DeserializeAsync(data, false, new SerializationContext(isKey ? MessageComponentType.Key : MessageComponentType.Value, topic));

            deserializationTask.Wait();

            return(deserializationTask.Result.ToOutputString());
        }
        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);
        }
예제 #15
0
        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);
        }
        private async Task <TValue?> DeserializeAsync <TValue>(
            byte[]?message,
            MessageComponentType componentType,
            MessageSerializationContext context)
            where TValue : class
        {
            if (message == null)
            {
                return(null);
            }

            var avroDeserializer = new AvroDeserializer <TValue>(
                SchemaRegistryClientFactory.GetClient(SchemaRegistryConfig),
                AvroSerializerConfig);

            var confluentSerializationContext = GetConfluentSerializationContext(componentType, context);

            return(await avroDeserializer.DeserializeAsync(
                       new ReadOnlyMemory <byte>(message),
                       false,
                       confluentSerializationContext)
                   .ConfigureAwait(false));
        }
예제 #17
0
        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);
        }
예제 #18
0
        public void Poco_Deserialize()
        {
            var deserializer = new AvroDeserializer <Dictionary <string, string> >(schemaRegistryClient);

            Assert.Throws <System.InvalidOperationException>(() => deserializer.DeserializeAsync(new System.ReadOnlyMemory <byte>(new byte[] { 1, 2, 3 }), false, new SerializationContext(MessageComponentType.Key, testTopic)).GetAwaiter().GetResult());
        }
 public async Task <GenericRecord> DeserializeAsync(byte[] bts, string topic) =>
 await _avroDeserializer.DeserializeAsync(bts, false, new SerializationContext(MessageComponentType.Value, topic));