spark RDD算子(八)之键值对关联操作 subtractByKey, join, rightOuterJoin, leftOuterJoin
来源:互联网 发布:显示器尺寸 知乎 编辑:程序博客网 时间:2024/06/12 22:06
先从spark-learning中的一张图大致了解其功能
subtractByKey
函数定义
def subtractByKey[W](other: RDD[(K, W)])(implicit arg0: ClassTag[W]): RDD[(K, V)]def subtractByKey[W](other: RDD[(K, W)], numPartitions: Int)(implicit arg0: ClassTag[W]): RDD[(K, V)]def subtractByKey[W](other: RDD[(K, W)], p: Partitioner)(implicit arg0: ClassTag[W]): RDD[(K, V)]
类似于subtrac,删掉 RDD 中键与 other RDD 中的键相同的元素
join
函数定义
def join[W](other: RDD[(K, W)]): RDD[(K, (V, W))]def join[W](other: RDD[(K, W)], numPartitions: Int): RDD[(K, (V, W))]def join[W](other: RDD[(K, W)], partitioner: Partitioner): RDD[(K, (V, W))]
RDD1.join(RDD2)
可以把RDD1,RDD2中的相同的key给连接起来,类似于sql中的join操作
leftOuterJoin
def leftOuterJoin[W](other: RDD[(K, W)]): RDD[(K, (V, Option[W]))]def leftOuterJoin[W](other: RDD[(K, W)], numPartitions: Int): RDD[(K, (V, Option[W]))]def leftOuterJoin[W](other: RDD[(K, W)], partitioner: Partitioner): RDD[(K, (V, Option[W]))]
直接看图即可
对两个 RDD 进行连接操作,类似于sql中的左外连接
rightOuterJoin
对两个 RDD 进行连接操作,类似于sql中的右外连接,存在的话,value用的Some, 不存在用的None,具体的看上面的图和下面的代码即可
代码示例
scala语言
scala> val rdd = sc.makeRDD(Array((1,2),(3,4),(3,6))) scala> val other = sc.makeRDD(Array((3,9))) scala> rdd.subtractByKey(other).collect() res0: Array[(Int, Int)] = Array((1,2)) scala> rdd.join(other).collect() res1: Array[(Int, (Int, Int))] = Array((3,(4,9)), (3,(6,9))) scala> rdd.leftOuterJoin(other).collect() res2: Array[(Int, (Int, Option[Int]))] = Array((1,(2,None)), (3,(4,Some(9))), (3,(6,Some(9)))) scala> rdd.rightOuterJoin(other).collect() res3: Array[(Int, (Option[Int], Int))] = Array((3,(Some(4),9)), (3,(Some(6),9)))
java语言
JavaRDD<Tuple2<Integer,Integer>> rddPre = sc.parallelize(Arrays.asList(new Tuple2(1,2) , new Tuple2(3,4) , new Tuple2(3,6))); JavaRDD<Tuple2<Integer,Integer>> otherPre = sc.parallelize(Arrays.asList(new Tuple2(3,10))); //JavaRDD转换成JavaPairRDD JavaPairRDD<Integer, Integer> rdd = JavaPairRDD.fromJavaRDD(rddPre); JavaPairRDD<Integer, Integer> other = JavaPairRDD.fromJavaRDD(otherPre); //subtractByKey JavaPairRDD<Integer, Integer> subRDD = rdd.subtractByKey(other); //join JavaPairRDD<Integer, Tuple2<Float, Integer>> joinRDD = rdd.join(other); //leftOuterJoin JavaPairRDD<Integer, Tuple2<Integer, Optional<Integer>>> integerTuple2JavaPairRDD = rdd.leftOuterJoin(other); //rightOutJoin JavaPairRDD<Integer, Tuple2<Optional<Integer>, Integer>> rightOutJoin = rdd.rightOuterJoin(other);
0 0
- spark RDD算子(八)之键值对关联操作 subtractByKey, join, rightOuterJoin, leftOuterJoin
- Spark算子:RDD键值转换操作(5)–leftOuterJoin、rightOuterJoin、subtractByKey
- Spark算子:RDD键值转换操作(5)–leftOuterJoin、rightOuterJoin、subtractByKey
- Spark算子:RDD键值转换操作(5)–leftOuterJoin、rightOuterJoin、subtractByKey
- 3.3 Spark RDD键值转换操作5-leftOuterJoin、rightOuterJoin、subtractByKey
- Spark算子[16]:subtractByKey、join、rightOuterJoin、leftOuterJoin 实例详解
- RDD键值转换操作(5)–leftOuterJoin、rightOuterJoin、subtractByKey
- Spark编程之基本的RDD算子之join,rightOuterJoin, leftOuterJoin
- spark RDD算子(六)之键值对聚合操作reduceByKey,foldByKey,排序操作sortByKey
- spark RDD算子(五)之键值对聚合操作 combineByKey
- spark RDD算子(七)之键值对分组操作 groupByKey,cogroup
- Spark算子:RDD键值转换操作(4)–cogroup/join
- Spark算子:RDD键值转换操作(4)–cogroup、join
- Spark算子:RDD键值转换操作(4)–cogroup、join
- spark RDD算子(四)之创建键值对RDD mapToPair flatMapToPair
- Spark学习之键值对(pair RDD)操作
- Spark 键值对RDD操作
- Spark 的键值对(pair RDD)操作,Scala实现
- java菜鸟的回炉之旅之十一----方法
- 算法导论 思考题 9-3
- tcpdump 简介
- 使用zxing解析二维码抛出com.google.zxing.NotFoundException 解决方案
- lianbiao
- spark RDD算子(八)之键值对关联操作 subtractByKey, join, rightOuterJoin, leftOuterJoin
- C++11资料
- mysql优化方式
- PyQt5程序生成的APP制作成DMG
- Java利用Zxing生成二维码
- 接口
- 多线程编程之线程安全退出
- 吃豆豆进度条_完整版
- SPOJ 694 Distinct Substrings <后缀数组>