【问题标题】:Spark Streaming Accumulated Word CountSpark Streaming 累计字数
【发布时间】:2014-09-06 10:51:45
【问题描述】:

这是一个用 Scala 编写的 Spark Streaming 程序。它每 1 秒计算来自套接字的单词数。结果将是字数,例如,从时间 0 到 1 的字数,然后从时间 1 到 2 的字数。但我想知道是否有某种方法可以改变这个程序,以便我们可以累积字数?也就是从时间0到现在的字数。

val sparkConf = new SparkConf().setAppName("NetworkWordCount")
val ssc = new StreamingContext(sparkConf, Seconds(1))

// Create a socket stream on target ip:port and count the
// words in input stream of \n delimited text (eg. generated by 'nc')
// Note that no duplication in storage level only for running locally.
// Replication necessary in distributed scenario for fault tolerance.
val lines = ssc.socketTextStream(args(0), args(1).toInt, StorageLevel.MEMORY_AND_DISK_SER)
val words = lines.flatMap(_.split(" "))
val wordCounts = words.map(x => (x, 1)).reduceByKey(_ + _)
wordCounts.print()
ssc.start()
ssc.awaitTermination()

【问题讨论】:

    标签: scala distributed apache-spark spark-streaming


    【解决方案1】:

    您可以为此使用StateDStream。有一个example of stateful word count from sparks examples

    object StatefulNetworkWordCount {
      def main(args: Array[String]) {
        if (args.length < 2) {
          System.err.println("Usage: StatefulNetworkWordCount <hostname> <port>")
          System.exit(1)
        }
    
        StreamingExamples.setStreamingLogLevels()
    
        val updateFunc = (values: Seq[Int], state: Option[Int]) => {
          val currentCount = values.foldLeft(0)(_ + _)
    
          val previousCount = state.getOrElse(0)
    
          Some(currentCount + previousCount)
        }
    
        val sparkConf = new SparkConf().setAppName("StatefulNetworkWordCount")
        // Create the context with a 1 second batch size
        val ssc = new StreamingContext(sparkConf, Seconds(1))
        ssc.checkpoint(".")
    
        // Create a NetworkInputDStream on target ip:port and count the
        // words in input stream of \n delimited test (eg. generated by 'nc')
        val lines = ssc.socketTextStream(args(0), args(1).toInt)
        val words = lines.flatMap(_.split(" "))
        val wordDstream = words.map(x => (x, 1))
    
        // Update the cumulative count using updateStateByKey
        // This will give a Dstream made of state (which is the cumulative count of the words)
        val stateDstream = wordDstream.updateStateByKey[Int](updateFunc)
        stateDstream.print()
        ssc.start()
        ssc.awaitTermination()
      }
    }
    

    它的工作方式是为每个批次获取一个Seq[T],然后更新一个类似于累加器的Option[T]。它是Option 的原因是因为在第一批它将是None 并保持这种状态,除非它被更新。在此示例中,计数是一个 int,如果您正在处理大量数据,您甚至可能希望有一个 LongBigInt

    【讨论】:

      【解决方案2】:

      我有一个非常简单的答案,它只有几行代码。你可以发现这是大多数的火花书籍。请记住,我使用了 localhost 和端口 9999。

      from pyspark import SparkContext
      from pyspark.streaming import StreamingContext
      
      sc = SparkContext(appName="PythonStreamingNetworkWordCount")
      ssc = StreamingContext(sc, 1)
      lines = ssc.socketTextStream("localhost", 9999)
      counts = lines.flatMap(lambda line: line.split(" "))\
                           .map(lambda word: (word, 1))\
                           .reduceByKey(lambda a, b: a+b)
      counts.pprint()
      ssc.start()
      ssc.awaitTermination()
      

      你可以使用简单的来阻止

      ssc.stop()

      这是一个非常基本的代码,但此代码有助于基本了解 spark 流,更具体地说是 Dstream。

      在您的终端(Mac 终端)类型中向 localhost 提供输入

      nc -l 9999

      所以它会听你在那之后输入的所有内容,并且会计算单词

      希望这有帮助。

      【讨论】:

      • 实际上,这是在 PySpark 中实现的解决方案。
      猜你喜欢
      • 2019-01-08
      • 1970-01-01
      • 2016-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      • 2020-03-19
      相关资源
      最近更新 更多