Compare data in two RDD in spark

后端 未结 2 1053
悲哀的现实
悲哀的现实 2021-02-10 05:47

I am able to print data in two RDD with the below code.

usersRDD.foreach(println)
empRDD.foreach(println)

I need to compare data in two RDDs. H

2条回答
  •  太阳男子
    2021-02-10 06:41

    You need to key the data in each RDD so that there is something to join records on. Have a look at groupBy for example. Then you join the resulting RDDs. For each key, you get the matching values in both. If you are interested in finding the unmatched keys, use leftOuterJoin, like this:

    // Returns the entries in userRDD that have no corresponding key in empRDD.
    def nonEmp(userRDD: RDD[(String, String)], empRDD: RDD[(String, String)]) = {
      userRDD.leftOuterJoin(empRDD).collect {
        case (name, (age, None)) => name -> age
      }
    }
    

提交回复
热议问题