Example #1
0
        public virtual void TestSeparatedGetProducerDataAssigned()
        {
            var fooSchema = AvroSchema <SchemaTestUtils.Foo> .Of(ISchemaDefinition <SchemaTestUtils.Foo> .Builder().WithPojo(typeof(SchemaTestUtils.Foo)).Build());

            var barSchema = AvroSchema <SchemaTestUtils.Bar> .Of(ISchemaDefinition <SchemaTestUtils.Bar> .Builder().WithPojo(typeof(SchemaTestUtils.Bar)).Build());

            var keyValueSchema = ISchema <int> .KeyValue(fooSchema, barSchema, KeyValueEncodingType.SEPARATED);

            var foo = new SchemaTestUtils.Foo
            {
                Field1 = "field1",
                Field2 = "field2",
                Field3 = 3
            };
            var bar = new SchemaTestUtils.Bar
            {
                Field1 = true
            };

            // Check kv.encoding.type SPRAERATE
            var encodeBytes = keyValueSchema.Encode(new KeyValue <SchemaTestUtils.Foo, SchemaTestUtils.Bar>(foo, bar));
            var builder     = new MessageMetadata();

            builder.ProducerName           = "separated";
            builder.PartitionKey           = Convert.ToBase64String(fooSchema.Encode(foo));
            builder.PartitionKeyB64Encoded = true;
            var msg = Message <KeyValue <SchemaTestUtils.Foo, SchemaTestUtils.Bar> > .Create(builder, new ReadOnlySequence <byte>(encodeBytes), keyValueSchema);

            var keyValue = msg.Value;

            Assert.Equal(keyValue.Key, foo);
            Assert.Equal(keyValue.Value, bar);
            Assert.True(builder.ShouldSerializePartitionKey());
        }
Example #2
0
        public virtual async Task ReaderSourceTaggedTest()
        {
            var topic = $"reader-topics-{Guid.NewGuid()}";
            var ids   = PublishMessages(topic, 50);
            var start = MessageIdUtils.GetOffset(ids.First());
            var end   = MessageIdUtils.GetOffset(ids.Last());

            var conf = new ReaderConfigBuilder <DataOp>().Topic(topic);

            var reader = _pulsarSystem.EventSource("public", "default", topic, start, end, "http://127.0.0.1:8080")
                         .Reader(_clientConfigurationData, conf, AvroSchema <DataOp> .Of(typeof(DataOp)))
                         .SourceMethod()
                         .CurrentTaggedEvents(new Messages.Consumer.Tag("twitter", "mestical"));

            //let leave some time to wire everything up
            await Task.Delay(TimeSpan.FromSeconds(20));

            var receivedCount = 0;

            while (receivedCount == 0)
            {
                await foreach (var response in reader.CurrentEvents(TimeSpan.FromSeconds(5)))
                {
                    receivedCount++;
                    _output.WriteLine(JsonSerializer.Serialize(response, new JsonSerializerOptions {
                        WriteIndented = true
                    }));
                }
                await Task.Delay(TimeSpan.FromSeconds(1));
            }

            Assert.True(receivedCount > 0);
        }
Example #3
0
        public virtual void TestAllowNullBytesSchemaEncodeAndDecode()
        {
            var fooAvroSchema = AvroSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).Build());

            var barAvroSchema = AvroSchema <Bar> .Of(ISchemaDefinition <Bar> .Builder().WithPojo(typeof(Bar)).Build());

            var bar = new Bar
            {
                Field1 = true
            };

            var foo = new Foo
            {
                Field1 = "field1",
                Field2 = "field2",
                Field3 = "3",
                Field4 = bar,
                Color  = Color.RED
            };

            var fooBytes = fooAvroSchema.Encode(foo);
            var barBytes = barAvroSchema.Encode(bar);

            var encodeBytes = ISchema <object> .KvBytes().Encode(new KeyValue <byte[], byte[]>(fooBytes, barBytes));

            var decodeKV = ISchema <object> .KvBytes().Decode(encodeBytes);

            var fooBack = fooAvroSchema.Decode(decodeKV.Key);
            var barBack = barAvroSchema.Decode(decodeKV.Value);

            Assert.True(foo.Equals(fooBack));
            Assert.True(bar.Equals(barBack));
        }
