Example #1
0
        public void SendMessage()
        {
            var config = new ProducerConfig
            {
                BootstrapServers = "132.232.27.116:9092",
                Acks             = Acks.All
            };

            using (var kafkaProducer = new KafkaProducer(config, "topic-b"))
            {
                var result = kafkaProducer.Produce <object>("a", new { name = "Öí°Ë½ä3" }, 1);
            }
        }
        static void Main(string[] args)
        {
            #region Config

            //var schemaRegistryUrl = "http://*****:*****@"..\..\..\wwwroot\schema.json";

            var bootstrapServers = "localhost:9092";
            var topic            = "quick-start";    // "stream-topic";
            var groupId          = "consumer-group"; // "simple-consumer";

            int partition = 0;
            int offset    = 0;

            var recordConfig = new RecordConfig(schemaRegistryUrl);

            #endregion // Config

            #region Kafka Consumer

            var kafkaConsumer = new KafkaConsumer(
                bootstrapServers,
                recordConfig,
                topic,
                groupId,
                partition,
                offset,
                (key, value, dt) =>
            {
                Console.WriteLine($"Consumed Object:\nkey = {key}");
                foreach (var field in value.Schema.Fields)
                {
                    Console.WriteLine($"  {field.Name} = {value[field.Name]}");
                }
            },
                e => Console.WriteLine(e))
                                .StartConsuming();

            #endregion // Kafka Consumer

            #region Create Kafka Producer

            var kafkaProducer = new KafkaProducer(
                bootstrapServers,
                recordConfig,
                topic,
                //partition,
                //offset,
                e => Console.WriteLine(e));

            #endregion // Create Kafka Producer

            var count = 0;
            var timer = new Timer(_ =>
            {
                var lstTuple = new List <Tuple <string, GenericRecord> >();
                for (var i = 0; i < 10; i++)
                {
                    count++;

                    #region Create GenericRecord Object

                    var gr = new GenericRecord(recordConfig.RecordSchema);
                    gr.Add("SEQUENCE", count);
                    gr.Add("ID", count);
                    gr.Add("CategoryID", count);
                    gr.Add("YouTubeCategoryTypeID", count);
                    gr.Add("CreationTime", DateTime.Now.Ticks);

                    lstTuple.Add(new Tuple <string, GenericRecord>($"{count}", gr));

                    #endregion // Create GenericRecord Object
                }

                kafkaProducer.Send(lstTuple.ToArray());
            },
                                  null, 0, 5000);

            Console.WriteLine("Press any key to quit...");
            Console.ReadKey();

            timer.Dispose();
            kafkaProducer.Dispose();
            kafkaConsumer.Dispose();
        }