在架构注册表中存储Avro架构

时间:2016-09-14 18:03:06

标签: apache-kafka avro

我正在使用Confluent的JDBC连接器以Avro格式将数据发送到Kafka。我需要将此架构存储在架构注册表中,但我不确定它接受的格式。我已经阅读了文档here,但它并没有多提及。

我试过这个(将Avro输出并粘贴到 - 一个int和一个字符串字段中):

curl -X POST -H "Content-Type: application/vnd.schemaregistry.v1+json"     --data '{"type":"struct","fields":[{"type":"int64","optional":true,"field":"id"},{"type":"string","optional":true,"field":"serial"}],"optional":false,"name":"test"}' http://localhost:8081/subjects/view/versions

但我收到错误:{" error_code":422," message":"无法识别的字段:输入"}

2 个答案:

答案 0 :(得分:9)

您作为JSON提供的架构应该以'架构'键。您提供的实际架构将是架构键的

所以你的请求应该是这样的:

 curl -X POST -H "Content-Type: application/vnd.schemaregistry.v1+json"     --data '{"schema" : "{\"type\":\"string\",\"fields\":[{\"type\":\"int64\",\"optional\":true,\"field\":\"id\"},{\"type\":\"string\",\"optional\":true,\"field\":\"serial\"}],\"optional\":false,\"name\":\"test\"}"}' http://localhost:8081/subjects/view/versions

我对该命令进行了另外两项更改:

  • 我已在架构键的值内转义了每个双引号。
  • 我已将struct数据结构更改为string。我不确定为什么它不采取复杂的结构。

查看他们如何为架构here建模,以获取文档中描述的第一个POST请求。

答案 1 :(得分:0)

首先,您需要提前存储架构吗?如果将JDBC连接器与Avro转换器(它是架构注册表包的一部分)一起使用,JDBC连接器将从数据库中找出该表的架构并为您注册。您需要在KafkaConnect配置文件中指定转换器。您可以将此作为示例:https://github.com/confluentinc/schema-registry/blob/master/config/connect-avro-standalone.properties

如果你真的想自己注册架构,那么shell命令就有可能出现问题 - 在shell中转义JSON很棘手。我在Chrome中安装了Advanced Rest Client,并使用它来处理架构注册表和KafkaConnect的REST API。