Example #4
0
        public virtual void TestFillParametersToSchemainfo()
        {
            var fooSchema = AvroSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).Build());

            var barSchema = AvroSchema <Bar> .Of(ISchemaDefinition <Bar> .Builder().WithPojo(typeof(Bar)).Build());

            fooSchema.SchemaInfo.Name = "foo";
            fooSchema.SchemaInfo.Type = SchemaType.AVRO;
            IDictionary <string, string> keyProperties = new Dictionary <string, string>
            {
                ["foo.key1"] = "value",
                ["foo.key2"] = "value"
            };

            fooSchema.SchemaInfo.Properties = keyProperties;
            barSchema.SchemaInfo.Name       = "bar";
            barSchema.SchemaInfo.Type       = SchemaType.AVRO;
            IDictionary <string, string> valueProperties = new Dictionary <string, string>
            {
                ["bar.key"] = "key"
            };

            barSchema.SchemaInfo.Properties = valueProperties;
            var keyValueSchema1 = ISchema <object> .KeyValue(fooSchema, barSchema);

            Assert.Equal("foo", keyValueSchema1.SchemaInfo.Properties["key.schema.name"]);
            Assert.Equal(SchemaType.AVRO.ToString(), keyValueSchema1.SchemaInfo.Properties["key.schema.type"]);
            Assert.Equal(@"{""foo.key1"":""value"",""foo.key2"":""value""}", keyValueSchema1.SchemaInfo.Properties["key.schema.properties"]);
            Assert.Equal("bar", keyValueSchema1.SchemaInfo.Properties["value.schema.name"]);
            Assert.Equal(SchemaType.AVRO.ToString(), keyValueSchema1.SchemaInfo.Properties["value.schema.type"]);
            Assert.Equal(@"{""bar.key"":""key""}", keyValueSchema1.SchemaInfo.Properties["value.schema.properties"]);
        }
Example #5
0
        public virtual void TestInlineKeyValueEncodingTypeSchemaEncodeAndDecode()
        {
            var fooSchema = AvroSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).Build());

            var barSchema = AvroSchema <Bar> .Of(ISchemaDefinition <Bar> .Builder().WithPojo(typeof(Bar)).Build());

            var keyValueSchema = ISchema <object> .KeyValue(fooSchema, barSchema, KeyValueEncodingType.INLINE);


            var bar = new Bar
            {
                Field1 = true
            };

            var foo = new Foo
            {
                Field1 = "field1",
                Field2 = "field2",
                Field3 = "3",
                Field4 = bar,
                Color  = Color.RED
            };

            // Check kv.encoding.type INLINE
            var encodeBytes = keyValueSchema.Encode(new KeyValue <Foo, Bar>(foo, bar));

            Assert.True(encodeBytes.Length > 0);
            var keyValue = keyValueSchema.Decode(encodeBytes);
            var fooBack  = keyValue.Key;
            var barBack  = keyValue.Value;

            Assert.True(foo.Equals(fooBack));
            Assert.True(bar.Equals(barBack));
        }
Example #6
0
        private ISet <MessageId> PublishMessages(string topic, int count)
        {
            var ids     = new HashSet <MessageId>();
            var builder = new ProducerConfigBuilder <DataOp>()
                          .Topic(topic);
            var producer = _client.NewProducer(AvroSchema <DataOp> .Of(typeof(DataOp)), builder);

            for (var i = 0; i < count; i++)
            {
                var       key = "key" + i;
                MessageId id  = null;
                if (i % 2 == 0)
                {
                    id = producer.NewMessage().Key(key).Property("twitter", "mestical").Value(new DataOp {
                        Text = "my-event-message-" + i, EventTime = DateTimeHelper.CurrentUnixTimeMillis()
                    }).Send();
                }
                else
                {
                    id = producer.NewMessage().Key(key).Value(new DataOp {
                        Text = "my-event-message-" + i, EventTime = DateTimeHelper.CurrentUnixTimeMillis()
                    }).Send();
                }
                ids.Add(id);
            }
            return(ids);
        }
