如何使用 kafka-console-producer 向选定分区生成消息? [英] How to produce messages to selected partition using kafka-console-producer?

查看:62
本文介绍了如何使用 kafka-console-producer 向选定分区生成消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Kafka 文档:

According to the Kafka documentation:

生产者负责选择将哪条消息分配给主题内的哪个分区.

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

如何使用 kafka-console-producer.sh 将消息发送到选定的分区?

How can I send messages to a selected partition using kafka-console-producer.sh?

我想在发送消息时指定某种分区 ID".

I would like to specify some sort of 'partition id' at message sending.

推荐答案

更新:这个答案在 2014 年是正确的,但是更多当前版本的 Kafka 可以通过控制台生产者生成键/值对.请参阅下面答案

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 不支持开箱即用地向特定分区生成消息.

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

但是,更新脚本以传递分区 Id 的额外参数,然后在自定义分区程序中处理它应该非常简单,如@Chiron 在 kafka.tools.ConsoleProducer 类的修改版本中的帖子中所述.

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.

查看源代码:

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

这篇关于如何使用 kafka-console-producer 向选定分区生成消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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