使用Flume + Spark Streaming进行单词计数应用示例

时间:2015-09-18 07:28:28

标签: scala apache-spark spark-streaming flume

以下是我使用Scala在spark.streaming中获取Flume事件和进程的代码。

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

  

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

为什么?

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

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

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)
    }
}

1 个答案:

答案 0 :(得分:1)

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

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