Kafka mqtt连接器如何发送mqtt主题作为密钥?

时间:2018-11-19 08:08:49

标签: apache-kafka mqtt apache-kafka-connect

我有一个正在运行的MQTT代理和一个Kafka代理,我使用了kafka-connector:https://github.com/Landoop/stream-reactor,并进行了以下配置:

$data = DB::table('tables')
    ->select('orders.order_id', 'users.*')
    ->leftJoin('lists', 'lists.order_id', '=', 'orders.order_id')
    ->leftJoin('users', 'users.user_id', '=', 'lists.user_id')
    ->where('users.type', '=', 'admin')
    ->get();

dd($data);

在kcql中,我定义了kafka应该作为密钥的消息字段,无论如何,还是要使用mqtt-topic作为密钥?因此,我不需要在kcql中定义name=Mqtt-Source connector.class=com.datamountaineer.streamreactor.connect.mqtt.source.MqttSourceConnector tasks.max=1 connect.mqtt.kcql=INSERT INTO test SELECT * FROM + WITHCONVERTER=`com.datamountaineer.streamreactor.connect.converters.source.JsonSimpleConverter` WITHKEY(id) connect.mqtt.connection.clean=true connect.mqtt.connection.timeout=1000 connect.mqtt.connection.keep.alive=1000 connect.mqtt.client.id=test_mqtt_connector connect.mqtt.converter.throw.on.error=true connect.mqtt.hosts=tcp://mqtt-broker:1883 connect.mqtt.service.quality=1

2 个答案:

答案 0 :(得分:0)

我不了解Landoop的KCQL,但是假设主题是消息值的一部分,您可以将其移动到键中,就像这样

transforms=ReplaceKey,ExtractKey
transforms.ReplaceKey.type=org.apache.kafka.connect.transforms.ValueToKey
# change the field accordingly
transforms.ReplaceKey.fields=mqtt_topic
transforms.ExtractKey.type=org.apache.kafka.connect.transforms.ExtractField$Key
# make sure this is the same field as above
transforms.ExtractKey.field=mqtt_topic

如果没有,则可以将其静态插入

transforms=AddKey
transforms.AddKey.type=org.apache.kafka.connect.transforms.InsertField$Key
# The exclamation makes this a required field
transforms.AddKey.static.field=mqtt_topic!
transforms.AddKey.static.value="<<your topic name>>"

但是,以上内容可能不适用于SELECT * FROM +,您可以在其中从所有MQTT主题中进行选择

答案 1 :(得分:0)

最后,我将Kafka-connect更改为下一个,默认情况下会这样做:

https://github.com/asierg95/kafka-connect-mqtt