How to produce messages to selected partition using kafka-console-producer?

后端 未结 5 857
忘掉有多难
忘掉有多难 2021-02-01 06:40

According to the Kafka documentation:

The producer is responsible for choosing which message to assign to which partition within the topic.

5条回答
  •  鱼传尺愫
    2021-02-01 07:10

    Update: This answer was correct on 2014, but more current versions of Kafka can produce key/value pairs via the console producer. See the below answers

    kafka-console-producer.sh doesn't support producing messages to a particular partition out of the box.

    However it should be pretty straightforward to update the script to pass an extra parameter for partition Id and then handle it in a custom partitioner as described in the post by @Chiron in a modified version of kafka.tools.ConsoleProducer class.

    Take a look at the source code at:

    https://apache.googlesource.com/kafka/+/refs/heads/trunk/bin/kafka-console-producer.sh https://apache.googlesource.com/kafka/+/refs/heads/trunk/core/src/main/scala/kafka/tools/ConsoleProducer.scala

提交回复
热议问题