亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 go 中使用 Kafka Avro 消息

在 go 中使用 Kafka Avro 消息

Go
拉莫斯之舞 2022-04-20 17:43:21
我正在嘗試以 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 回答

?
弒天下

TA貢獻1818條經驗 獲得超8個贊

剛剛發現(通過比較二進制 avro 消息)我必須刪除消息字節數組的前 5 個元素 - 現在一切正常:)

message = msg.Value[5:]

也許有人可以解釋為什么


查看完整回答
反對 回復 2022-04-20
?
忽然笑

TA貢獻1806條經驗 獲得超5個贊

僅當您使用 Confluent 模式注冊表時才真正有用。



查看完整回答
反對 回復 2022-04-20
  • 2 回答
  • 0 關注
  • 279 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號