RDD Transformation Functions
Use syntax:
1
<RDD Variable>.<transformation function>(func)
Copied!

map(func)

Return a new distributed dataset formed by passing each element of the source through a function func.
1
val rdd=sc.parallelize(List(1,2,3,4,5))
2
rdd.map(x=>x+1).collect.foreach(println)
3
/*
4
2
5
3
6
4
7
5
8
6
9
*/
Copied!

filter(func)

Return a new dataset formed by selecting those elements of the source on which func returns true.
1
val rdd=sc.parallelize(List(1,2,3,4,5))
2
rdd.filter(x=>x%2==0).collect.foreach(println)
3
/*
4
2
5
4
6
*/
Copied!

flatMap(func)

Similar to map, but each input item can be mapped to 0 or more output items (so func should return a Seq rather than a single item).
1
val rdd=sc.parallelize(List("1 2","3 4","5 6"))
2
rdd.collect.foreach(println)
3
/*
4
1 2
5
3 4
6
5 6
7
*/
8
rdd.flatMap(x=>x.split(' ')).collect.foreach(println)
9
/*
10
1
11
2
12
3
13
4
14
5
15
6
16
*/
Copied!

mapPartitions(func)

Similar to map, but runs separately on each partition (block) of the RDD, so func must be of type Iterator => Iterator when running on an RDD of type T. (Note: T is datatype placeholder, can be Int, String, etc)
1
val rdd = sc.parallelize(List(1,2,3,4,5,6),2)
2
val rdd2= rdd.mapPartitions( (i: Iterator[Int]) =>
3
i.toList.toIterator)
Copied!

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) => Iterator when running on an RDD of type T.
1
val rdd = sc.parallelize(Seq(1,2,3,4,5),3)
2
rdd.mapPartitionsWithIndex(
3
(index: Int, i: Iterator[Int]) => i.toList
4
.map(x => index + ", "+x).iterator)
5
.collect.foreach(println)
6
/*
7
0, 1
8
1, 2
9
1, 3
10
2, 4
11
2, 5
12
*/
Copied!

sample(withReplacement, fraction, seed)

Sample a fraction of the data, with or without replacement, using a given random number generator seed.
withReplacement, Boolean argument, true or false. If true, same element can appear in the sample output; if false, same element can not appear in the sample output.
1
val rdd = sc.parallelize(Seq(1,2,3,4,5))
2
rdd.sample(true,0.7,1).collect.foreach(println)
3
/*
4
1
5
1
6
2
7
*/
Copied!

union(otherDataset)

Return a new dataset that contains the union of the elements in the source dataset and the argument.
1
val rdd = sc.parallelize(Seq(1,2,3,4,5))
2
rdd.union(sc.parallelize(Seq(10,11,12,13,14)))
3
.collect.foreach(println)
4
5
/*
6
1
7
2
8
3
9
4
10
5
11
10
12
11
13
12
14
13
15
14
16
*/
Copied!

intersection(otherDataset)

Return a new RDD that contains the intersection of elements in the source dataset and the argument.
1
sc.parallelize(Seq(1,2,3,4,5))
2
.intersection(sc.parallelize(Seq(10,11,3,4,15)))
3
.collect.foreach(println)
4
​
5
/*
6
4
7
3
8
*/
Copied!

distinct([numPartitions]))

Return a new dataset that contains the distinct elements of the source dataset.
1
sc.parallelize(Seq(1,2,3,4,5,5,6,5,5,4,3))
2
.distinct.collect
3
.foreach(println)
4
5
/*
6
4
7
6
8
2
9
1
10
3
11
5
12
*/
13
​
Copied!

groupByKey([numPartitions])

When called on a dataset of (K, V) pairs, returns a dataset of (K, Iterable) pairs.
Note: If you are grouping in order to perform an aggregation (such as a sum or average) over each key, using reduceByKey or aggregateByKey will yield much better performance.
Note: By default, the level of parallelism in the output depends on the number of partitions of the parent RDD. You can pass an optional numPartitions argument to set a different number of tasks.
1
sc.parallelize(Seq(1,2,3,4,5,5,6,5,5,4,3))
2
.map(x=>(x,1))
3
.groupByKey
4
.collect.foreach(print)
5
​
6
/*
7
(4,CompactBuffer(1, 1))(6,CompactBuffer(1))(2,CompactBuffer(1))(1,CompactBuffer(1))(3,CompactBuffer(1, 1))(5,CompactBuffer(1, 1, 1, 1))
8
*/
Copied!

reduceByKey(func, [numPartitions])

When called on a dataset of (K, V) pairs, returns a dataset of (K, V) pairs where the values for each key are aggregated using the given reduce function func, which must be of type (V,V) => V. Like in groupByKey, the number of reduce tasks is configurable through an optional second argument.
1
sc.parallelize(Seq(1,2,3,4,5,5,6,5,5,4,3))
2
.map(x=>(x,1)).reduceByKey((a,b)=>a+b)
3
.collect.foreach(println)
4
5
//or
6
​
7
sc.parallelize(Seq(1,2,3,4,5,5,6,5,5,4,3))
8
.map(x=>(x,1)).reduceByKey(_+_)
9
.collect.foreach(println)
10
​
11
/*
12
(4,2)
13
(6,1)
14
(2,1)
15
(1,1)
16
(3,2)
17
(5,4)
18
*/
Copied!

