コード例 #1
0
ファイル: GenericSchemaTest.cs プロジェクト: eaba/SharpPulsar
        public void TestGenericTopic()
        {
            var schema = AvroSchema <ComplexGenericData> .Of(typeof(ComplexGenericData));

            var genericSchema = GenericAvroSchema.Of(schema.SchemaInfo);

            _output.WriteLine(schema.SchemaInfo.SchemaDefinition);
            var pBuilder = new ProducerConfigBuilder <IGenericRecord>()
                           .Topic(_topic);
            var producer = _client.NewProducer(genericSchema, pBuilder);

            const int messageCount = 10;

            for (var i = 0; i < messageCount; i++)
            {
                var dataForWriter = new GenericRecord((Avro.RecordSchema)genericSchema.AvroSchema);
                dataForWriter.Add("Feature", "Education");
                dataForWriter.Add("StringData", new Dictionary <string, string> {
                    { "Index", i.ToString() }, { "FirstName", "Ebere" }, { "LastName", "Abanonu" }
                });
                dataForWriter.Add("ComplexData", ToBytes(new ComplexData {
                    ProductId = i, Point = i * 2, Sales = i * 2 * 5
                }));
                var record  = new GenericAvroRecord(null, genericSchema.AvroSchema, genericSchema.Fields, dataForWriter);
                var receipt = producer.Send(record);
                _output.WriteLine(JsonSerializer.Serialize(receipt, new JsonSerializerOptions {
                    WriteIndented = true
                }));
            }

            var messageReceived = 0;
            var builder         = new ConsumerConfigBuilder <IGenericRecord>()
                                  .Topic(_topic)
                                  .ForceTopicCreation(true)
                                  .SubscriptionName($"generic_sub");
            var consumer = _client.NewConsumer(ISchema <object> .AutoConsume(), builder);

            Thread.Sleep(TimeSpan.FromSeconds(5));
            for (var i = 0; i < messageCount; ++i)
            {
                var m = consumer.Receive();
                Assert.NotNull(m);
                var receivedMessage = m.Value;
                var feature         = receivedMessage.GetField("Feature").ToString();
                var strinData       = (Dictionary <string, object>)receivedMessage.GetField("StringData");
                var complexData     = FromBytes <ComplexData>((byte[])receivedMessage.GetField("ComplexData"));
                _output.WriteLine(feature);
                _output.WriteLine(JsonSerializer.Serialize(strinData, new JsonSerializerOptions {
                    WriteIndented = true
                }));
                _output.WriteLine(JsonSerializer.Serialize(complexData, new JsonSerializerOptions {
                    WriteIndented = true
                }));
                messageReceived++;
                consumer.Acknowledge(m);
            }

            Assert.Equal(10, messageReceived);
            producer.Close();
            consumer.Close();
        }