简体   繁体   English

如何使用 kafka sink 向 kafka topic 发送数据?

[英]How to send data to kafka topic with kafka sink?

Currently I have a sink connector which gets data from topic A and sends its to an external service.目前我有一个接收器连接器,它从主题 A 获取数据并将其发送到外部服务。

Now I have a use case when based on some logic I should send it to topic B instead of the service.现在我有一个用例,当基于某种逻辑我应该将它发送到主题 B 而不是服务时。 And this logic based on the response of the target service,that will return response based on the data.而这个逻辑基于目标服务的响应,将根据数据返回响应。 So because the data should be sent to the target system every time I couldnt use the stream api.因此,因为每次我无法使用 stream api 时,数据都应该发送到目标系统。

Is that feasible somehow?这在某种程度上可行吗?

Or should I add a kafka producer manually to my sink?或者我应该手动将 kafka 生产者添加到我的接收器? If so is there any drawback?如果是这样有什么缺点吗?

The first option, is to create a custom Kafka Connect Single Message Transform that will implement the desired logic and possibly use ExtractTopic as well (depending on how your custom smt looks like).第一个选项是创建一个自定义的 Kafka Connect 单消息转换,它将实现所需的逻辑并可能使用ExtractTopic (取决于您的自定义 smt 的外观)。


The second option is to build your own consumer.第二种选择是建立自己的消费者。 For example:例如:

Step 1: Create one more topic on top of topic A步骤 1:在主题 A 之上再创建一个主题

Create one more topic, say topic_a_to_target_system再创建一个主题,比如topic_a_to_target_system

Step 2: Implement your custom consumer第 2 步:实现您的自定义使用者

Implement a Kafka Consumer that consumes all the messages from topic topic_a .实现一个 Kafka 消费者,它使用来自主题topic_a的所有消息。 At this point, you need to instantiate a Kafka Producer and based on the logic, decide whether the topic needs to be forwarded to topic_B or to the target system ( topic_a_to_target_system ).此时,您需要实例化一个 Kafka Producer,并根据逻辑决定主题是需要转发到topic_B还是目标系统( topic_a_to_target_system )。

Step 3: Start Sink connector on topic_a_to_target_system第 3 步:在topic_a_to_target_system上启动 Sink 连接器

Finally start your sink connector so that it sinks the data from topic topic_a_to_target_system to your target system.最后启动您的接收器连接器,以便它将数据从主题topic_a_to_target_system到您的目标系统。

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

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