简体   繁体   English

在 KAFKA 中生成和使用 JSON

[英]Producing and consuming JSON in KAFKA

We are going to deploy Apache Kafka 2.10 on our project and communicate via JSON objects between producer and consumer.我们将在我们的项目上部署 Apache Kafka 2.10,并在生产者和消费者之间通过 JSON 对象进行通信。

So far I suppose I need to:到目前为止,我想我需要:

  1. Implement a custom serializer to convert JSON into byte array实现自定义序列化器将 JSON 转换为字节数组
  2. implement a custom deserializer to convert byte array into JSON object实现自定义反序列化器将字节数组转换为 JSON 对象
  3. Produce the message产生消息
  4. Read the message in Consumer class读取 Consumer 类中的消息

Regarding first point, I believe it should be something like this:关于第一点,我认为应该是这样的:

@Override
public byte[] serialize(String topic, T data) {
    if (data == null)
        return null;
    try {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsBytes(data);
    } catch (Exception e) {
        throw new SerializationException("Error serializing JSON message", e);
    }
}

Where T data could be passed as string "{\\"key\\" : \\"value\\"}" .其中T data可以作为字符串"{\\"key\\" : \\"value\\"}"传递。

However by now there are problems with 2-4 points.然而,现在有 2-4 分的问题。 I tried this in custom deserializer:我在自定义解串器中试过这个:

@Override
public JsonNode deserialize(String topic, byte[] bytes) {
    if (bytes == null)
        return null;

    JsonNode data;
    try {
        objectMapper = new ObjectMapper();
        data = objectMapper.readTree(bytes);
    } catch (Exception e) {
        throw new SerializationException(e);
    }
    return data;
}

and in my Consumer I tried:在我的消费者中,我尝试过:

    KafkaConsumer<String, TextNode> consumer = new KafkaConsumer<String, TextNode>(messageConsumer.properties);
    consumer.subscribe(Arrays.asList(messageConsumer.topicName));
    int i = 0;
    while (true) {
        ConsumerRecords<String, TextNode> records = consumer.poll(100);
        for (ConsumerRecord<String, TextNode> record : records) {
            System.out.printf("offset = %d, key = %s, value = %s\n", record.offset(), record.key(), record.value().asText());
        }
    }

I thought this will produce a proper original json string, but all I got invoking record.value().asText() was some hash string "IntcImtleVwiIDogXCJ2YWx1ZVwiIH0i" .我认为这会产生一个正确的原始 json 字符串,但我调用record.value().asText()只是一些哈希字符串"IntcImtleVwiIDogXCJ2YWx1ZVwiIH0i"

Any advice or example of communicating via JSON in kafka would be greatly appreciated.在 kafka 中通过 JSON 进行通信的任何建议或示例将不胜感激。

I recommend you to use UTF-8 encoding as string JSON serializer:我建议您使用 UTF-8 编码作为字符串 JSON 序列化程序:
1. Producer gets the data as a JSON string ( "{\\"key\\" : \\"value\\"}" ) 1. Producer 获取数据为 JSON 字符串( "{\\"key\\" : \\"value\\"}"
2. Producer serialize the JSON string to bytes using UTF-8 ( jsonString.getBytes(StandardCharsets.UTF_8); ) 2. Producer 使用 UTF-8 ( jsonString.getBytes(StandardCharsets.UTF_8); ) 将 JSON 字符串序列化为字节
3. Producer sends this bytes to Kafka 4. Consumer reading the bytes from Kafka 5. Consumer deserializing the bytes to JSON string using UTF-8 ( new String(consumedByteArray, StandardCharsets.UTF_8); ) 3. 生产者将此字节发送到 Kafka 4. 消费者从 Kafka 读取字节 5. 消费者使用 UTF-8 将字节反序列化为 JSON 字符串( new String(consumedByteArray, StandardCharsets.UTF_8);
6. Consumer doing whatever it needs to with the JSON string 6. 消费者对 JSON 字符串做任何需要做的事情

I intentionally didn't use your code so the flow will be understandable, I think you can apply this example to your project very easily :)我故意没有使用你的代码,所以流程是可以理解的,我认为你可以很容易地将这个例子应用到你的项目中 :)

There a built-in JsonSerializer with Apache Kafka. Apache Kafka 有一个内置的 JsonSerializer。 I'm not sure what 2.10 means for a version but the current version 0.10.0.1 definitely can do the serialization for you.我不确定 2.10 对某个版本意味着什么,但当前版本 0.10.0.1 绝对可以为您进行序列化。 Just look for the JsonSerializer class.只需查找 JsonSerializer 类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM