带有AVRO的Apache kafka,架构ID在消息中的何处? [英] Apache kafka with AVRO, where in the message does the schema id go?

查看:127
本文介绍了带有AVRO的Apache kafka,架构ID在消息中的何处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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", //一些更多的字段, 有效载荷":{ "name":"xyz", 年龄":"23" } }

{ "componentName": "ABC", //some more fields, "payload": { "name" : "xyz", "age": "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.这是编码器串行器还是其他一些东西?我们是否还需要在发送的消息中嵌入模式?编码器将如何选择模式?

which said that there is encoder which takes a schema and finds it's schema id from schema registry. Is this encoder serializer or something different? Do we need to embed schema also in the message we are sending? how will encoder pick the schema?

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?

推荐答案

我今天正好在看这个,看来模式ID被编码为消息的第一个字节.

I was looking at exactly this today and it seems the schema id is encoded as the first bytes of the message.

当键或值反序列化时(

When the key or value is de-serialized (code):

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

序列化时(代码):

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

这篇关于带有AVRO的Apache kafka,架构ID在消息中的何处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