单个Kafka制作人可以针对多个主题产生消息吗? [英] Can single Kafka producer produce messages to multiple topics and how?

查看:801
本文介绍了单个Kafka制作人可以针对多个主题产生消息吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在探索Kafka,目前我正在使用一个producer和一个主题来生成消息,并且该消息被一个Consumer使用.非常简单.

I am just exploring Kafka, currently i am using One producer and One topic to produce messages and it is consumed by one Consumer. very simple.

我正在阅读Kafka页面,new Producer API is thread-safe和共享单个实例将提高性能.

I was reading the Kafka page, the new Producer API is thread-safe and sharing single instance will improve the performance.

这是否意味着我可以使用单个生产者将消息发布到多个主题?

Does it mean i can use single Producer to publish messages to multiple topics?

推荐答案

我从来没有尝试过,但是我想可以.由于生产者和发送记录的代码是(从这里 https://kafka.apache.org/090/javadoc/index.html?org/apache/kafka/clients/producer/KafkaProducer.html ):

Never tried it myself, but I guess you can. Since the code for producer and sending the record is (from here https://kafka.apache.org/090/javadoc/index.html?org/apache/kafka/clients/producer/KafkaProducer.html):

Producer<String, String> producer = new KafkaProducer<>(props);
 for(int i = 0; i < 100; i++)
     producer.send(new ProducerRecord<String, String>("my-topic", Integer.toString(i), Integer.toString(i)));

所以,我想,如果您只是在ProducerRecord中编写不同的主题,那应该是不可能的.

So, I guess, if you just write different topics in the ProducerRecord, than it should be possible.

此外,这里 http://kafka.apache.org/081/documentation.html #producerapi 明确表示您可以使用send(List<KeyedMessage<K,V>> messages)方法写入多个主题.

Also, here http://kafka.apache.org/081/documentation.html#producerapi it explicitly says that you can use a method send(List<KeyedMessage<K,V>> messages) to write into multiple topics.

这篇关于单个Kafka制作人可以针对多个主题产生消息吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