[英]How to read GenericRecord specific data in confluent kafka C#
This is my simple code snippet which try to to read the Avro generic record form consumer: 这是我的简单代码片段,它试图阅读Avro通用记录表单消费者:
using (var schemaRegistry = new CachedSchemaRegistryClient(new SchemaRegistryConfig { SchemaRegistryUrl = schemaRegistryUrl }))
using (var consumer = new
ConsumerBuilder<string, GenericRecord>(new ConsumerConfig { BootstrapServers = bootstrapServers, GroupId = groupName })
.SetKeyDeserializer(new AsyncAvroDeserializer<string>(schemaRegistry).AsSyncOverAsync())
.SetValueDeserializer(new AsyncAvroDeserializer<GenericRecord>(schemaRegistry).AsSyncOverAsync())
.SetErrorHandler((_, e) => Console.WriteLine($"Error: {e.Reason}"))
.Build())
{
consumer.Subscribe(topicName);
try
{
while (true)
{
try
{
var consumeResult = consumer.Consume();
Console.WriteLine($"Key: {consumeResult.Message.Key}\nValue: {consumeResult.Value}");
Console.WriteLine(consumeResult.Value.Schema);
Console.WriteLine(consumeResult.Value.Schema["favorite_number"]);
}
catch (ConsumeException e)
{
Console.WriteLine($"Consume error: {e.Error.Reason}");
}
}
}
catch (OperationCanceledException)
{
// commit final offsets and leave the group.
consumer.Close();
}
}
As you can see i have can log the schema but dont know how to get its data value. 正如您所看到的,我可以记录模式,但不知道如何获取其数据值。
Console.WriteLine(consumeResult.Value.Schema);
Console.WriteLine(consumeResult.Value.Schema["favorite_number"]);
While the schema and data is like this in this object consumeResult.Value
: 虽然架构和数据在此对象中
consumeResult.Value
:
{Schema: {"type":"record","name":"User","namespace":"Confluent.Kafka.Examples.AvroSpecific","fields":[{"name":"name","type":"string"},{"name":"favorite_number","type":["int","null"]},{"name":"favorite_color","type":["string","null"]}]}, contents : { name: sfs, favorite_number: 41, favorite_color: blue, }} {Schema:{“type”:“record”,“name”:“User”,“namespace”:“Confluent.Kafka.Examples.AvroSpecific”,“fields”:[{“name”:“name”,“type” “:” 串 “},{” 名称 “:” favorite_number “ ”类型“:[ ”INT“, ”空“]},{ ”名称“: ”favorite_color“, ”类型“:[ ”字符串“,” null“]}]}, 内容 :{name:sfs,favorite_number:41,favorite_color:blue,}}
I want to read content data. 我想阅读内容数据。
the
content
is not accessiblecontent
无法访问
I don't think Value.contents
is what you want. 我认为
Value.contents
不是你想要的。 Particularly because that property is private
, as you mentioned 特别是因为你提到的那个属性是
private
The getter is defined like a dictionary - Source code getter被定义为字典 - 源代码
Try consumeResult.Value["favorite_number"]
尝试
consumeResult.Value["favorite_number"]
When you do consumeResult.Value.Schema["favorite_number"]
, you get the Field
object within the schema not the value of the field within the outer record. 当您执行
consumeResult.Value.Schema["favorite_number"]
,您将获得架构中的Field
对象,而不是外部记录中字段的值。 - Source code - 源代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.