我想将多个kafka主题数据放入单个kafka主题

问题描述 投票:0回答:1

假设我有 5 个 kafka 主题,每个主题都从不同的来源接收数据。

kafka-topic-1 的数据为:

This is the first log of the kafka. It contains detail of mac.

kafka-topic-2 的数据为:

This is the second log of the kafka. It contains detail of julie.

kafka-topic-3 的数据为:

This is the third log of the kafka. It contains detail of sam.

kafka-topic-4 的数据为:

This is the fourth log of the kafka. It contains detail of harry.

kafka-topic-5 的数据为:

This is the fifth log of the kafka. It contains detail of hermione.

目前的情况是,我曾经调用这些kafka主题来发送数据以进行进一步处理,但现在我希望所有主题的数据应该立即直接转到一个主题,而不是一一调用这些主题,并且稍后的处理可以使用该单个 kafka 主题完成。

新主题的数据为:

This is the first log of the kafka. It contains detail of mac.

This is the second log of the kafka. It contains detail of julie.

This is the third log of the kafka. It contains detail of sam.

This is the fourth log of the kafka. It contains detail of harry.

This is the fifth log of the kafka. It contains detail of hermione.

我只想使用这个kafka主题来接收数据。我想这样做是因为每次我需要确保命名正确,而管理这么多主题会让事情变得混乱。为了简单起见,我希望所有卡夫卡主题数据仅进入单个卡夫卡主题。请告诉我解决方案。

apache-kafka kafka-consumer-api
1个回答
0
投票

你不需要一个主题。订阅它们全部,不使用循环,而是使用正则表达式模式。

Java 示例

final Pattern topics = Pattern.compile("kafka-topic-[1-5]");
consumer.subscribe(topics);

注意:跨主题永远无法保证顺序

© www.soinside.com 2019 - 2024. All rights reserved.