简体   繁体   English

Spark Streaming MQTT

[英]Spark Streaming MQTT

I've been using spark to stream data from kafka and it's pretty easy. 我一直在使用spark从kafka传输数据,这很简单。

I thought using the MQTT utils would also be easy, but it is not for some reason. 我认为使用MQTT工具也很容易,但不是出于某种原因。

I'm trying to execute the following piece of code. 我正在尝试执行以下代码。

  val sparkConf = new SparkConf(true).setAppName("amqStream").setMaster("local")
  val ssc = new StreamingContext(sparkConf, Seconds(10))

  val actorSystem = ActorSystem()
  implicit val kafkaProducerActor = actorSystem.actorOf(Props[KafkaProducerActor])

  MQTTUtils.createStream(ssc, "tcp://localhost:1883", "AkkaTest")
    .foreachRDD { rdd =>
      println("got rdd: " + rdd.toString())
      rdd.foreach { msg =>
        println("got msg: " + msg)
      }
    }

  ssc.start()
  ssc.awaitTermination()

The weird thing is that spark logs the msg I sent in the console, but not my println. 奇怪的是,spark会记录我在控制台中发送的消息,而不是我的println。

It logs something like this: 它记录这样的事情:

19:38:18.803 [RecurringTimer - BlockGenerator] DEBUG oassreceiver.BlockGenerator - Last element in input-0-1435790298600 is SOME MESSAGE 19:38:18.803 [RecurringTimer - BlockGenerator] DEBUG oassreceiver.BlockGenerator - 输入0-1435790298600中的最后一个元素是SOME MESSAGE

foreach is a distributed action, so your println may be executing on the workers. foreach是一个分布式动作,因此你的println可能正在对worker进行操作。 If you want to see some of the messages printed out locally, you could use the built in print function on the DStream or instead of your foreachRDD collect (or take) some of the elements back to the driver and print them there. 如果你想看到一些本地打印的消息,你可以使用DStream上的内置print功能,或者代替你的foreachRDD将一些元素收集(或取出)给驱动程序并在那里打印。 Hope that helps and best of luck with Spark Streaming :) 希望对Spark Streaming有帮助并祝你好运:)

If you wish to just print incoming messages, try something like this instead of the for_each (translating from a working Python version, so do check for Scala typos): 如果你想打印传入的消息,尝试这样的东西而不是for_each(从工作的Python版本翻译,所以检查Scala拼写错误):

val mqttStream = MQTTUtils.createStream(ssc, "tcp://localhost:1883", "AkkaTest")
mqttStream.print()

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

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