1,258
社区成员
发帖
与我相关
我的任务
分享
import org.apache.spark.{SparkConf, SparkContext}
/**
* Created by mahuichao on 16/8/12.
*/
object Test04 {
var str: String = ""
def main(args: Array[String]): Unit = {
val conf = new SparkConf().setAppName("test04").setMaster("local[2]")
val sc = new SparkContext(conf)
sc.setLogLevel("FATAL")
val path = "/Users/mahuichao/Downloads/test.txt"
val file = sc.textFile(path)
file.flatMap(_.split(" ")).map { word =>
str = "hello the crude world"
(word, (1, str))
// (word,1)
}.reduceByKey {
case (x: (Int, String), y: (Int, String)) =>
println("I am the value of str:" + str)
(x._1 + y._1, str)
}.map { case (x, (y1, y2)) =>
(x, y1)
}.collect().foreach(println)
}
}