Spark-Operator-cartesian

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值