Spark1.x RDD基本操作

本文详细介绍了Spark1.x中Resilient Distributed Datasets (RDD)的基本操作,包括Transformations如map、filter、flatMap、groupByKey等,以及Actions如reduce、collect、count、saveAsTextFile等。通过实例展示了如何使用这些操作处理RDD。
摘要由CSDN通过智能技术生成

1.   Spark1.x RDD基本操作 

Spark官方文档:

http://spark.apache.org/docs/latest/programming-guide.html#rdd-operations


1.1.   Transformations

1.1.1. 创建RDD

1、数组创建RDD

从普通数组创建RDD,里面包含了1到9这9个数字,它们分别在3个分区中。

valrdd1= sc.parallelize(1 to 9, 3)

rdd1:org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[1] at parallelize at<console>:18

2、文件创建RDD

valrdd1=sc.textFile("hdfs://192.168.180.10:9000/input/item.txt") 

rdd1:org.apache.spark.rdd.RDD[String] = MappedRDD[3] at textFile at<console>:18

3、其它方式

创建RDD还包含其它方式。

1.1.2. map (func)

map是对RDD中的每个元素都执行一个指定的函数来产生一个新的RDD;

RDD元素是一对一关系;

valrdd1 = sc.parallelize(1 to 9, 3)

rdd1.collect

res2:Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)

valrdd2 = rdd1.map(x => x*2)

rdd2.collect

res3:Array[Int] = Array(2, 4, 6, 8, 10, 12, 14, 16, 18)

1.1.3. filter(func)

返回一个新的数据集,由经过func函数后返回值为true的原元素组成;

对RDD元素进行过滤;

valrdd3 = rdd2. filter (x => x  > 10)

rdd3.collect

res4:Array[Int] = Array(12, 14, 16, 18)

1.1.4. flatMap(func)

类似于map,但是每一个输入元素,会被映射为0到多个输出元素(因此,func函数的返回值是一个Seq,而不是单一元素)

RDD元素是一对多关系;

valrdd4 = rdd3. flatMap (x => x to 20)

res5:Array[Int] = Array(12, 13, 14, 15, 16, 17, 18, 19, 20, 14, 15, 16, 17, 18, 19,20, 16, 17, 18, 19, 20, 18, 19, 20)

1.1.5. mapPartitions(func)

mapPartitions是map的一个变种。map的输入函数是应用于RDD中每个元素,而mapPartitions的输入函数是应用于每个分区,也就是把每个分区中的内容作为整体来处理的。 

它的函数定义为:

defmapPartitions[U: ClassTag](f: Iterator[T] => Iterator[U],preservesPartitioning: Boolean = false): RDD[U]

f即为输入函数,它处理每个分区里面的内容。每个分区中的内容将以Iterator[T]传递给输入函数f,f的输出结果是Iterator[U]。最终的RDD由所有分区经过输入函数处理后的结果合并起来的。

def myfunc[T](iter: Iterator[T]) :Iterator[(T, T)] = {

    varres = List[(T, T)]()

    varpre = iter.next while (iter.hasNext) {

       val cur = iter.next;

       res .::= (pre, cur) pre = cur;

    }

   res.iterator

}

val rdd5 = rdd1.mapPartitions(myfunc)
rdd5.collect
res5: Array[(Int, Int)] = Array((2,3), (1,2), (5,6), (4,5), (8,9), (7,8))

1.1.6. mapPartitionsWithIndex(func)

Similar to mapPartitions, but also provides func with an integer value representing the index of the partition, so func must be of type (Int, Iterator<T>) => Iterator<U> when running on an RDD of type T.

1.1.7. sample(withReplacement,fraction, seed)

根据给定的随机种子seed,随机抽样出数量为frac的数据。 

1.1.8. union(otherDataset)

返回一个新的数据集,由原数据集和参数联合而成。

val rdd8 = rdd1.union(rdd3)

rdd8.collect

res14: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9,12, 14, 16, 18)

1.1.9. intersection(otherDataset)

返回一个新的数据集,包含两个数据集的交集数据;
val rdd9 = rdd8.intersection(rdd1)
rdd9.collect
res16: Array[Int] = Array(6, 1, 7, 8, 2, 3, 9, 4, 5)

1.1.10. distinct([numTasks]))

返回一个数据集, 两个数据集去除重复数据
val rdd10 = rdd8.union(rdd9).distinct
rdd10.collect
res19: Array[Int] = Array(12, 1, 14, 2, 3, 4, 16, 5, 6, 18, 7, 8, 9)

1.1.11. groupByKey([numTasks])

在一个由(K,V)对组成的数据集上调用,返回一个(K,Seq[V])对的数据集。注意:默认情况下,使用8个并行任务进行分组,你可以传入numTask可选参数,根据数据量设置不同数目的Task。
val rdd0 = sc.parallelize(Array((1,1), (1,2) , (1,3) , (2,1) , (2,2) , (2,3)), 3)
rdd0.collect
res27: Array[(Int, Int)] = Array((1,1), (1,2), (1,3), (2,1), (2,2), (2,3))
val rdd11 = rdd0.groupByKey()
rdd11.collect
res33: Array[(Int, Iterable[Int])] = Array((1,ArrayBuffer(1, 2, 3)), (2,ArrayBuffer(1, 2, 3)))

