日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Spark的RDD操作之Join大全

發布時間:2024/1/17 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spark的RDD操作之Join大全 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、RDD的Join操作有哪些?

?

(一)Join:Join類似于SQL的inner join操作,返回結果是前面和后面集合中配對成功的,過濾掉關聯不上的。源代碼如下:

  • /**

  • * Return an RDD containing all pairs of elements with matching keys in `this` and `other`. Each

  • * pair of elements will be returned as a (k, (v1, v2)) tuple, where (k, v1) is in `this` and

  • * (k, v2) is in `other`. Uses the given Partitioner to partition the output RDD.

  • */

  • def join[W](other: RDD[(K, W)], partitioner: Partitioner): RDD[(K, (V, W))] = self.withScope {

  • this.cogroup(other, partitioner).flatMapValues( pair =>

  • for (v <- pair._1.iterator; w <- pair._2.iterator) yield (v, w)

  • )

  • }

  • ?

    (二)leftOuterJoin:leftOuterJoin類似于SQL中的左外關聯left outer join,返回結果以前面的RDD為主,關聯不上的記錄為空。聲明如下:

    ?

  • /**

  • * Perform a left outer join of `this` and `other`. For each element (k, v) in `this`, the

  • * resulting RDD will either contain all pairs (k, (v, Some(w))) for w in `other`, or the

  • * pair (k, (v, None)) if no elements in `other` have key k. Uses the given Partitioner to

  • * partition the output RDD.

  • */

  • def leftOuterJoin[W](

  • other: RDD[(K, W)],

  • partitioner: Partitioner): RDD[(K, (V, Option[W]))] = self.withScope {

  • this.cogroup(other, partitioner).flatMapValues { pair =>

  • if (pair._2.isEmpty) {

  • pair._1.iterator.map(v => (v, None))

  • } else {

  • for (v <- pair._1.iterator; w <- pair._2.iterator) yield (v, Some(w))

  • }

  • }

  • }


  • ?

    ?(三)rightOuterJoin:rightOuterJoin類似于SQL中的有外關聯right outer join,返回結果以參數也就是右邊的RDD為主,關聯不上的記錄為空。聲明如下:

  • /**

  • * Perform a right outer join of `this` and `other`. For each element (k, w) in `other`, the

  • * resulting RDD will either contain all pairs (k, (Some(v), w)) for v in `this`, or the

  • * pair (k, (None, w)) if no elements in `this` have key k. Uses the given Partitioner to

  • * partition the output RDD.

  • */

  • def rightOuterJoin[W](other: RDD[(K, W)], partitioner: Partitioner)

  • : RDD[(K, (Option[V], W))] = self.withScope {

  • this.cogroup(other, partitioner).flatMapValues { pair =>

  • if (pair._1.isEmpty) {

  • pair._2.iterator.map(w => (None, w))

  • } else {

  • for (v <- pair._1.iterator; w <- pair._2.iterator) yield (Some(v), w)

  • }

  • }

  • }

  • ?

    二、實戰操作

    ?

    下面我們用一個非常簡單的栗子,來進行比較說明:

    首先rdd1是一個行業基本RDD,包含ID和行業名稱,rdd2是一個行業薪水RDD,包含ID和薪水。

    [plain]?view plain?copy

  • <code?class="language-plain">//設置運行環境??
  • ????val?conf?=?new?SparkConf().setAppName("SparkRDDJoinOps").setMaster("local[4]")??
  • ????val?sc?=?new?SparkContext(conf)??
  • ????//建立一個基本的鍵值對RDD,包含ID和名稱,其中ID為1、2、3、4??
  • ????val?rdd1?=?sc.makeRDD(Array(("1","Spark"),("2","Hadoop"),("3","Scala"),("4","Java")),2)??
  • ????//建立一個行業薪水的鍵值對RDD,包含ID和薪水,其中ID為1、2、3、5??
  • ????val?rdd2?=?sc.makeRDD(Array(("1","30K"),("2","15K"),("3","25K"),("5","10K")),2)??
  • ??
  • ????println("//下面做Join操作,預期要得到(1,×)、(2,×)、(3,×)")??
  • ????val?joinRDD=rdd1.join(rdd2).collect.foreach(println)??
  • ??
  • ????println("//下面做leftOutJoin操作,預期要得到(1,×)、(2,×)、(3,×)、(4,×)")??
  • ????val?leftJoinRDD=rdd1.leftOuterJoin(rdd2).collect.foreach(println)??
  • ????println("//下面做rightOutJoin操作,預期要得到(1,×)、(2,×)、(3,×)、(5,×)")??
  • ????val?rightJoinRDD=rdd1.rightOuterJoin(rdd2).collect.foreach(println)??
  • ??
  • ????sc.stop()</code>??

  • ?

    三、結果如下:

    ?

    ?

    ?

  • <span style="font-size:18px;">//下面做Join操作,預期要得到(1,×)、(2,×)、(3,×)

  • (2,(Hadoop,15K))

  • (3,(Scala,25K))

  • (1,(Spark,30K))

  • //下面做leftOutJoin操作,預期要得到(1,×)、(2,×)、(3,×)、(4,×)

  • (4,(Java,None))

  • (2,(Hadoop,Some(15K)))

  • (3,(Scala,Some(25K)))

  • (1,(Spark,Some(30K)))

  • //下面做rightOutJoin操作,預期要得到(1,×)、(2,×)、(3,×)、(5,×)

  • (2,(Some(Hadoop),15K))

  • (5,(None,10K))

  • (3,(Some(Scala),25K))

  • (1,(Some(Spark),30K))</span>

  • 結果就證明了我們的預期。

    總結

    以上是生活随笔為你收集整理的Spark的RDD操作之Join大全的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。