note 1
这里只是将我学习初期笔记拿来分享,没有做太多精细的推理验证,如有错误,希望指正。
note 2
整个算子系列应用的测试数据是相同的,在本系列第一篇Spark-Operator-Map中有完整的测试数据
note 3
因为工作环境如此,我个人使用Java+Scala混合开发,请知悉
note 4
代码版本
-Spark2.2
-Scala2.11
- 源码
/**
* Return the Cartesian product of this RDD and another one, that is, the RDD of all pairs of
* elements (a, b) where a is in `this` and b is in `other`.
*/
def cartesian[U: ClassTag](other: RDD[U]): RDD[(T, U)] = withScope {
new CartesianRDD(sc, this, other)
}
- 两个rdd取笛卡尔集
- 新的rdd 每个元素为一个二元组
- 没有什么好说的,直接看测试与结果
测试代码
object Cartesian {
val ss = SparkSession.builder().master("local").appName("basic").getOrCreate()
val sc = ss.sparkContext
sc.setLogLevel("error")
def main(args: Array[String]): Unit = {
val rdd = sc.textFile("/home/missingli/IdeaProjects/SparkLearn/src/main/resources/sparkbasic.txt")
val rdd2 = sc.textFile("/home/missingli/IdeaProjects/SparkLearn/src/main/resources/sparkbasic3.txt")
val rdd3 = rdd.cartesian(rdd2)
rdd3.map(r=>r._1+r._2).foreach(println)
}
}
结果
1,a,c,b3,h,r,x
1,a,c,b4,6,s,b
1,a,c,b5,h,d,o
1,a,c,b6,q,w,e
1,a,c,b7,j,s,b
1,a,c,b8,h,m,o
1,a,c,b9,q,w,c
2,w,gd,h3,h,r,x
2,w,gd,h4,6,s,b
2,w,gd,h5,h,d,o
2,w,gd,h6,q,w,e
2,w,gd,h7,j,s,b
2,w,gd,h8,h,m,o
2,w,gd,h9,q,w,c
3,h,r,x3,h,r,x
3,h,r,x4,6,s,b
3,h,r,x5,h,d,o
3,h,r,x6,q,w,e
3,h,r,x7,j,s,b
3,h,r,x8,h,m,o
3,h,r,x9,q,w,c
4,6,s,b3,h,r,x
4,6,s,b4,6,s,b
4,6,s,b5,h,d,o
4,6,s,b6,q,w,e
4,6,s,b7,j,s,b
4,6,s,b8,h,m,o
4,6,s,b9,q,w,c
5,h,d,o3,h,r,x
5,h,d,o4,6,s,b
5,h,d,o5,h,d,o
5,h,d,o6,q,w,e
5,h,d,o7,j,s,b
5,h,d,o8,h,m,o
5,h,d,o9,q,w,c
6,q,w,e3,h,r,x
6,q,w,e4,6,s,b
6,q,w,e5,h,d,o
6,q,w,e6,q,w,e
6,q,w,e7,j,s,b
6,q,w,e8,h,m,o
6,q,w,e9,q,w,c