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

后端 未结 5 859
忘掉有多难
忘掉有多难 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:16

    By now, the ConsoleProducer seems to support writing keyed messages to the topic. Kafka will use the hash of the key to distribute the message into partitions, at least with the default behaviour.

    Currently, the default separator is \t, so entering key[\t]message will distribute it amongst partitions:

    key1    a-message
    

    The separator can be changed by providing the key.separator configuration, for example:

    kafka-console-producer --broker-list localhost:9092,localhost:9093 \
      --topic mytopic --property key.separator=,
    

    Send messages like this:

    key2,another-message
    

    I have tested this with the default tab and a custom seperator successfuly. The messages were distributed to two separate partitions.

提交回复
热议问题