Example #7
0
        public GenericAvroReaderTest()
        {
            fooSchema = AvroSchema <SchemaTestUtils.Foo> .Of(typeof(SchemaTestUtils.Foo));

            fooV2Schema = AvroSchema <SchemaTestUtils.FooV2> .Of(typeof(SchemaTestUtils.FooV2));

            fooSchemaNotNull = AvroSchema <SchemaTestUtils.Foo> .Of(ISchemaDefinition <SchemaTestUtils.Foo> .Builder().WithAlwaysAllowNull(false).WithPojo(typeof(SchemaTestUtils.Foo)).Build());

            fooOffsetSchema = AvroSchema <SchemaTestUtils.Foo> .Of(typeof(SchemaTestUtils.Foo));

            fooOffsetSchema.SchemaInfo.Properties.Add(GenericAvroSchema.OFFSET_PROP, "5");
            var prop = fooOffsetSchema.AvroSchema.GetProperty(GenericAvroSchema.OFFSET_PROP);

            foo = new SchemaTestUtils.Foo
            {
                Field1          = "foo1",
                Field2          = "bar1",
                Field4          = new SchemaTestUtils.Bar(),
                FieldUnableNull = "notNull"
            };

            fooV2 = new SchemaTestUtils.FooV2
            {
                Field1 = ("foo1"),
                Field3 = (10)
            };
        }
Example #8
0
        public virtual void TestSchemaDefinition()
        {
            var schema1 = typeof(DefaultStruct).GetSchema();
            var schema2 = AvroSchema <StructWithAnnotations> .Of(typeof(StructWithAnnotations));

            var schemaDef1 = schema1.ToString();
            var schemaDef2 = Encoding.UTF8.GetString(schema2.SchemaInfo.Schema);

            Assert.NotEqual(schemaDef1, schemaDef2);

            var schema3 = AvroSchema <StructWithAnnotations> .Of(ISchemaDefinition <StructWithAnnotations> .Builder().WithJsonDef(schemaDef1).Build());

            var schemaDef3 = Encoding.UTF8.GetString(schema3.SchemaInfo.Schema);

            Assert.True(schemaDef1.Contains("DefaultStruct") && schemaDef3.Contains("DefaultStruct"));
            Assert.NotEqual(schemaDef2, schemaDef3);

            var @struct = new StructWithAnnotations
            {
                Field1 = 5678
            };

            // schema2 is using the schema generated from POJO,
            // it allows field2 to be nullable, and field3 has default value.
            schema2.Encode(@struct);
            // schema3 is using the schema passed in, which doesn't allow nullable
            schema3.Encode(@struct);
        }
Example #9
0
        public virtual void TestDefaultGetProducerDataAssigned()
        {
            var fooSchema = AvroSchema <SchemaTestUtils.Foo> .Of(ISchemaDefinition <SchemaTestUtils.Foo> .Builder().WithPojo(typeof(SchemaTestUtils.Foo)).Build());

            var barSchema = AvroSchema <SchemaTestUtils.Bar> .Of(ISchemaDefinition <SchemaTestUtils.Bar> .Builder().WithPojo(typeof(SchemaTestUtils.Bar)).Build());

            var keyValueSchema = ISchema <object> .KeyValue(fooSchema, barSchema);

            var foo = new SchemaTestUtils.Foo
            {
                Field1 = "field1",
                Field2 = "field2",
                Field3 = 3
            };
            var bar = new SchemaTestUtils.Bar
            {
                Field1 = true
            };

            // // Check kv.encoding.type default, not set value
            var encodeBytes = keyValueSchema.Encode(new KeyValue <SchemaTestUtils.Foo, SchemaTestUtils.Bar>(foo, bar));
            var builder     = new MessageMetadata();

            builder.ProducerName = "default";

            var msg = Message <KeyValue <SchemaTestUtils.Foo, SchemaTestUtils.Bar> > .Create(builder, new ReadOnlySequence <byte>(encodeBytes), keyValueSchema);

            var keyValue = msg.Value;

            Assert.Equal(keyValue.Key, foo);
            Assert.Equal(keyValue.Value, bar);
            Assert.False(builder.ShouldSerializePartitionKey());
        }
