【问题标题】:Twitter data from spark来自 Spark 的 Twitter 数据
【发布时间】:2017-02-17 19:55:10
【问题描述】:

我正在学习使用 Spark 流的 Twitter 集成。

   import org.apache.spark.streaming.{Seconds, StreamingContext}
    import org.apache.spark.SparkContext._
    import org.apache.spark.streaming.twitter._
    import org.apache.spark.SparkConf

    /**
     * Calculates popular hashtags (topics) over sliding 10 and 60 second windows from a Twitter
     * stream. The stream is instantiated with credentials and optionally filters supplied by the
     * command line arguments.
     *
     * Run this on your local machine as
     *
     */
    object TwitterPopularTags {
      def main(args: Array[String]) {


        if (args.length < 4) {
          System.err.println("Usage: TwitterPopularTags <consumer key> <consumer secret> " +
            "<access token> <access token secret> [<filters>]")
          System.exit(1)
        }

        StreamingExamples.setStreamingLogLevels()

        val Array(consumerKey, consumerSecret, accessToken, accessTokenSecret) = args.take(4)
        val filters = args.takeRight(args.length - 4)

        // Set the system properties so that Twitter4j library used by twitter stream
        // can use them to generat OAuth credentials
        System.setProperty("twitter4j.oauth.consumerKey", consumerKey)
        System.setProperty("twitter4j.oauth.consumerSecret", consumerSecret)
        System.setProperty("twitter4j.oauth.accessToken", accessToken)
        System.setProperty("twitter4j.oauth.accessTokenSecret", accessTokenSecret)

        val sparkConf = new SparkConf().setAppName("TwitterPopularTags").setMaster("local[2]")
        val ssc = new StreamingContext(sparkConf, Seconds(2))
        val stream = TwitterUtils.createStream(ssc, None, filters)//Dstream

        val hashTags = stream.flatMap(status => status.getText.split(" ").filter(_.startsWith("#")))

        val topCounts60 = hashTags.map((_, 1)).reduceByKeyAndWindow(_ + _, Seconds(60))
                         .map{case (topic, count) => (count, topic)}
                         .transform(_.sortByKey(false))

        val topCounts10 = hashTags.map((_, 1)).reduceByKeyAndWindow(_ + _, Seconds(10))
                         .map{case (topic, count) => (count, topic)}
                         .transform(_.sortByKey(false))


        // Print popular hashtags
        topCounts60.foreachRDD(rdd => {
          val topList = rdd.take(10)
          println("\nPopular topics in last 60 seconds (%s total):".format(rdd.count()))
          topList.foreach{case (count, tag) => println("%s (%s tweets)".format(tag, count))}
        })

        topCounts10.foreachRDD(rdd => {
          val topList = rdd.take(10)
          println("\nPopular topics in last 10 seconds (%s total):".format(rdd.count()))
          topList.foreach{case (count, tag) => println("%s (%s tweets)".format(tag, count))}
        })

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

我无法完全理解以下 2 行代码:

val Array(consumerKey, consumerSecret, accessToken, accessTokenSecret) = args.take(4)
val filters = args.takeRight(args.length - 4)

谁能解释一下这两条线?

感谢和问候,

【问题讨论】:

    标签: scala twitter apache-spark spark-streaming


    【解决方案1】:
    val Array(consumerKey, consumerSecret, accessToken, accessTokenSecret) = args.take(4)
    

    args 是一个数组; take(4) 返回一个包含前(最左边)四个元素的子数组。将这四个元素赋值给Array(consumerKey, consumerSecret, accessToken, accessTokenSecret) 意味着val consumerKey 将保存第一个元素的值; consumerSecret 将保存第二个的值,依此类推。这是一个巧妙的 Scala 技巧,将数组(也可以与其他集合一起完成)“解包”为命名值。

    val filters = args.takeRight(args.length - 4)
    

    takeRight(n) 从右侧返回一个子数组,表示数组中的最后一个 n 元素。在这里,除了前四个元素之外的所有元素都被分配到一个名为 filters 的新值中。

    【讨论】:

    • 非常感谢 Tzach 的解释。很有帮助
    猜你喜欢
    • 2016-03-27
    • 1970-01-01
    • 2019-11-19
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2019-08-31
    • 2021-10-18
    相关资源
    最近更新 更多