aggregateByKey(zeroValue)(seqOp, combOp, [numPartitions])

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.
As you can see, it takes 4 arguments:
1st argument if 0, it is aggregation for sum; if Double.MaxValue, it is aggregation for minimum; if Double.MinValue, it is aggregation for maximum.
2nd argument is reduce operation within partition
3rd argument is comnining reduce operation across partitions
4th argument is optional, limit to number of Partitions
1
sc.parallelize(Seq(1,2,3,4,5,5,6,5,5,4,3))
2
.map(x=>(x,1)).aggregateByKey(0)((a,b)=>a+b,(c,d)=>c+d)
3
.collect.foreach(println)
4
5
//or
6
​
7
sc.parallelize(Seq(1,2,3,4,5,5,6,5,5,4,3))
8
.map(x=>(x,1)).aggregateByKey(0)(_+_,_+_)
9
.collect.foreach(println)
10
11
/*
12
(4,2)
13
(6,1)
14
(2,1)
15
(1,1)
16
(3,2)
17
(5,4)
18
*/
Copied!

join(otherDataset, [numPartitions])

When called on datasets of type (K, V) and (K, W), returns a dataset of (K, (V, W)) pairs with all pairs of elements for each key. Outer joins are supported through leftOuterJoin, rightOuterJoin, and fullOuterJoin.
1
val emp = sc.parallelize(Seq(("david",10), ("jack",20)
2
,("mary",30)))
3
val dept = sc.parallelize(Seq(("hr",10)
4
, ("accounting",20), ("sales",30)))
5
val emp_dept = emp.map(t => (t._2, t._1))
6
val dept_dept = dept.map(t => (t._2,t._1))
7
​
8
emp_dept.join(dept_dept).collect.foreach(println)
9
​
10
/*
11
(30,(mary,sales))
12
(20,(jack,accounting))
13
(10,(david,hr))
14
*/
15
​
16
emp_dept.fullOuterJoin(dept_dept).collect.foreach(println)
17
/*
18
(30,(Some(mary),Some(sales)))
19
(20,(Some(jack),Some(accounting)))
20
(10,(Some(david),Some(hr)))
21
*/
22
​
23
emp_dept.leftOuterJoin(dept_dept).collect.foreach(println)
24
​
25
/*
26
(30,(mary,Some(sales)))
27
(20,(jack,Some(accounting)))
28
(10,(david,Some(hr)))
29
*/
30
​
31
emp_dept.rightOuterJoin(dept_dept).collect.foreach(println)
32
/*
33
(30,(Some(mary),sales))
34
(20,(Some(jack),accounting))
35
(10,(Some(david),hr))
36
*/
37
​
38
​
Copied!

cogroup(otherDataset, [numPartitions])

When called on datasets of type (K, V) and (K, W), returns a dataset of (K, (Iterable, Iterable)) tuples. This operation is also called groupWith.
1
val emp = sc.parallelize(Seq(("david",10)
2
, ("jack",20), ("mary",30)))
3
val dept = sc.parallelize(Seq(("hr",10)
4
, ("accounting",20), ("sales",30)))
5
val emp_dept = emp.map(t => (t._2, t._1))
6
val dept_dept = dept.map(t => (t._2,t._1))
7
emp_dept.cogroup(dept_dept)
8
.collect.foreach(println)
9
/*
10
(30,(CompactBuffer(mary),CompactBuffer(sales)))
11
(20,(CompactBuffer(jack),CompactBuffer(accounting)))
12
(10,(CompactBuffer(david),CompactBuffer(hr)))
13
*/
Copied!

cartesian(otherDataset)

When called on datasets of types T and U, returns a dataset of (T, U) pairs (all pairs of elements).
1
sc.parallelize(Seq(1,2,3))
2
.cartesian(sc.parallelize(Seq(6,7)))
3
.collect.foreach(println)
4
​
5
/*
6
(1,6)
7
(1,7)
8
(2,6)
9
(3,6)
10
(2,7)
11
(3,7)
12
*/
Copied!

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
sc.parallelize(Seq(1,2,3)).pipe("more")
2
.foreach(println)
3
​
4
/*
5
2
6
3
7
1
8
*/
Copied!

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
val rdd=sc.parallelize(Seq(0 until 99),4)
2
rdd.partitions.size
3
//4
4
rdd.coalesce(2).partitions.size
5
//2
Copied!

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
sc.parallelize(Seq(1,2,3,4,5,5,6,5,5,4,3)).repartition(3)
2
.collect
3
.foreach(println)
4
​
5
/*
6
3
7
6
8
4
9
1
10
4
11
5
12
3
13
2
14
5
15
5
16
5
17
*/
Copied!

repartitionAndSortWithinPartitions(partitioner)

Repartition the RDD according to the given partitioner and, within each resulting partition, sort records by their keys. This is more efficient than calling repartition and then sorting within each partition because it can push the sorting down into the shuffle machinery.
1
val rdd=sc.parallelize(Seq(40,10,30,20,50))
2
.zipWithIndex()
3
rdd.collect
4
//res17: Array[(Int, Long)] = Array((40,0), (10,1), (30,2), (20,3), (50,4))
5
//Notice unsorted pair
6
import org.apache.spark.HashPartitioner
7
rdd.repartitionAndSortWithinPartitions
8
(new HashPartitioner(1)).collect
9
//Sorted
10
//res16: Array[(Int, Long)] = Array((10,1), (20,3), (30,2), (40,0), (50,4))
Copied!
Last modified 1yr ago
Copy link