WordCount的实例

scala> sc.textFile("hdfs://hadoop1:9000/sparktest/hello.txt").flatMap( line => line.split("\t")).collect
res8: Array[String] = Array(you, jump, i, jump)

scala> sc.textFile("hdfs://hadoop1:9000/sparktest/hello.txt").flatMap( line => line.split("\t")).map( word => (word,1)).collect
res9: Array[(String, Int)] = Array((you,1), (jump,1), (i,1), (jump,1))

scala> sc.textFile("hdfs://hadoop1:9000/sparktest/hello.txt").flatMap( line => line.split("\t")).map( word => (word,1)).reduceByKey( (x:Int,y:Int) => x + y).collect
res10: Array[(String, Int)] = Array((jump,2), (you,1), (i,1))

scala> sc.textFile("hdfs://hadoop1:9000/sparktest/hello.txt").flatMap( line => line.split("\t")).map( word => (word,1)).reduceByKey( (_+_)).collect
res11: Array[(String, Int)] = Array((jump,2), (you,1), (i,1))

scala> sc.textFile("hdfs://hadoop1:9000/sparktest/hello.txt").flatMap( _.split("\t")).map( (_,1)).reduceByKey( (_+_)).collect
res12: Array[(String, Int)] = Array((jump,2), (you,1), (i,1))

你可能感兴趣的:(Spark学习随笔)