1.1.12. reduceByKey(func, [numTasks])

在一个(K,V)对的数据集上使用,返回一个(K,V)对的数据集,key相同的值,都被使用指定的reduce函数聚合到一起。和groupbykey类似,任务的个数是可以通过第二个可选参数来配置的。
val rdd12 = rdd0.reduceByKey((x,y) => x + y)
rdd12.collect
res34: Array[(Int, Int)] = Array((1,6), (2,6))

1.1.13. aggregateByKey(zeroValue)(seqOp, combOp, [numTasks])

When called on a dataset of (K, V) pairs, returns a dataset of (K, U) pairs where the values for each key are aggregated using the given combine functions and a neutral "zero" value. Allows an aggregated value type that is different than the input value type, while avoiding unnecessary allocations. Like in groupByKey, the number of reduce tasks is configurable through an optional second argument.

1.1.14. sortByKey([ascending], [numTasks])

按照K排序, 其中K需要实现Ordered。
val rdd14 = rdd0.sortByKey()
rdd14.collect
res36: Array[(Int, Int)] = Array((1,1), (1,2), (1,3), (2,1), (2,2), (2,3))

1.1.15. join(otherDataset, [numTasks])

将输入数据集(K, V)和另外一个数据集(K, W)进行Join, 得到(K, (V, W))
笛卡尔积,V和W的所有组合;
val rdd15 = rdd0.join(rdd0)
rdd15.collect
res37: Array[(Int, (Int, Int))] = Array((1,(1,1)), (1,(1,2)), (1,(1,3)), (1,(2,1)), (1,(2,2)), (1,(2,3)), (1,(3,1)), (1,(3,2)), (1,(3,3)), (2,(1,1)), (2,(1,2)), (2,(1,3)), (2,(2,1)), (2,(2,2)), (2,(2,3)), (2,(3,1)), (2,(3,2)), (2,(3,3)))

1.1.16. cogroup(otherDataset, [numTasks])

将输入数据集(K, V)和另外一个数据集(K, W)进行cogroup, 得到一个格式为(K, Seq[V], Seq[W])的数据集。
val rdd16 = rdd0.cogroup(rdd0)
rdd16.collect
res38: Array[(Int, (Iterable[Int], Iterable[Int]))] = Array((1,(ArrayBuffer(1, 2, 3),ArrayBuffer(1, 2, 3))), (2,(ArrayBuffer(1, 2, 3),ArrayBuffer(1, 2, 3))))

1.1.17. cartesian(otherDataset)

做笛卡尔积: 对于数据集T合U, 得到(T, U)格式的数据集
val rdd17 = rdd1.cartesian(rdd3)
rdd17.collect
res39: Array[(Int, Int)] = Array((1,12), (2,12), (3,12), (1,14), (1,16), (1,18), (2,14), (2,16), (2,18), (3,14), (3,16), (3,18), (4,12), (5,12), (6,12), (4,14), (4,16), (4,18), (5,14), (5,16), (5,18), (6,14), (6,16), (6,18), (7,12), (8,12), (9,12), (7,14), (7,16), (7,18), (8,14), (8,16), (8,18), (9,14), (9,16), (9,18))

1.1.18. pipe(command, [envVars])

Pipe each partition of the RDD through a shell command, e.g. a Perl or bash script. RDD elements are written to the process's stdin and lines output to its stdout are returned as an RDD of strings.

1.1.19. coalesce(numPartitions)

Decrease the number of partitions in the RDD to numPartitions. Useful for running operations more efficiently after filtering down a large dataset.

1.1.20. repartition(numPartitions)

Reshuffle the data in the RDD randomly to create either more or fewer partitions and balance it across them. This always shuffles all data over the network.

1.2. Actions

1.2.1. reduce(func)

对数据集的所有元素执行聚集(func)函数, 该函数必须是可交换的

1.2.2. collect()

将数据集中的所有元素以一个array的形式返回

1.2.3. count()

返回数据集中元素的个数

1.2.4. first()

返回数据集中的第一个元素, 类似于take(1)

1.2.5. take(n)

返回一个包含数据集中前n个元素的数组, 当前该操作不能并行。

1.2.6. takeSample(withReplacement,num, [seed])

返回包含随机的num个元素的数组

1.2.7. takeOrdered(n, [ordering])

返回包含随机的n个元素的数组,按照顺序输出。

1.2.8. saveAsTextFile(path)

把数据集中的元素写到一个文本文件. Spark会对每个元素调用toString方法来把每个元素存成文本文件的一行.

1.2.9. saveAsSequenceFile(path)

把数据集中的元素存成Hadoop SequenceFile的格式. 只有当RDD的格式为键值对时才可以.

1.2.10. saveAsObjectFile(path)

Write the elements of the dataset in a simple format using Java serialization, which can then be loaded usingSparkContext.objectFile().

1.2.11. countByKey()

只有当RDD的格式为键值对时才可以. 返回一个(K, Int)的map, Int为K的个数.

1.2.12. foreach(func)

 对数据集中的每个元素都执行func.
 
 

 转载请注明出处:
http://blog.csdn.net/sunbow0/article/details/41962317

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值