简体   繁体   English

如何从 bash 将一些消息从一个 kafka 主题复制到另一个主题?

[英]How copy some message from one kafka topic to another from bash?

pls help请帮助

We have 2 kafka topic.我们有 2 个 kafka 主题。 I want copy 10 message from beginning from topic1 to topic2.我想从 topic1 开始复制 10 条消息到 topic2。

I`m try do it with kafka-console-consumer and kafka-console-producer我正在尝试使用kafka-console-consumerkafka-console-producer

First i save 10 message from topic1 to some directory:首先,我将 10 条消息从 topic1 保存到某个目录:

for (( i=1; i<=10; i++ )); do bin/kafka-console-consumer.sh --bootstrap-server 1.1.2.3:9092 --group CONSUMER1 --topic TOPIC1 --max-messages 1 > /tmp/_topic/$i.msg; done;

then i try with kafka-console-producer send it to topic2:然后我尝试使用 kafka-console-producer 将它发送到 topic2:

for (( i=1; i<=10; i++ )); do  bin/kafka-console-producer.sh --broker-list 1.1.2.4:9092 --topic TOPIC2 < /tmp/_topic/$i.msg; done;

And i got error - my service Can't deserialize data.我收到错误消息 - 我的服务无法反序列化数据。 My question is:我的问题是:

  1. does my solution will work?我的解决方案有效吗?
  2. Why i can reciev this error?为什么我能收到这个错误?
  3. What is best way to copy message from one topic to another once?一次将消息从一个主题复制到另一个主题的最佳方法是什么?

UPD: How i`m solve this problem (thanks: Robin Moffatt): I using kafka-mirror and this jar: https://github.com/opencore/mirrormaker_topic_rename with this i can copy message from one topic kafka to another on one cluster UPD:我如何解决这个问题(感谢:Robin Moffatt):我使用 kafka-mirror 和这个 jar: https ://github.com/opencore/mirrormaker_topic_rename 有了这个我可以将消息从一个主题 kafka 复制到另一个主题簇

You can do this with kafkacat :你可以用kafkacat做到这kafkacat

kafkacat -b localhost:9092 -C -t source-topic -K: -e -o beginning -c10 | \
kafkacat -b localhost:9092 -P -t target-topic -K: 
  • | redirects the output of the first kafkacat (which is a -C consumer) into the input of the second kafkacat (which is a -P producer)将第一个 kafkacat(它是-C消费者)的输出重定向到第二个 kafkacat(它是-P生产者)的输入
  • -c10 means just consume 10 messages -c10表示只消耗 10 条消息
  • -o beginning means start at the beginning of the topic. -o beginning表示从主题的开头开始。

Note that this won't work if you've got binary data (eg Avro).请注意,如果您有二进制数据(例如 Avro),这将不起作用。 To properly do this use something like Replicator or MirrorMaker2 and a ByteArrayConverter.要正确执行此操作,请使用 Replicator 或 MirrorMaker2 和 ByteArrayConverter 之类的东西。

Ref: https://rmoff.net/2019/09/29/copying-data-between-kafka-clusters-with-kafkacat/参考: https : //rmoff.net/2019/09/29/copying-data-between-kafka-clusters-with-kafkacat/

For anyone interested in doing this for binary kafka messages, I wrote a simple tool ( kpipe ) to do that.对于任何有兴趣对二进制kafka 消息执行此操作的人,我编写了一个简单的工具 ( kpipe )来执行此操作。 Hope it helps someone希望它能帮助别人

暂无
暂无

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

相关问题 如何将 Kafka 消息从一个主题发送到另一个主题? - How to send Kafka message from one topic to another topic? 如何修改保存在一个kafka主题中的来自Twitter API的消息并将其发送到另一个kafka主题 - How to modify message from Twitter API saved in one kafka topic and send it to another kafka topic 如何将所有消息从一个 kafka 主题(avro 格式)复制和转换到另一个主题(json 格式) - How to copy and transform all messages from one kafka topic (in avro format) to another topic (in json format) 如何将主题从 kafka 集群复制到另一个 kafka 集群? - How to copy a topic from a kafka cluster to another kafka cluster? 使用偏移量/时间戳将消息从一个 Kafka 主题复制到另一个主题 - Copy messages from one Kafka topic to another using offsets/timestamps 如何使用一些过滤器使用来自 Kafka 主题的消息? - How to consume message from Kafka topic using some filter? 将消息从一个 Kafka 主题复制到另一个 Kafka 主题 - Replicating messages from one Kafka topic to another kafka topic 从 Kafka 的主题中删除一条消息 - Removing one message from a topic in Kafka 如何根据来自另一个主题的响应读取来自 Kafka 主题的消息 - how to read message from Kafka topic based on response from another topic 如何创建从一个主题读取并写入另一个主题的 Kafka Stream - How to create Kafka Stream that reads from one topic and writes to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM