[英]Sample word count application using Flume + Spark Streaming
以下是我使用Scala获取Flume事件并在spark.streaming
进行处理的代码。
尝试使用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)
}
}
为了获得该功能reduceByKey
上DStream[(String, Int)]
,你需要导入以下包:
import org.apache.spark.streaming.StreamingContext._
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.