本文摘自《Spark 快速大数据分析》
函数名 | 目的 | 示例 | 结果 |
---|---|---|---|
map() | 将函数应用于 RDD 中的每一个元素,将返回值构成新的 RDD | rdd.map(x -> x+1) | {2, 3, 4, 4} |
flatMap() | 将函数应用于 RDD 中的每一个元素,将返回的迭代器的全部内容构成新的 RDD。一般用来切分单词 | rdd.flatMap(x -> x.to(3)) | {1, 2, 3, 2, 3, 3, 3} |
filter() | 返回一个由经过传给 filter() 的函数的元素组成的 RDD | rdd.filter(x -> x != 1) | {2, 3, 3} |
distinct() | 去重 | rdd.distinct() | {1, 2, 3} |
sample(withReplacement, fraction, [seed]) | 对 RDD 采样,以及是否替换 | rdd.sample(false, 0.5) | 非肯定的 |
函数名 | 目的 | 示例 | 结果 |
---|---|---|---|
union() | 生成一个包含两个 RDD 中全部元素的 | RDD rdd.union(other) | {1, 2, 3, 3, 4, 5} |
intersection() | 求两个 RDD 共同的元素的 RDD | rdd.intersection(other) | {3} |
subtract() | 移除另外一个 RDD 中的元素 | rdd.subtract(other) | {1, 2} |
cartesian() | 于另外一个 RDD 的笛卡尔积 | rdd.cartesian(other) | {(1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 3), (3, 4), (3, 5)} |
函数名 | 目的 | 示例 | 结果 |
---|---|---|---|
collect() | 返回 RDD 中的全部元素 | rdd.collect() | {1, 2, 3, 3} |
count() | RDD 中的元素个数 | rdd.count() | 4 |
countByValue() | 各元素再 RDD 中出现的次数 | rdd.countByValue() | {(1, 1), (2, 1), (3, 2)} |
take(num) | 从 RDD 中返回 num 个元素 | rdd.take(2) | {1, 2} |
top(num) | 从 RDD 中返回最前面的 num 个元素 | rdd.top(2) | {3, 3} |
takeOrdered(num)(ordering) | 从 RDD 中按照提供的顺序返回最前面的 num 个元素 | rdd.takeOrdered(2)(myOrdering) | {3, 3} |
takeSample(withReplacement, num, [seed]) | 从 RDD 中返回任意一些元素 | rdd.takeSample(false, 1) | 非肯定的 |
reduce(func) | 并行整合 RDD 中的数据(例如 sum) | rdd.reduce((x, y) -> x + y) | 9 |
fold(zeor)(func) | 和 reduce() 同样,可是须要提供初始值 | rdd.fold(0)((x, y) -> x + y) | 9 |
★ aggregate(zeroValue)(seqOp, combOp) | 和 reduce() 类似,可是一般返回不一样类型的函数 | rdd.aggergate((0, 0))((x, y) -> (x._1 + y, x._2 + 1), (x, y) -> (x._1 + y._1, x._2 + y._2)) | (9, 4) |
foreach(func) | 对 RDD 中的每一个元素使用给定的函数 | rdd.foreach(func) | 无 |
函数名 | 目的 | 示例 | 结果 |
---|---|---|---|
reduceByKey(func) | 合并具备相同键的值 | rdd.reduceByKey((x, y) -> x + y) | {(1, 2), (3, 10)} |
groupByKey() | 对具备相同键的值进行分组 | rdd.groupByKey() | {(1, [2]), (3, [4, 6])} |
★ combineByKey(createCombiner, mergeValue, mergeCombiners, partitioner) | 使用不一样返回类型合并具备相同键的值 | 见例4-12 到例 4-14 | |
mapValues(func) | 对 pair RDD 中的每一个值应用一个函数而不改变键 | rdd.mapValues(x -> x + 1) | {(1, 3), (3, 5), (3, 7)} |
flatMapValues(func) | 对 pair RDD 中的每一个值应用一个返回迭代器的函数,而后对返回的每一个元素都生成一个对应原键值对记录。一般用于符号化 | rdd.flatMapValues(x -> (x to 5)) | {(1, 2), (1, 3), (1, 4), (1, 5), (3, 4), (3, 5)} |
keys() | 返回一个仅包含键的 RDD | rdd.keys() | {1, 3, 3} |
values() | 返回一个仅包含值的 RDD | rdd.values() | {2, 4, 6} |
sortByKey() | 返回一个根据键排序的 RDD | rdd.sortByKey() | {(1, 2), (3, 4), (3, 6)} |
函数名 | 目的 | 示例 | 结果 |
---|---|---|---|
subtractByKey | 删掉 RDD 中键与 other RDD 中的键相同的元素 | rdd.substractByKey(other) | {(1, 2)} |
join | 对两个 RDD 进行内链接 | rdd.join(other) | {(3, (4, 9)), (3, (6, 9))} |
★ rightOuterJoin | 对两个 RDD 进行链接操做,确保第一个 RDD 的键必须存在(右外链接) | rdd.rightOuterJoin(other) | {(3, (Some(4), 9)), (3, (Some(6), 9))} |
★ leftOuterJoin | 对两个 RDD 进行链接操做,确保第二个 RDD 的键必须存在(左外链接) | rdd.leftOuterJoin(other) | {(1, (2, None)), (3, (4, Some(9))), (3, (6, Some(9)))} |
cogroup | 将两个RDD 中拥有相同键的数据分组到一块儿 | rdd.cogroup(other) | {(1, ([2], [])), (3, ([4, 6], [9]))} |
函数名 | 目的 | 示例 | 结果 |
---|---|---|---|
countByKey() | 对每一个键对应的元素分别计数 | rdd.countByKey() | {(1, 1), (3, 2)} |
collectAsMap() | 将结果以映射表的形式返回,以便查询 | rdd.collectAsMap() | Map{(1, 2), (3, 6)} |
lookup(key) | 返回给定键对应的全部值 | rdd.lookup(3) | [4, 6] |
本文出自 walker snapshot