简体   繁体   English

将消息从一个 Kafka 主题复制到另一个 Kafka 主题

[英]Replicating messages from one Kafka topic to another kafka topic

I want to make a flow from a Kafka cluster/topic in thr prod cluster into another Kafka cluster in the dev environment for scalability and regrrssion testing.我想从 thr prod 集群中的 Kafka 集群/主题流到开发环境中的另一个 Kafka 集群,以进行可扩展性和 regrrssion 测试。

For the duck-tape solution, I cascade a Kafka consumer and producer, but my instinct tells me that there should be a better way.对于duck-tape解决方案,我级联了Kafka消费者和生产者,但我的直觉告诉我应该有更好的方法。 But, I couldn't find any good solution yet.但是,我还没有找到任何好的解决方案。 Can anyone help me?任何人都可以帮助我吗?

If you want to replicate data from one cluster to another then there is one kafka tool called MirrorMaker .如果您想将数据从一个集群复制到另一个集群,那么有一个名为MirrorMaker kafka 工具。

Kafka comes with a tool for mirroring data between Kafka clusters. Kafka 附带了一个用于在 Kafka 集群之间镜像数据的工具。 The tool reads from a source cluster and writes to a destination cluster.该工具从源集群读取并写入目标集群。 Data will be read from topics in the source cluster and written to a topic with the same name in the destination cluster.数据将从源集群中的主题读取并写入目标集群中的同名主题。

Here is syntax to run MirrorMaker tool:以下是运行MirrorMaker工具的语法:

bin/kafka-run-class.sh kafka.tools.MirrorMaker
       --consumer.config consumer.properties
       --producer.config producer.properties --whitelist my-topic

You can find this script in kafka installation directory.您可以在 kafka 安装目录中找到此脚本。 Here you need to provide consumer.properties of your source cluster and producer.properties of your destination cluster .在这里,您需要提供source cluster consumer.propertiesdestination cluster producer.properties You can whitelist which topics should be mirrored through --whitelist option.您可以通过--whitelist选项将哪些主题列入白名单。

You can find more information about Mirroring data between clusters您可以找到有关在集群之间镜像数据的更多信息

Note: MirrorMaker copies data into same topic_name in destination cluster as source cluster注意: MirrorMaker 将数据复制到destination clustersource cluster相同的topic_name

While mirror makes works perfect for across the cluster solution, however, for same cluster your ducktap solution is not bad as MirrorMaker assumes you are pulling from one cluster to another cluster.虽然mirror 非常适合跨集群解决方案,但是,对于同一个集群,您的ducktap 解决方案还不错,因为MirrorMaker 假设您是从一个集群拉到另一个集群。

So a solution where you simply want to copy data between different topics in the same cluster, kafkacat is your friend.因此,您只想在同一集群中的不同主题之间复制数据的解决方案,kafkacat 是您的朋友。

export BOOTSTRAP_SERVERS=localhost:9096
export SOURCE_TOPIC=source_topic
export TARGET_TOPIC=target_topic

kafkacat -C -b $BOOTSTRAP_SERVERS -o beginning -e -t $SOURCE_TOPIC  | kafkacat -P -b $BOOTSTRAP_SERVERS  -t $TARGET_TOPIC

Kafka is basically a messaging queue, therefore it has a passive behavior: something has to put messages into it ( producer ), and something has to pull messages from it ( consumer ). Kafka 基本上是一个消息队列,因此它具有被动行为:必须将消息放入其中( producer ),并且必须其中提取消息( consumer )。

If you want to make a sort of pipeline between two kafka topics, so that messages from one topic will go automatically to the other topic, you'll need some code which will have the properties of consumer from the first topic and a producer to the second topic.如果您想在两个 kafka 主题之间建立某种管道,以便来自一个主题的消息将自动转到另一个主题,您将需要一些代码,这些代码将具有从第一个主题和生产者到生产者的属性第二个话题。

Depending on your programming language, you can choose between some ready-to-go well documented producer and/or consumer solutions.根据您的编程语言,您可以在一些现成的、有据可查的生产者和/或消费者解决方案之间进行选择。

For more sophisticated cases you can check out Apache Storm , etc.对于更复杂的情况,您可以查看Apache Storm等。

And if you need to copy messages from one topic into another topic, maybe with some additional logic or transformation, you can also use Kafka Streams .如果您需要将消息从一个主题复制到另一个主题,也许需要一些额外的逻辑或转换,您也可以使用Kafka Streams

https://docs.confluent.io/current/streams/index.html https://docs.confluent.io/current/streams/index.html

And examples和例子

https://github.com/confluentinc/kafka-streams-examples/blob/5.4.0-post/src/main/java/io/confluent/examples/streams/WordCountLambdaExample.java https://github.com/confluentinc/kafka-streams-examples/blob/5.4.0-post/src/main/java/io/confluent/examples/streams/WordCountLambdaExample.java

Alternatively, check MirrorMaker .或者,检查MirrorMaker

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM