import org.apache.spark.{SparkConf, SparkContext}
/**
* Created by liupeng on 2017/6/16.
*/
object T_flatMap {
System.setProperty("hadoop.home.dir","F:\\hadoop-2.6.5")
def main(args: Array[String]): Unit = {
val conf = new SparkConf().setAppName("flatMap_test").setMaster("local")
val sc = new SparkContext(conf)
val nameList : List[String] = List(
"hello liupeng", "hello liuxi", "hello xiaoma"
)
//flatMap = flat + map返回多个元素
val nameRDD = sc.parallelize(nameList)
val words = nameRDD.flatMap(line => line.split(" "))
words.foreach(println)
}
}
运行结果:
hello
liupeng
hello
liuxi
hello
xiaoma
liupeng
hello
liuxi
hello
xiaoma