यह मेरा पहली बार का उपयोग कर काफ्का और मैं योजना बना रहा हूँ का उपयोग करने के लिए काफ्का के साथ .नेट
मैं जानना चाहता था अगर मैं भेज सकते हैं JSON एक संदेश के रूप में जब मैं एक घटना का निर्माण
मैं कर रहा हूँ ट्यूटोरियल का पालन: https://developer.confluent.io/get-started/dotnet/#build-producer
इसके अलावा, वहाँ एक तरीका है के लिए है कि मूल्य के लिए मैप किया जा करने के लिए एक मॉडल इतना है कि मूल्य/json संरचना हमेशा के लिए बंधे है कि मॉडल
तो, उदाहरण के लिए: अगर मैं चाहता हूँ मेरी json मूल्य किया जा करने के लिए
{
"customerName":"anything",
"eventType":"one-of-three-enums",
"columnsChanged": "string value or something"
}
उदाहरण के अधिकांश कि मैं पा सकते हैं इस तरह हैं:
using Confluent.Kafka;
using System;
using Microsoft.Extensions.Configuration;
class Producer {
static void Main(string[] args)
{
if (args.Length != 1) {
Console.WriteLine("Please provide the configuration file path as a command line argument");
}
IConfiguration configuration = new ConfigurationBuilder()
.AddIniFile(args[0])
.Build();
const string topic = "purchases";
string[] users = { "eabara", "jsmith", "sgarcia", "jbernard", "htanaka", "awalther" };
string[] items = { "book", "alarm clock", "t-shirts", "gift card", "batteries" };
using (var producer = new ProducerBuilder<string, string>(
configuration.AsEnumerable()).Build())
{
var numProduced = 0;
const int numMessages = 10;
for (int i = 0; i < numMessages; ++i)
{
Random rnd = new Random();
var user = users[rnd.Next(users.Length)];
var item = items[rnd.Next(items.Length)];
producer.Produce(topic, new Message<string, string> { Key = user, Value = item },
(deliveryReport) =>
{
if (deliveryReport.Error.Code != ErrorCode.NoError) {
Console.WriteLine($"Failed to deliver message: {deliveryReport.Error.Reason}");
}
else {
Console.WriteLine($"Produced event to topic {topic}: key = {user,-10} value = {item}");
numProduced += 1;
}
});
}
producer.Flush(TimeSpan.FromSeconds(10));
Console.WriteLine($"{numProduced} messages were produced to topic {topic}");
}
}
}
मैं होगा की तरह आइटम किया जा करने के लिए एक वर्ग में json संरचना है ।