Example #10
0
        public void ProduceAndConsume()
        {
            var record1 = AvroSchema <SimpleRecord> .Of(typeof(SimpleRecord));

            var consumerBuilder = new ConsumerConfigBuilder <SimpleRecord>()
                                  .Topic(_topic)
                                  .ConsumerName("avroUpgradeSchema1")
                                  .SubscriptionName("test-sub");
            var consumer = _client.NewConsumer(record1, consumerBuilder);

            var producerBuilder = new ProducerConfigBuilder <SimpleRecord>()
                                  .Topic(_topic)
                                  .ProducerName("avroUpgradeSchema1");
            var producer = _client.NewProducer(record1, producerBuilder);

            producer.NewMessage()
            .Value(new SimpleRecord {
                Name = "Ebere Abanonu", Age = int.MaxValue
            })
            .Send();

            Thread.Sleep(TimeSpan.FromSeconds(10));
            var message = consumer.Receive();

            Assert.NotNull(message);
            consumer.Acknowledge(message);
            consumer.Unsubscribe();

            var record2 = AvroSchema <SimpleRecord2> .Of(typeof(SimpleRecord2));

            var consumerBuilder2 = new ConsumerConfigBuilder <SimpleRecord2>()
                                   .Topic(_topic)
                                   .ConsumerName("avroUpgradeSchema2")
                                   .SubscriptionName("test-sub");
            var consumer1 = _client.NewConsumer(record2, consumerBuilder2);

            var producerBuilder2 = new ProducerConfigBuilder <SimpleRecord2>()
                                   .Topic(_topic)
                                   .ProducerName("avroUpgradeSchema2");
            var producer2 = _client.NewProducer(record2, producerBuilder2);

            producer2.NewMessage()
            .Value(new SimpleRecord2 {
                Name = "Ebere", Age = int.MaxValue, Surname = "Abanonu"
            })
            .Send();

            Thread.Sleep(TimeSpan.FromSeconds(10));
            var msg = consumer1.Receive();

            Assert.NotNull(msg);
            consumer1.Acknowledge(msg);
            consumer1.Unsubscribe();

            producer.Close();
            producer2.Close();
            consumer.Close();
            consumer1.Close();
        }
Example #11
0
        public GenericAvroSchemaTest()
        {
            var avroFooV2Schema = AvroSchema <SchemaTestUtils.FooV2> .Of(ISchemaDefinition <SchemaTestUtils.FooV2> .Builder().WithAlwaysAllowNull(false).WithPojo(typeof(SchemaTestUtils.FooV2)).Build());

            //var avroFooSchema = AvroSchema<SchemaTestUtils.Foo>.Of(ISchemaDefinition<SchemaTestUtils.Foo>.Builder().WithAlwaysAllowNull(false).WithPojo(typeof(SchemaTestUtils.Foo)).Build());
            writerSchema = new GenericAvroSchema(avroFooV2Schema.SchemaInfo);
            readerSchema = new GenericAvroSchema(avroFooV2Schema.SchemaInfo);
        }
        public void Start()
        {
            var clientConfig = new PulsarClientConfigBuilder()
                               .ServiceUrl(_pulsarSettings.ServiceUrl)
                               .ConnectionsPerBroker(1)
                               .UseProxy(_pulsarSettings.UseProxy)
                               .OperationTimeout(_pulsarSettings.OperationTimeout)
                               .AllowTlsInsecureConnection(false)
                               .ProxyServiceUrl(_pulsarSettings.ProxyServiceUrl, ProxyProtocol.SNI)
                               .Authentication(new AuthenticationDisabled())
                               .ClientConfigurationData;

            _pulsarSystem = PulsarSystem.GetInstance(clientConfig);
            _schema       = AvroSchema.Of(typeof(Echo.Common.Echo));
            _producer     = CreateProducer();
        }
Example #13
0
        public void TestDateTimeDecimalLogicalType()
        {
            var avroSchema = AvroSchema <LogicalMessage> .Of(ISchemaDefinition <LogicalMessage> .Builder().WithPojo(typeof(LogicalMessage)).WithJSR310ConversionEnabled(true).Build());

            var logMsg = new LogicalMessage {
                Schema = Avro.Schema.Parse(avroSchema.SchemaInfo.SchemaDefinition), CreatedTime = DateTime.Now, DayOfWeek = "Saturday", Size = new AvroDecimal(102.65M)
            };

            var bytes1 = avroSchema.Encode(logMsg);

            Assert.True(bytes1.Length > 0);

            var msg = avroSchema.Decode(bytes1);

            Assert.NotNull(msg);
            Assert.True(msg.Size == 102.65M);
        }
Example #14
0
        private ISet <string> PublishMessages(string topic, int count)
        {
            ISet <string> keys    = new HashSet <string>();
            var           builder = new ProducerConfigBuilder <DataOp>()
                                    .Topic(topic);
            var producer = _client.NewProducer(AvroSchema <DataOp> .Of(typeof(DataOp)), builder);

            for (var i = 0; i < count; i++)
            {
                var key = "key" + i;
                producer.NewMessage().Key(key).Value(new DataOp {
                    Text = "my-sql-message-" + i
                }).Send();
                keys.Add(key);
            }
            return(keys);
        }
