What are the common RDD transformation operations and action operators in spark? Practical~

The common transformation operations and action operation operators of RDD need to be remembered. After remembering, you don't need to worry about whether the result obtained is an RDD or an operation result when programming.

Conversion action:

An RDD transformation operation on {1,2,3,4,4}

map():将函数应用到RDD中的每一个元素,返回值构成新的RDD;
flatmap():将函数应用于RDD中的每一个元素,并将返回的迭代器中的所有内容构成新的RDD,常用于切分单词;
filter():根据条件过滤,返回新的RDD;
distinct():去重
sample(withReplacement,fraction,[seed]):对RDD采样,以及是否替换。
withReplacement : Boolean , True表示进行替换采样,PoissonSampler取样器,False表示进行非替换采样,BernoulliSampler的取样器。
fraction : Double, 在0~1之间的一个浮点值,表示要采样的记录在全体记录中的比例。
seed :随机种子。

The transformation operation on two RDDs with data {1,2,3} and {2,3,4} respectively

union():生成一个包含两个RDD中的所有元素的RDD
intersection():求两个RDD共同元素的RDD
subtract():移除一个RDD中的内容,例如移除 训练数据,rdd.subtract(other)
cartesian():与另一个 RDD的笛卡尔积;rdd.cartesian(other)

The transformation operation on the key-value pair RDD collection {(1,2), (3,4), (3,5,)}

reduceByKey(func):合并具有相同键的值;
rdd.reduceByKey((x,y)=>x+y)
{(1,2),(3,9)}
groupByKey():对具有相同键的值进行分组。
rdd.groupByKey()={(1,[2]),(3,[4,5])}
combineByKey(creatCombiner,mergeValue,mergeCombiner,partitioner)
使用不同的返回类型合并具有相同键的值。详见:https://blog.csdn.net/jiangpeng59/article/details/52538254
mapValues(func):对pairRDD中的每个值应用
flatMapValues(func):对pairRDD中的每个值应用一个返回迭代器的函数,然后对返回的每个元素都生成一个对应原键的键值对记录,通常用于符号化。
rdd.flatMapValues(x=>(x to 5))
keys():返回一个只包含键的RDD
values():返回一个只包含值的RDD
sortBKey():返回一个根据键排序的RDD

Transformation operation on two pair RDDs

subtractByKey():删除RDD中键与otherRDD中键相同的元素
rdd.subtractByKey(other)
join():对两个RDD进行内连接
rightOuterJoin():对两个RDD进行连接操作,确保第一个RDD的的键必须存在(右外链接)rdd.rightOuterJoin(other)
leftOuterJoin():与上相似
cogroup():将两个RDD中拥有相同键的数据分组。

Action operation
write picture description here
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324852043&siteId=291194637