简体   繁体   English

如何使用java(spring)将文本对象消息生成到kafka主题中?

[英]How to produce a json object message into kafka topic using java(spring)?

I want to produce a message into kafka topic. 我想向kafka主题发送消息。 That message should have this pattern: 该消息应具有以下模式:

   {"targetFileInfo":{"path":"2018-05-07-10/row01-small-01.txt.ready"}}

I know that is a json pattern, so how can i convert that json in String? 我知道这是一个json模式,那么如何在String中转换该json?

I use a maven project, so which dependencies are needed to use 我使用一个Maven项目,因此需要使用哪些依赖项

 String stringData = JSON.stringify({"targetFileInfo":{"path":"2018-05-07-10/row01-small-01.txt.ready"}});

So I think it is better don't convert Json to string and send indeed that massage into kafka topic. 因此,我认为最好不要将Json转换为字符串,并且不要将按摩发送到kafka主题中。

My Code is like that, it can send a String but i don't know how i can modify my code to send the massage above. 我的代码就是这样,它可以发送一个字符串,但是我不知道如何修改我的代码以发送上述消息。 maybe you can help me. 也许你可以帮我。

 Producer<String, String> producer = null;

    Properties props = new Properties();
    props.put("bootstrap.servers", "localhost:9092");
    props.put("acks", "all");
    props.put("retries", 0);
    props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

    producer = new KafkaProducer<>(props);
    String msg = "welcome";
    producer.send(new ProducerRecord<String, String>("event", msg));

    producer.close();

As per the comment you need to send JsonNode as message on kafka. 根据评论,您需要在kafka JsonNode作为消息发送。 Write a custom Serializer / Deserializer for the same. 为此编写一个自定义的序列化器/反序列化器。

import java.io.IOException;
import java.util.Map;

import org.apache.kafka.common.serialization.Deserializer;
import org.apache.kafka.common.serialization.Serializer;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonNodeSerDes implements Serializer<JsonNode>, Deserializer<JsonNode> {

    private ObjectMapper mapper = new ObjectMapper();

    @Override
    public byte[] serialize(String topic, JsonNode data) {

        try {
            return mapper.writeValueAsBytes(data);
        } catch (JsonProcessingException e) {
            return new byte[0];
        }
    }

    @Override
    public JsonNode deserialize(String topic, byte[] data) {

        try {
            return mapper.readValue(data, JsonNode.class);
        } catch (IOException e) {
            return null;
        }
    }

    @Override
    public void configure(Map<String, ?> configs, boolean isKey) {
    }

    @Override
    public void close() {
    }
}

I wrote serializer / deserializer in the same class. 我在同一个类中编写了序列化器/反序列化器。 You can separate them in two class (one implementing Serializer , another implementing Deserializer ). 您可以将它们分为两个类(一个实现Serializer ,另一个实现Deserializer )。

While creating KafkaProducer you need to provide "value.serializer" config and "value.deserializer" config for KafkaConsumer . 在创建KafkaProducer您需要提供“value.serializer”配置和“value.deserializer”配置KafkaConsumer

External Dependencies used: 使用的外部依赖项:

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.8.8</version>
</dependency>

That solved my problem: 那解决了我的问题:

 Producer<String, String> producer = null;

    Properties props = new Properties();
    props.put("bootstrap.servers", "localhost:9092");
    props.put("acks", "all");
    props.put("retries", 0);
    props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

    producer = new KafkaProducer<>(props);

    try {
        producer = new KafkaProducer<String, String>(props);
    } catch (Exception e) {
        e.printStackTrace();
    }
    blobStorageChecker = new BlobStorageChecker();
    String folder = blobStorageChecker.getCurrentDateUTC();
    String msg = "{\"targetFileInfo\":{\"path\":\"test/"+folder+"row01-small.txt\"},\"sourceFileInfo\":{\"lastModifiedTime\":1525437960000,\"file\":\"/row01-small-01.txt\",\"filename\":\"/data/row01/row01-small.txt\",\"size\":19728,\"remoteUri\":\"ftp://waws-prod-am2-191.ftp.net/data/orsted-real/inbound/row01\",\"contentEncoding\":\"\",\"contentType\":\"\"}}";
    ProducerRecord<String, String> record = new ProducerRecord<String, String>("event-orsted-v1", null, msg);
    if (producer != null) {
        try {
            Future<RecordMetadata> future = producer.send(record);
            RecordMetadata metadata = future.get();
        } catch (Exception e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
    }
    producer.close();

暂无
暂无

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

相关问题 如何使用Java修改一个kafka主题的消息并发送到另一个kafka主题? - how to modify message of one kafka topic and send to another kafka topic using java? 在Java中将Json Object发送到Kafka主题 - Sending Json Object to kafka topic in java java.lang.ClassCastException:类模型 - 在主题中将 JSON 对象作为消息发送时,springboot kafka 集成出错 - java.lang.ClassCastException: class model - Error in springboot kafka integration while sending JSON object as message in topic Java Spring引导kafka从带有偏移量的主题中删除消息 - Java Spring boot kafka delete message from topic with offset 如何使用Spring和Hibernate生成消耗JSON对象的JSON响应 - How to produce json response that consumes json object using spring and Hibernate 如何在使用 Spring Cloud Stream 发布到 Kafka 主题之前向消息添加标头 - How to add headers to a message before posting to Kafka Topic using Spring Cloud Stream 无法使用 ZeroCode 在 Kafka 主题上发布 json 消息 - Cannot publish json message on Kafka topic using ZeroCode 如果第一个集群使用 java 出现故障,如何将消息推送到第二个 kafka 集群主题 - How to push message to second kafka cluster topic if first cluster goes down using java 如何在 Spring Boot 中反序列化 Kafka 主题中的 Json 字符串 - How to deserialize Json string from Kafka topic in spring boot 如何确定Kafka消息的主题 - How to determine topic for a Kafka message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM