使用 Flume + Spark Streaming 的示例字数统计应用程序

Sample word count application using Flume + Spark Streaming

下面是我使用 Scala 在 spark.streaming 中获取 Flume 事件和处理的代码。

尝试使用 reduceBykey 函数时出现以下编译错误:

value reduceByKey is not a member of org.apache.spark.streaming.dstream.DStream[(String, Int)]

为什么?

我们是否需要以除此之外的任何特定方式处理 Flume 流?

我不认为这是一个依赖性问题,我有其他简单的应用程序在同一个 Eclipse IDE 中工作,其中使用了 reduceBykey

package com.deloitte.spark.learning

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

object Wordcount {
    def main(args: Array[String]) {
        if (args.length < 2) {
            System.err.println("Usage: NetworkWordCount <hostname> <port>")
            System.exit(1)
        }
        val sparkConf = new Sparkconf().setMaster("local[2]").setAppName("aa")
        val ssc = new StreamingContext(sparkConf, Seconds(200))
        val stream = FlumeUtils.createStream(ssc, args(0), args(1).toInt)
        stream.count().map(cnt => "Received " + cnt + " flume events." ).print()
        val lines = stream.map {
            e => new String(e.event.getBody().array(), "UTF-8")
        }
        val words = lines.flatMap(_.split(" "))
        val wordCounts = words.map(x => (x, 1))
        ssc.start()
        ssc.awaitTermination(1000)
    }
}

为了在 DStream[(String, Int)] 上获得函数 reduceByKey,您需要导入以下包:

import org.apache.spark.streaming.StreamingContext._