标签:blank class highlight 元素 list 键值对 nbsp int article
1、map(function)
map是对RDD中的每个元素都执行一个指定的函数来产生一个新的RDD。任何原RDD中的元素在新RDD中都有且只有一个元素与之对应。
val a = sc.parallelize(1 to 9, 3) val b = a.map(x => x*2)//x => x*2是一个函数,x是传入参数即RDD的每个元素,x*2是返回值 a.collect //结果Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9) b.collect //结果Array[Int] = Array(2, 4, 6, 8, 10, 12, 14, 16, 18)
也可以把key变成Key-Value键值对
val a = sc.parallelize(List("dog", "tiger", "lion", "cat", "panther", " eagle"), 2) val b = a.map(x => (x, 1)) b.collect.foreach(println(_)) /* (dog,1) (tiger,1) (lion,1) (cat,1) (panther,1) ( eagle,1) */
2、
暂时写这些,参考博文:https://blog.csdn.net/weixin_34418883/article/details/85874446
https://blog.csdn.net/a414129902/article/details/84770548
标签:blank class highlight 元素 list 键值对 nbsp int article
原文地址:https://www.cnblogs.com/1061321925wu/p/12235223.html