以下内容整理来源于DT大数据梦工厂:http://weibo.com/ilovepains
(f : T => U) 函数参数为f, 函数类型是T, 返回类型是 U
实现统计文件里面数据总和
object FileTextLines {
def main (args: Array[String]) {
val conf = new SparkConf()
conf.setAppName("name count").setMaster("local")
val sc = new SparkContext(conf)
val datas = sc.textFile("D://googledown//datas.txt")
val linedatas = datas.map(lines => (lines, 1))
val pairsDatas = linedatas.reduceByKey(_+_)
pairsDatas.collect.foreach(ds => println(ds._1 + "...."+ ds._2))
}
}
collect 方法分析:
/** * Return an array that contains all of the elements in this RDD. */ def collect(): Array[T] = withScope { val results = sc.runJob(this, (iter: Iterator[T]) => iter.toArray) Array.concat(results: _*) }forech
override def foreach[U](f : scala.Function1[A, U]) : scala.Unit = { /* compiled code */ }
测试数据:
9 8
8 3
8 3
9 8
hadoop
spark
flume
spark
hadoop
hadoop
redis
spark
redis
redis
作业:动手画出Spark RDD TrasFormation 的图