object sparktest {
def main(args: Array[String]): Unit = {
val conf = new SparkConf()
.setAppName("Searcher")
.setMaster("local[*]")
val sc = new SparkContext(conf)
val rdd:RDD[String] = sc.parallelize(Array("hello world","hello boy","nihao shijie ","hellow zhongguo"))
val rdd2 = rdd.flatMap(_.split(" ")).foreach(println) //是值
val rdd3 = rdd.map(_.split(" ")).foreach(println) //是地址
sc.stop()
}
}
关于Spark中,map和flatMap的区别
map之后返回的结果是RDD( RDD("hello","world"),RDD("hello","boy"),RDD("nihao ","shijie "),RDD("hellow ","zhongguo") )
flatmap之后返回的结果是RDD("hello","world","hello","boy","nihao ","shijie ","hellow ","zhongguo")
rdd(map).foreach(println) //打印出来是地址
rdd(map).foreach(x=>x.foreach(println)) //打印出来是值
rdd(flatmap).foreach(println) //打印出来是值