我正在嘗試以 avro 格式使用 Kafka 消息,但我無法在 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 消息必須被解碼。我發現了不同的庫:github.com/linkedin/goavro、github.com/elodina/go-avro但是解碼后我得到一個沒有值的json(兩個庫):{"f1":""}戈夫羅: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))繼續前進: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
- 2 回答
- 0 關注
- 279 瀏覽
添加回答
舉報
0/150
提交
取消