Go 在围棋中使用卡夫卡Avro消息

Go 在围棋中使用卡夫卡Avro消息,go,apache-kafka,avro,kafka-consumer-api,Go,Apache Kafka,Avro,Kafka Consumer Api,我试图使用avro格式的卡夫卡消息,但我无法在Go中将avro中的消息解码为json 我正在使用Confluent平台(3.0.1)。例如,我生成的avro消息如下: kafka-avro-console-producer --broker-list localhost:9092 --topic test --property value.schema='{"type":"record","name":"myrecord","fields":[{"name":"f1","type":"strin

我试图使用avro格式的卡夫卡消息,但我无法在Go中将avro中的消息解码为json

我正在使用Confluent平台(3.0.1)。例如,我生成的avro消息如下:

kafka-avro-console-producer --broker-list localhost:9092 --topic test --property value.schema='{"type":"record","name":"myrecord","fields":[{"name":"f1","type":"string"}]}'
{"f1":"message1"}
{"f1":"message2"}
现在我用go Kafka图书馆:sarama来消费信息。纯文本消息工作正常。必须对Avro信息进行解码。我发现了不同的libs:github.com/linkedin/goavro、github.com/elodina/go-avro

但是在解码之后,我得到了一个没有值的json(两个LIB):

戈瓦罗:

avroSchema := `
{"type":"record","name":"myrecord","fields":[{"name":"f1","type":"string"}]}
`
codec, err := goavro.NewCodec(avroSchema)
if err != nil {
    log.Fatal(err)
}
bb := bytes.NewBuffer(msg.Value)
decoded, err := codec.Decode(bb)
log.Println(fmt.Sprintf("%s", decoded))
go avro:

schema := avro.MustParseSchema(avroSchema)
reader := avro.NewGenericDatumReader()
reader.SetSchema(schema)
decoder := avro.NewBinaryDecoder(msg.Value)
decodedRecord := avro.NewGenericRecord(schema)
log.Println(decodedRecord.String())
msg=sarama.ConsumerMessage刚刚发现(通过比较二进制avro消息),我必须删除消息字节数组的前5个元素-现在一切正常:)

也许有人能解释一下原因

这只有在使用合流模式注册表时才真正有用

“也许有人可以解释原因”的答案是:当您使用schema registry serializer生成消息时,Kafka客户端的消息序列化程序会在消息的第一个字节中添加模式id,如果您使用cline反序列化程序,它会检查第一个字节以检测模式id并从模式注册表中获取它(如果需要),但当您不使用标准Kafka客户端的反序列化程序时,您需要跳过第一个字节。
schema := avro.MustParseSchema(avroSchema)
reader := avro.NewGenericDatumReader()
reader.SetSchema(schema)
decoder := avro.NewBinaryDecoder(msg.Value)
decodedRecord := avro.NewGenericRecord(schema)
log.Println(decodedRecord.String())
message = msg.Value[5:]