简体   繁体   English

Kafka 产生多值消息

[英]Kafka produce multiple value message

I am new to working with Kafka .我是Kafka新手。

Am I able to send a message with multiple values?我可以发送具有多个值的消息吗? ie, I have this example here which is producing a message with a random number, however I would like to add a timestamp for each produced record.即,我在这里有这个示例,它生成一条带有随机数的消息,但是我想为每个生成的记录添加一个时间戳。 What is the standard way to do this?执行此操作的标准方法是什么?

    while(true){
        for(int key=0; key < 10000; key++){
            Random rand = new Random();
            int  n = rand.nextInt(50) + 1;

            SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
            Date date = new Date();
            ProducerRecord<String, String> producerRecord = new ProducerRecord<String, String>("java-topic", Integer.toString(1),Integer.toString(n));
            producer.send(producerRecord);
            Thread.sleep(10000);
        }
        //producer.close();
    }

Internally Kafka broker (server) only stores a byte array. Kafka 代理(服务器)内部只存储一个字节数组。 Thus You will have to encode the your message to contain all values.因此,您必须对您的消息进行编码以包含所有值。
A popular method to do this is to use JSON encoding in message一种流行的方法是在消息中使用 JSON 编码

int numbers[] = {1,2,3};
String msg = String.format("{\"numbers\": %s, \"timestamp\": \"%s\"}", 
                java.util.Arrays.toString(numbers), timestamp);
ProducerRecord<String, String> producerRecord = new ProducerRecord<String, String>(
       "java-topic", msg);

Note a timestamp is added by the kafka server when it receives a message which can be seen in ConsumerRecord.timestamp()请注意,当 kafka 服务器收到可以在ConsumerRecord.timestamp() 中看到的消息时,它会添加一个时间戳

Thanks for the help!!谢谢您的帮助!! Output answer输出答案

while(true){
    for(int key=0; key < 10000; key++){
        Random rand = new Random();
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

        int  n = rand.nextInt(50) + 1;
        String today = formatter.format(date);
        String msg = String.format("{\"numbers\": %s, \"timestamp\": \"%s\"}", n, today);

        ProducerRecord<String, String> producerRecord = new ProducerRecord<String, String>(
                "java-topic", Integer.toString(1), msg);
        producer.send(producerRecord);
        Thread.sleep(1000);
    }
    //producer.close();
}

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

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