SparkRDD Operations(一)

1.RDD分类

RDD 算子中,有两大分类 TransformationsActions,对于RDD而言,它支持者两种类型的操作;

1.1 Transformations:

  • 从现有的数据库中 经过装换操作之后形成新的RDD,常见的的 Transformations 算子有 map,filter,mapPartitions,groupByKey
  • Transformations 这类算子,都是 lazy的,因为它们不会立即计算结果,这类算子只会记得RDD之后的转换关系,也是就 bloodline(血统),这个是因为RDD的特性(A list of dependencies on other RDDs)

1.2 Actions:

  • 当Spark应用程序 遇到这类算子 会产生Job,在对数据集运行计算后,将值返回给驱动程序,常见的Actions 算子有 reduce,take,collect,saveAsTextFile

2.RDD常见算子操作

map:

map(func)
将func函数作用到数据集的每一个元素上,生成一个新的分布式的数据集返回

word => (word,1)

filter:

filter(func)
选出所有func返回值为true的元素,生成一个新的分布式的数据集返回

flatMap

flatMap(func)
输入的item能够被map到0或者多个items输出,返回值是一个Sequence

groupByKey:把相同的key的数据分发到一起

['hello', 'spark', 'hello', 'world', 'hello', 'world']
('hello',1) ('spark',1)........

reduceByKey: 把相同的key的数据分发到一起并进行相应的计算

 mapRdd.reduceByKey(lambda a,b:a+b)
 [1,1]  1+1
 [1,1,1]  1+1=2+1=3
 [1]    1

Join:

inner join
outer join:left/right/full

猜你喜欢

转载自blog.csdn.net/weixin_40420525/article/details/85121362