如何使用mqtt source connect更改消息的密钥

时间:2018-08-07 13:21:57

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

我有一个将Java应用程序sendind数据发送到mosquitto代理(mqtt)中,并使用kafka connect,将这些数据从mqtt代理发送到kafka主题,但是问题是,总是在mqtt源连接发送时数据,默认情况下,键始终是kafka主题的名称,我需要对其进行更改。我使用了SMT(单消息转换),我可以更改密钥,但是它是用base64编码的,任何想法我怎么能转换并仅获取id的值?

我的连接器配置经过以下转换:

name=MqttSourceConnector
connector.class=io.confluent.connect.mqtt.MqttSourceConnector
mqtt.qos=1
tasks.max=2
mqtt.clean.session.enabled=true
mqtt.server.uri=tcp://mosquitto-server:1883
mqtt.connect.timeout.seconds=30
key.converter.schemas.enable=false
value.converter.schemas.enable=false
mqtt.topics=mqtt_topic
mqtt.keepalive.interval.seconds=60
kafka.topic=kafka_topic
value.converter=org.apache.kafka.connect.converters.ByteArrayConverter
key.converter=org.apache.kafka.connect.json.JsonConverter
transforms=createMap,createKey,extractInt
transforms.createMap.type=org.apache.kafka.connect.transforms.HoistField$Value
transforms.createMap.field=id
transforms.createKey.type=org.apache.kafka.connect.transforms.ValueToKey
transforms.createKey.fields=id
transforms.extractInt.type=org.apache.kafka.connect.transforms.ExtractField$Value
transforms.extractInt.field=id

进入我的kafka主题的是:

Key { id: eyJpZCI6IlNCMDQiLCJ0ZW1wZXJhdHVyZSI6MjQuOTk2OTQyOTk0NDIyMTM4LCJodW1pZGl0eSI6MzkuNzUyNjQzNTk3MjcyMjYsInRpbWVzdGFtcCI6MTUzMjY4NTEzMTI2Nn0= }
Value { id: SB04, temperature: 24.996942994422138, humidity: 39.75264359727226, timestamp: 1532685131266 }

因此,我只需要将SB04值设置为键即可。有想法吗?

0 个答案:

没有答案