Spark common operator summary (3) - flatMapValues

 

 

flatmapValues ​​and flatmap is similar, but the people itself is a key value, so a pair how to become more pair, that is based on a function of values ​​of
val a = sc.parallelize(List((1,2),(3,4),(5,6)))
val b = a.flatMapValues(x=>1 to x)
b.collect.foreach(println(_))
/*结果
(1,1)
(1,2)
(3,1)
(3,2)
(3,3)
(3,4)
(5,1)
(5,2)
(5,3)
(5,4)
(5,5)
(5,6)
*/
 

 Put in a pair of values ​​into an array, then the k-v1 k-v2 ...

val list = List(("mobin",22),("kpop",20),("lufei",23))
val rdd = sc.parallelize(list)
val mapValuesRDD = rdd.flatMapValues(x => Seq(x,"male"))
mapValuesRDD.foreach(println)

输出:

(mobin,22)
(mobin,male)
(kpop,20)
(kpop,male)
(lufei,23)
(lufei,male)

If mapValues ​​will output: [Contrast] difference

(mobin,List(22, male))
(kpop,List(20, male))
(lufei,List(23, male))

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/pocahontas/p/11334575.html