【问题标题】:how to drop other results in spark streaming?如何在火花流中删除其他结果?
【发布时间】:2023-03-11 19:51:01
【问题描述】:

我想让字数统计流式传输,只显示我想在 Twitter 上看到的字。

所以,我做了如下的绳子

import java.util.Properties
import org.apache.spark.SparkConf
import org.apache.spark.streaming.StreamingContext
import org.apache.spark.streaming.Seconds
import twitter4j.conf.ConfigurationBuilder
import twitter4j.auth.OAuthAuthorization
import twitter4j.Status
import org.apache.spark.streaming.twitter.TwitterUtils
import org.apache.spark.streaming._

import org.apache.log4j._
import org.apache.spark.streaming.StreamingContext._
import org.apache.spark.streaming.{Seconds, StreamingContext}
import org.apache.spark.streaming.twitter._
import twitter4j.TwitterFactory
import twitter4j.conf.ConfigurationBuilder
import java.util.Properties
import org.apache.spark.storage.StorageLevel
import twitter4j.auth.OAuthAuthorization

val appName = "TwitterData"

val ssc = new StreamingContext(sc, Seconds(10))
val hashTags = "XRP"
val cb = new ConfigurationBuilder
val prop = new Properties()

cb.setDebugEnabled(true).setOAuthConsumerKey("key number").setOAuthConsumerSecret("key number").setOAuthAccessToken("key number").setOAuthAccessTokenSecret("key number")

val bld = cb.build()
val tf = new TwitterFactory(bld)
val twitter = tf.getInstance()
val filters = Array(hashTags).toSeq
val auth = new OAuthAuthorization(bld)
val twitterStream = TwitterUtils.createStream(ssc, Some(auth), filters, StorageLevel.MEMORY_ONLY)

twitterStream.cache()

val lines = twitterStream.map(status => status.getText)
lines.print()

val words = lines.flatMap(_.split(" "))
val pairs = words.map(x => {
  if (x == "xrp" || x == "ripple"){
    (x, 1)
  } else {
  }
})

pairs.print()

ssc.start()

它适用于带有 Twitter 的 Spark Streaming,但按照结果,我想删除除我想要获得的结果之外的所有空白。

-------------------------------------------
Time: 1603866040000 ms
-------------------------------------------
@RuleXRP I need 15to25 usd per xrp
RT @Grayscale: 10/27/20 UPDATE: Net Assets Under Management, Holdings per Share, and Market Price per Share for our Investment Products.

T....

-------------------------------------------
Time: 1603866040000 ms
-------------------------------------------
()
()
()
()
()
()
(xrp,1)
()
()
()
...

我该怎么做?如果有什么方法可以得到我想要变得更好的唯一结果然后我的绳索,请告诉我。我需要你的帮助。 我非常感谢您的建议。 谢谢

【问题讨论】:

    标签: conditional-statements spark-streaming word-count


    【解决方案1】:
    val pairs = words.map(x => {
      if (x == "xrp" || x == "ripple"){
        (x, 1)
      } else {
      }
    })
    

    这会映射您的结果

    相反,您可以在映射之前使用过滤器,这会稍微减少您的代码:

    val pairs = words
      .filter(x => x == "xrp" || x == "ripple")
      .map(x => (x, 1))
    

    【讨论】:

    • 感谢您的建议。但是如果我用你的代码运行..我什至看不到单词被提及多少次的结果......
    • 您没有尝试打印吗?我们的解决方案应该没有太大区别,我只是过滤掉不需要的东西
    • 我尝试打印,但如您所见,我只得到了带有空括号的结果。我尝试先使用地图然后过滤。我想我没有尝试先使用过滤器然后映射。
    • 您是如何尝试先使用过滤器的?如果您将其与确切的语义一起使用,则无论如何您都不会过滤掉点映射的东西,因为您正在过滤元组,所以过滤器将不再起作用。你重写了吗?
    • 我只是一个初学者,尝试用自顶向下的方法学习。所以,我可能没有那么多基础知识。通过这个解决方案,我可以更具体地学习如何使用地图和过滤器。谢谢
    猜你喜欢
    • 2017-03-24
    • 1970-01-01
    • 2017-08-12
    • 2022-08-23
    • 1970-01-01
    • 2018-08-09
    • 2022-01-05
    • 1970-01-01
    • 2021-05-31
    相关资源
    最近更新 更多