简体   繁体   English

Kafka 主题到多个 kafka 主题调度程序(同一集群)

[英]Kafka topic to multiple kafka topics dispatcher (same cluster)

My use-case is as follows: I have a kafka topic A with messages "logically" belonging to different "services", I don't handle neither the system sending the messages to A .我的用例如下:我有一个 kafka 主题A其中的消息“逻辑上”属于不同的“服务”,我既不处理将消息发送到A的系统。

I want to read such messages from A and dispatch them to a per-service set of topics on the same cluster (let's call them A_1, ..., A_n ), based on one column describing the service (the format is CSV-style, but it doesn't matter).我想根据描述服务的一列(格式为 CSV 样式)从A读取此类消息并将它们分派到同一集群上的一组每项服务主题(我们称它们为A_1, ..., A_n ) ,但没关系)。

The set of services is static, I don't have to handle addition/removal at the moment.服务集是静态的,我目前不必处理添加/删除。

I was hoping to use KafkaConnect to perform such task but, surprisingly, there are no Kafka source/sinks (I cannot find the tickets, but they have been rejected).我希望使用KafkaConnect来执行这样的任务,但令人惊讶的是,没有 Kafka 源/接收器(我找不到票,但它们已被拒绝)。

I have seen MirrorMaker2 but it looks like an overkill for my (simple) use-case.我见过MirrorMaker2但对于我的(简单)用例来说,它看起来有点矫枉过正。

I also know KafkaStreams but I'd rather not write and maintain code just for that.我也知道KafkaStreams但我宁愿不KafkaStreams编写和维护代码。

My question is: is there a way to achieve this topic dispatching with kafka native tools without writing a kafka-consumer/producer myself?我的问题是:有没有办法用 kafka 本地工具实现这个主题调度,而无需自己编写 kafka 消费者/生产者?

PS: if anybody thinks that MirrorMaker2 could be a good fit I am interested too, I don't know the tool very well. PS:如果有人认为MirrorMaker2很适合我,我也很感兴趣,我不太了解这个工具。

Mirror Maker is for doing ... mirroring. Mirror Maker 是用来做...镜像的。 It's useful when you want to mirror one cluster from one data center to the other with the same topics.当您希望将一个集群从一个数据中心镜像到另一个具有相同主题的集群时,这很有用。 Your use case is different.您的用例是不同的。

Kafka Connect is for syncing different systems (data from Databases for example) through Kafka topics but I don't see it for this use case either. Kafka Connect 用于通过 Kafka 主题同步不同的系统(例如来自数据库的数据),但我也没有在这个用例中看到它。

I would use a Kafka Streams application for that.我会为此使用 Kafka Streams 应用程序。

As for my knowledge, there is no straightforward way to branch incoming topic messages to a list of topics based on the incoming messages.就我所知,没有直接的方法可以根据传入的消息将传入的主题消息分支到主题列表。 You need to write custom code to achieve this.您需要编写自定义代码来实现这一点。

  1. Use Processor API Refer here使用处理器 API 请参阅此处
  2. Pass list of topics inside the Processor method在 Processor 方法中传递主题列表
  3. Use logic to identify topics need to branch使用逻辑识别需要分支的主题
  4. Use context.forward to publish a message to other topics使用 context.forward 将消息发布到其他主题

context.forward(key, value, To.child("selected topic")) context.forward(key, value, To.child("selected topic"))

All the other answers are right, at the time of writing I did find any "config-only" solution in the Kafka toolset.所有其他答案都是正确的,在撰写本文时,我确实在 Kafka 工具集中找到了任何“仅配置”解决方案。

What finally did the trick was to use Logstash , as its "kafka output plugin" supports jinja variables in topic-id parameter.最后的诀窍是使用Logstash ,因为它的“kafka 输出插件”支持topic-id参数中的 jinja 变量。

So once you have the "target topic name" available in a field (say service_name ) it's as simple as this:因此,一旦您在字段(例如service_name )中有可用的“目标主题名称”,它就像这样简单:

output {
  kafka {
    id => "sink"
    codec => [...]
    bootstrap_servers => [...]
    topic_id => "%{[service_name]}"
    [...]
  }
}

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

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