我是Kafka的新手,并且能够以某种方式管理kafka Avro的消费者和生产者。生产者正在生产该消息,并且我已经成功地将其传递给了消费者。这是我的生产者代码段:
static async void AvroProducer()
{
string bootstrapServers = "localhost:9092";
string schemaRegistryUrl = "Production163:8081";
string topicName = "player";
string groupName = "avro-generic-example-group";
var s = (RecordSchema)RecordSchema.Parse(
@"{
""namespace"": ""Confluent.Kafka.Examples.AvroSpecific"",
""type"": ""record"",
""name"": ""User"",
""fields"": [
{""name"": ""name"", ""type"": ""string""},
{""name"": ""favorite_number"", ""type"": [""int"", ""null""]},
{""name"": ""favorite_color"", ""type"": [""string"", ""null""]}
]
}"
);
using (var schemaRegistry = new CachedSchemaRegistryClient(new SchemaRegistryConfig { SchemaRegistryUrl = schemaRegistryUrl }))
using (var producer =
new ProducerBuilder<string, GenericRecord>(new ProducerConfig { BootstrapServers = bootstrapServers })
.SetKeySerializer(new AsyncAvroSerializer<string>(schemaRegistry))
.SetValueSerializer(new AsyncAvroSerializer<GenericRecord>(schemaRegistry))
.Build())
{
Console.WriteLine($"{producer.Name} producing on {topicName}. Enter user names, q to exit.");
int i = 0;
string text;
while ((text = Console.ReadLine()) != "q")
{
var record = new GenericRecord(s);
record.Add("name", text);
record.Add("favorite_number", i++);
record.Add("favorite_color", "blue");
producer.ProduceAsync(topicName, new Message<string, GenericRecord> { Key = text, Value = record })
.ContinueWith(task => task.IsFaulted
? $"error producing message: {task.Exception.Message}"
: $"produced to: {task.Result.TopicPartitionOffset}");
}
}
Console.ReadLine();
}
如您在上面的代码中看到的,我正在使用一种记录方案,但是我正在尝试这种模式:
//this is the new schema try
var s = (RecordSchema)RecordSchema.Parse(
@"{
""type"": ""record"",
""name"": ""TestingMsg"",
""doc"": ""Sample"",
""fields"": [
{
""name"": ""key"",
""type"": ""string""
},
{
""name"": ""Time"",
""type"": ""long""
},
{
""name"": ""sourceSeconds"",
""type"": ""long""
},
{
""name"": ""serverT"",
""type"": ""long""
},
{
""name"": ""statusCode"",
""type"": ""int""
}
]
}"
);
我正在尝试使用的新邮件,但是由于我没有在消费者中收到消息,因此无法使用。 这里是消费者:
void KafkaReader(CancellationToken cancellationToken)
{
Debug.Log("kafka reader started. . .");
// Set up your Kafka connection here.
while (_keepThreadRunning)
{
using (CachedSchemaRegistryClient schemaRegistry = new CachedSchemaRegistryClient(new SchemaRegistryConfig { SchemaRegistryUrl = schemaRegistryUrl }))
using (IConsumer<string, GenericRecord> consumer = new ConsumerBuilder<string, GenericRecord>(new ConsumerConfig { BootstrapServers = bootstrapServers, GroupId = groupName })
//using (IConsumer<string, GenericRecord> consumer = new ConsumerBuilder<string, GenericRecord>(new ConsumerConfig { BootstrapServers = bootstrapServers})
.SetKeyDeserializer(new AsyncAvroDeserializer<string>(schemaRegistry).AsSyncOverAsync())
.SetValueDeserializer(new AsyncAvroDeserializer<GenericRecord>(schemaRegistry).AsSyncOverAsync())
.SetErrorHandler((_, e) => Debug.Log($"Error: {e.Reason}"))
.Build())
{
Debug.Log("subscribe" );
consumer.Subscribe(topicName);
while (true)
{
ConsumeResult<string, GenericRecord> consumeResult = consumer.Consume(cancellationToken);//TimeSpan.FromMilliseconds(50000)//new TimeSpan(0,0,1)
_stringsReceived.Enqueue(consumeResult.Value.ToString());
if (consumeResult != null)
{
Debug.Log($"Key: {consumeResult.Message.Key}\nValue: {consumeResult.Value}");
}
else
{
Debug.Log("consumer Result is null");
}
//yield return new WaitForSeconds(1);
}
}
}
GetComponent<KafkaServerConfigUI>().KafkaDisconnected();
// Disconnect and clean up your connection here.
}
请记住,我只是使用批处理文件运行默认的Apache Kafka注册表。
D:\ApachKafka\confluent\confluent-5.2.1\bin\windows\schema-registry-start.bat D:\ApachKafka\confluent\confluent-5.2.1\etc\schema-registry\schema-registry.properties
我做错了什么?我需要将架构注册到任何地方吗?
答案 0 :(得分:1)
仅在悬赏之后,我就确定了问题所在(以下是Kafka的新手答案)。要进行任何更改或使用新的架构,您必须注册该架构。我错过了这个东西,因此没有在消费者中得到消息。这是short python script,可帮助您注册架构。
使用script,您必须提供Schema Registry的URL(以http://开头,而不仅仅是主机名和端口),应为其注册模式的主题以及路径到模式。
这是我注册架构的方式
答案 1 :(得分:0)
我知道您对此有答案。我的建议是避免为每个架构更新运行python脚本。
您可以使用schema-registry-ui。
简而言之,schema-registry-ui提供了 -探索和搜索模式 -Avro Evolution兼容性检查 -新模式注册 -Avro +表架构视图 -显示CURL命令
git clone https://github.com/Landoop/schema-registry-ui.git
cd schema-registry-ui
npm install -g bower
npm install
http-server .
http://schema-registry-ui.landoop.com/
或者提供了docker镜像。如果可以选择许可证,请尝试使用融合控制中心,该中心提供了更多选项。