【问题标题】:Search for specific key word using twitter api and spark使用 twitter api 和 spark 搜索特定关键字
【发布时间】:2018-11-29 14:50:48
【问题描述】:

我正在尝试此代码,并将 # 替换为 #Apple。

val ssc = new StreamingContext("local[*]", "PopularHashtags", Seconds(1))
val tweets = TwitterUtils.createStream(ssc, None)
val statuses = tweets.map(status => status.getText())
val tweetwords = statuses.flatMap(tweetText => tweetText.split(" "))
val hashtags = tweetwords.filter(word => word.startsWith("#"))
val hashtagKeyValues = hashtags.map(hashtag => (hashtag, 1))
val hashtagCounts = hashtagKeyValues.reduceByKeyAndWindow( (x,y) => x + y, (x,y) => x - y, Seconds(1000), Seconds(1))
val sortedResults = hashtagCounts.transform(rdd => rdd.sortBy(x => x._2, false))
sortedResults.print

但我没有得到任何结果。

这种流媒体对推文的数量以及从哪个地区获取推文是否有限制? 我还尝试在我的推特帐户中寻找#OPPO,因为这是趋势,所以我尝试寻找它,但仍然没有得到任何结果。

【问题讨论】:

    标签: scala apache-spark twitter twitter-streaming-api


    【解决方案1】:
    val ssc = new StreamingContext("local[*]", "PopularHashtags", Seconds(1))
    //The keyword you want to look for can be specified in a sequence as follows
    var seq:Seq[String] = Seq("#Rajasthan","#Apple")
    val tweets = TwitterUtils.createStream(ssc, None, seq)
    val statuses = tweets.map(status => status.getText())
    val tweetwords = statuses.flatMap(tweetText => tweetText.split(" "))
    val hashtags = tweetwords.filter(word=>word.contains("#"))
    val hashtagKeyValues = hashtags.map(hashtag => (hashtag, 1))
    val hashtagCounts = hashtagKeyValues.reduceByKeyAndWindow( (x,y) => x + y, (x,y) => x - y, Seconds(1000), Seconds(1))
    val sortedResults = hashtagCounts.transform(rdd => rdd.sortBy(x => x._2, false))
    sortedResults.print
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-16
      • 2011-10-27
      • 1970-01-01
      • 2020-11-07
      • 1970-01-01
      • 1970-01-01
      • 2012-02-05
      • 2017-10-28
      相关资源
      最近更新 更多