Example #15
0
        public virtual void TestSeparatedKeyValueEncodingTypeSchemaEncodeAndDecode()
        {
            var fooSchema = AvroSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).Build());

            var barSchema = AvroSchema <Bar> .Of(ISchemaDefinition <Bar> .Builder().WithPojo(typeof(Bar)).Build());


            var keyValueSchema = ISchema <object> .KeyValue(fooSchema, barSchema, KeyValueEncodingType.SEPARATED);

            var bar = new Bar
            {
                Field1 = true
            };

            var foo = new Foo
            {
                Field1 = "field1",
                Field2 = "field2",
                Field3 = "3",
                Field4 = bar,
                Color  = Color.RED
            };

            // Check kv.encoding.type SEPARATED
            var encodeBytes = keyValueSchema.Encode(new KeyValue <Foo, Bar>(foo, bar));

            Assert.True(encodeBytes.Length > 0);
            try
            {
                keyValueSchema.Decode(encodeBytes);
                Assert.True(false, "This method cannot be used under this SEPARATED encoding type");
            }
            catch (SchemaSerializationException e)
            {
                Assert.Contains("This method cannot be used under this SEPARATED encoding type", e.Message);
            }
            var keyValue = ((KeyValueSchema <Foo, Bar>)keyValueSchema).Decode(fooSchema.Encode(foo), encodeBytes, null);
            var fooBack  = keyValue.Key;
            var barBack  = keyValue.Value;

            Assert.True(foo.Equals(fooBack));
            Assert.True(bar.Equals(barBack));
        }
Example #16
0
        public Worker(ILogger <Worker> logger, IHubContext <EchoHub> echo, PulsarSettings pulsarSettings)
        {
            _echo   = echo;
            _logger = logger;
            var clientConfig = new PulsarClientConfigBuilder()
                               .ServiceUrl(pulsarSettings.ServiceUrl)
                               .ConnectionsPerBroker(1)
                               .UseProxy(pulsarSettings.UseProxy)
                               .OperationTimeout(pulsarSettings.OperationTimeout)
                               .AllowTlsInsecureConnection(false)
                               .ProxyServiceUrl(pulsarSettings.ProxyServiceUrl, ProxyProtocol.SNI)
                               .Authentication(new AuthenticationDisabled())
                               .ClientConfigurationData;

            _topic        = pulsarSettings.Topic;
            _pulsarSystem = PulsarSystem.GetInstance(clientConfig);
            _schema       = AvroSchema.Of(typeof(Echo.Common.Echo));
            _consumer     = Consumer(pulsarSettings);
        }
Example #17
0
        public void TestAvroSchemaUserDefinedReadAndWriter()
        {
            var reader           = new JsonReader <Foo>();
            var writer           = new JsonWriter <Foo>();
            var schemaDefinition = ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Bar)).WithSchemaReader(reader).WithSchemaWriter(writer).Build();

            var schema = AvroSchema <Foo> .Of(schemaDefinition);

            var foo = new Foo();

            foo.Color = Color.RED;
            var field1 = "test";

            foo.Field1 = field1;
            var encoded = schema.Encode(foo);

            foo = schema.Decode(encoded);
            Assert.Equal(Color.RED, foo.Color);
            Assert.Equal(field1, foo.Field1);
        }
Example #18
0
        public void TestLogicalType()
        {
            var avroSchema = AvroSchema <SchemaLogicalType> .Of(ISchemaDefinition <SchemaLogicalType> .Builder().WithPojo(typeof(SchemaLogicalType)).WithJSR310ConversionEnabled(true).Build());

            var schemaLogicalType = new SchemaLogicalType
            {
                TimestampMicros = DateTimeHelper.CurrentUnixTimeMillis() * 1000,
                TimestampMillis = DateTime.Parse("2019-03-26T04:39:58.469Z").Ticks,
                Decimal         = 12.34D,
                Date            = DateTimeOffset.Now.ToUnixTimeMilliseconds(),
                TimeMicros      = DateTimeHelper.CurrentUnixTimeMillis() * 1000,
                TimeMillis      = (DateTime.Now - DateTime.Today).Ticks
            };

            var bytes1 = avroSchema.Encode(schemaLogicalType);

            Assert.True(bytes1.Length > 0);

            var object1 = avroSchema.Decode(bytes1);

            Assert.True(schemaLogicalType.Equals(object1));
        }
