这是一种方法。假设你的 RDDs 都是 RDD[(String, String)] 类型,而你的第一个 RDD 的大小更小。
//create your first RDD
val rdd1: RDD[(String, String)] = sc.parallelize(Seq(
("111", "A"),
("112", "B"),
("113", "C"),
("114", "D"),
("115", "E"),
("116", "F"),
("117", "G")))
//as this rdd is small so collect it and convert it to a map
val mapRdd1: Map[String, String] = rdd1.collect.toMap
//broadcast this map to all executors
val bRdd = sc.broadcast(mapRdd1)
//create your second rdd
val rdd2: RDD[(String, String)] = sc.parallelize(Seq(
("111", "112:0.75,114:0.43,117:0.21"),
("112", "113:0.67,114:0.48,115:0.34,116:0.12")))
val result: RDD[(String, String)] = rdd2.map(x => (x._1, //keep first string as it is
x._2.split(",").map(a => a.split(":")) //split second string for the required transformations
//fetch the value from the bradcasted map
.map(t => (bRdd.value(t.head), t.last)).mkString(" ")))
result.foreach(println(_))
//output
//(111,(B,0.75) (D,0.43) (G,0.21))
//(112,(C,0.67) (D,0.48) (E,0.34) (F,0.12))
这假定rdd2 的所有含义都存在于您的第一个RDD 中。如果没有,则在从地图中获取值时使用bRdd.value.getOrElse(t.head,"DEFAULT_VALUE")。