如何将mqtt的有效负载数据转换为kafka字符串类型

yb3bgrhw  于 2021-06-07  发布在  Kafka
关注(0)|答案(1)|浏览(489)

现在我正在尝试从mqtt消息连接到kafka(实际上是kafka中的spark流)
我用了这个连接器https://github.com/evokly/kafka-connect-mqtt
和spark-2.1.0,kafka-0.10.1.1
像这样的Spark流输出

({"schema":{"type":"string","optional":false},"payload":"mqtt"},{"schema":{"type":"bytes","optional":false},"payload":"MTIzMTIz"})

生产商代码

object mqttProducer {
def main(args: Array[String]) {
val brokerUrl = "tcp://ip"
val topic = "mqtt"
val msg = "123123"

var client: MqttClient = null

// Creating new persistence for mqtt client
val persistence = new MqttDefaultFilePersistence("/tmp")

try {
  // mqtt client with specific url and client id
  client = new MqttClient(brokerUrl, MqttClient.generateClientId, persistence)

  client.connect()

  val msgTopic = client.getTopic(topic)
  val message = new MqttMessage(msg.getBytes("utf-8"))

  while (true) {
    msgTopic.publish(message)
    println("Publishing Data, Topic : %s, Message : %s".format(msgTopic.getName, message))
    Thread.sleep(1000)
  }
}

catch {
  case e: MqttException => println("Exception Caught: " + e)
}

finally {
  client.disconnect()
}

和Spark流Kafka消费代码

package hb.test1
import org.apache.kafka.common.serialization.StringDeserializer
import org.apache.spark.SparkConf
import org.apache.spark.streaming.Seconds
import org.apache.spark.streaming.StreamingContext
import org.apache.spark.streaming.kafka010.ConsumerStrategies.Subscribe
import org.apache.spark.streaming.kafka010.KafkaUtils
import org.apache.spark.streaming.kafka010.LocationStrategies.PreferConsistent

object test2 {

  def main(args: Array[String]): Unit = {

val sparkConf = new SparkConf().setAppName("app")
val ssc = new StreamingContext(sparkConf, Seconds(1))    

val kafkaParams = Map[String, Object](
  "bootstrap.servers" -> 
  "servers ip",
  "key.deserializer" -> classOf[StringDeserializer],
  "value.deserializer" -> classOf[StringDeserializer],
  "group.id" -> "use_a_separate_group_id_for_each_stream",
  "auto.offset.reset" -> "latest",
  "enable.auto.commit" -> (false: java.lang.Boolean)
)
val topics = Array("mqtt-kafka")
  val stream = KafkaUtils.createDirectStream[String, String](
  ssc,
  PreferConsistent,
  Subscribe[String, String](topics, kafkaParams)
)

val testStream = stream.map(x => (x.key, x.value))

testStream.print()

ssc.start()
ssc.awaitTermination()
  }
}

我怎么能得到字符串而不是字节?请帮帮我

egdjgwm8

egdjgwm81#

有效负载“mtizmtiz”是刚刚base64编码的字符串“123123”。如果您只想获取mqtt负载并将其发送到kafka而不使用base64编码,那么应该使用bytearrayconverter。在同一mqtt连接器的配置中,我将值转换器设置为:
“value.converter”:“io.confluent.connect.replicator.util.bytearrayconverter”
上面的bytearrayconverter与confluent enterprise发行版一起提供,但也有其他开源的kafka connect bytearrayconverter,如qubole/streamx kafka-connect-s3连接器附带的。
https://github.com/qubole/streamx/blob/8b9d43008d9901b8e6020404f137944ed97522e2/src/main/java/com/qubole/streamx/bytearrayconverter.java
kip-128将标准bytearray转换器添加到kafka连接框架中
https://cwiki.apache.org/confluence/display/kafka/kip-128%3a+add+bytearrayconverter+for+kafka+connect
更新:Kafka0.11现在已发布,并与bytearrayconverter船舶。配置 "value.converter": "org.apache.kafka.connect.converters.ByteArrayConverter" 而且您应该获得没有base64编码的原始mqtt负载。

相关问题