Example #19
0
        public virtual void TestAvroSchemaCreate()
        {
            var fooSchema = AvroSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).WithAlwaysAllowNull(false).Build());

            var barSchema = AvroSchema <Bar> .Of(ISchemaDefinition <Bar> .Builder().WithPojo(typeof(Bar)).WithAlwaysAllowNull(false).Build());

            var keyValueSchema1 = ISchema <object> .KeyValue(fooSchema, barSchema);

            var keyValueSchema2 = ISchema <object> .KeyValue(AvroSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).WithAlwaysAllowNull(false).Build()), AvroSchema <Bar> .Of(ISchemaDefinition <Bar> .Builder().WithPojo(typeof(Bar)).WithAlwaysAllowNull(false).Build()));

            Assert.Equal(keyValueSchema1.SchemaInfo.Type, SchemaType.KeyValue);
            Assert.Equal(keyValueSchema2.SchemaInfo.Type, SchemaType.KeyValue);

            Assert.Equal(((KeyValueSchema <Foo, Bar>)keyValueSchema1).KeySchema.SchemaInfo.Type, SchemaType.AVRO);
            Assert.Equal(((KeyValueSchema <Foo, Bar>)keyValueSchema1).ValueSchema.SchemaInfo.Type, SchemaType.AVRO);
            Assert.Equal(((KeyValueSchema <Foo, Bar>)keyValueSchema2).KeySchema.SchemaInfo.Type, SchemaType.AVRO);
            Assert.Equal(((KeyValueSchema <Foo, Bar>)keyValueSchema2).ValueSchema.SchemaInfo.Type, SchemaType.AVRO);

            var schemaInfo1 = Encoding.UTF8.GetString(keyValueSchema1.SchemaInfo.Schema);
            var schemaInfo2 = Encoding.UTF8.GetString(keyValueSchema2.SchemaInfo.Schema);

            Assert.Equal(schemaInfo1, schemaInfo2);
        }
Example #20
0
        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();
        }
Example #21
0
        private void PlainAvroProducer(string topic)
        {
            var jsonSchem = AvroSchema <JournalEntry> .Of(typeof(JournalEntry));

            var builder = new ConsumerConfigBuilder <JournalEntry>()
                          .Topic(topic)
                          .SubscriptionName($"my-subscriber-name-{DateTimeHelper.CurrentUnixTimeMillis()}")
                          .AckTimeout(TimeSpan.FromMilliseconds(20000))
                          .ForceTopicCreation(true)
                          .AcknowledgmentGroupTime(0);
            var consumer       = _client.NewConsumer(jsonSchem, builder);
            var producerConfig = new ProducerConfigBuilder <JournalEntry>()
                                 .ProducerName(topic.Split("/").Last())
                                 .Topic(topic)
                                 .Schema(jsonSchem)
                                 .SendTimeout(10000);

            var producer = _client.NewProducer(jsonSchem, producerConfig);

            for (var i = 0; i < 10; i++)
            {
                var student = new Students
                {
                    Name   = $"[{i}] Ebere: {DateTimeOffset.Now.ToUnixTimeMilliseconds()} - presto-ed {DateTime.Now.ToString(CultureInfo.InvariantCulture)}",
                    Age    = 202 + i,
                    School = "Akka-Pulsar university"
                };
                var journal = new JournalEntry
                {
                    Id            = $"[{i}]Ebere: {DateTimeOffset.Now.ToUnixTimeMilliseconds()}",
                    PersistenceId = "sampleActor",
                    IsDeleted     = false,
                    Ordering      = 0,
                    Payload       = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(student)),
                    SequenceNr    = 0,
                    Tags          = "root"
                };
                var metadata = new Dictionary <string, string>
                {
                    ["Key"]        = "Single",
                    ["Properties"] = JsonSerializer.Serialize(new Dictionary <string, string> {
                        { "Tick", DateTime.Now.Ticks.ToString() }
                    }, new JsonSerializerOptions {
                        WriteIndented = true
                    })
                };
                var id = producer.NewMessage().Properties(metadata).Value(journal).Send();
            }
            Thread.Sleep(TimeSpan.FromSeconds(5));
            for (var i = 0; i < 10; i++)
            {
                var msg = consumer.Receive();
                if (msg != null)
                {
                    var receivedMessage = msg.Value;
                    _output.WriteLine(JsonSerializer.Serialize(receivedMessage, new JsonSerializerOptions {
                        WriteIndented = true
                    }));
                }
            }
        }
Example #22
0
 public static ISchema <T> NewAvroSchema <T>(ISchemaDefinition <T> schemaDefinition)
 {
     return(AvroSchema <T> .Of(schemaDefinition));
 }