从代码中的模式注册表中获取生产者的avro模式

时间:2018-02-23 12:41:05

标签: apache-kafka schema avro confluent producer

Confluent提供以下示例:

String key = "key1";
String userSchema = "{\"type\":\"record\"," +
                "\"name\":\"myrecord\"," +
                "\"fields\":[{\"name\":\"f1\",\"type\":\"string\"}]}";
Schema.Parser parser = new Schema.Parser();
Schema schema = parser.parse(userSchema);
GenericRecord avroRecord = new GenericData.Record(schema);
avroRecord.put("f1", "value1");
ProducerRecord<Object, Object> record = new ProducerRecord<>("topic1", key, avroRecord);
try {
  producer.send(record);
} catch(SerializationException e) {
  // may need to do something with it
}

有没有办法直接从架构注册表(如果已经存在)获取生产者的userSchema,而不是硬编码?

1 个答案:

答案 0 :(得分:0)

您可以使用Maven plugin下载架构。

如果你真的想在运行时获取模式,还有REST API