如何在MQTTUtils中使用createpairedStream?

时间:2019-03-05 10:14:40

标签: scala apache-spark spark-streaming mqtt apache-bahir

我不能使用

  

MQTTUtils.createPairedStream()

在Scala中?

如何将主题列表指定为参数?我尝试了所有方法,例如字典,列表,元组,但是没有用。然后我在python中尝试过,当时它显示了

之类的错误
  

在发送驱动程序端口号之前退出Java网关进程

1 个答案:

答案 0 :(得分:1)

Scala是一种静态类型的语言,您提供给方法的参数必须具有特定的类型才能编译该方法调用。有时,方法会被重载,在这种情况下,可以提供不同的参数组合;在Scala中,有时,某些参数可能具有默认值,在这种情况下,根本不需要提供它们。

有几种方法可以确定给定方法可以使用哪些参数。键入MQTTUtils.createPairedStream(时,IDE可能会显示工具提示。有时,库开发人员会发布API文档,该文档以易于阅读的格式显示方法签名(包括参数类型和返回值)。如果这两个都不是您的选择,并且该项目是开源的,则只需查找源代码即可。在this case中,您会发现以下内容(请注意,如果您使用的是其他版本,则可能需要在GitHub界面中更改标签):

  /**
   * Create an input stream that receives messages pushed by a MQTT publisher.
   * @param ssc           StreamingContext object
   * @param brokerUrl     Url of remote MQTT publisher
   * @param topics        Array of topic names to subscribe to
   * @param storageLevel  RDD storage level. Defaults to StorageLevel.MEMORY_AND_DISK_SER_2.
   */
  def createPairedStream(
      ssc: StreamingContext,
      brokerUrl: String,
      topics: Array[String],
      storageLevel: StorageLevel = StorageLevel.MEMORY_AND_DISK_SER_2
    ): ReceiverInputDStream[(String, String)] = {
    new MQTTPairedInputDStream(ssc, brokerUrl, topics, storageLevel)
}

这是八个createPairedStream方法重载中最简单的方法(其余所有重载附加参数)。这表明您需要(按顺序)提供StreamingContext,代理URL和一系列主题。