带有AVRO的Apache kafka,架构ID中的消息在哪里?

时间:2018-02-27 11:37:14

标签: java avro

I do have a number of queries about AVRO schema.

I have read that, we need to pass a schema id and the message in the Kafka event.The body of my Kafka event is like - 

{     “componentName”:“ABC”,     //一些更多的领域,     “有效载荷”:{     “名字”:“xyz”,     “年龄”:“23”     }     }

In payload field, we provide the actual data. Here, where will I provide the schema id. I found one answer related to this at [link][1] 


  [1]: https://stackoverflow.com/questions/31204201/apache-kafka-with-avro-and-schema-repo-where-in-the-message-does-the-schema-id, 

表示有编码器采用模式并从模式注册表中找到它的模式ID。这个编码器序列化器还是不同的?我们是否还需要在我们发送的消息中嵌入架构?编码器将如何选择架构?

Do we need to explicitly register schema in schema registry as said on this [link][1] 

Also, how will we associate a schema with a topic name?

1 个答案:

答案 0 :(得分:1)

我今天正好在看这个,看来模式ID被编码为消息的前几个字节。

对键或值进行反序列化(code)时:

ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(0);
out.write(ByteBuffer.allocate(4).putInt(e).array());

序列化(code)时:

ByteBuffer e = this.getByteBuffer(payload);
int id1 = e.getInt();
Schema schema = this.schemaRegistry.getById(id1);