簡體   English   中英

spark:在另一個rdd內部訪問rdd

[英]spark: access rdd inside another rdd

我有一個大小為6000的查找rdd,lookup_rdd:RDD [String]

a1 a2 a3 a4 a5 .....

另一個rdd,data_rdd:RDD [(String,Iterable [(String,Int)])] :( id,(item,count))具有唯一的ID,

(id1,List((a1,2), (a3,4))) (id2,List((a2,1), (a4,2), (a1,1))) (id3,List((a5,1)))

lookup_rdd中的FOREACH元素我想檢查每個id是否具有該元素,如果有,我放入計數,如果不是,則放入0,然后存儲在文件中。

什么是實現此目標的有效方法。 可能進行散列嗎? 例如。 我想要的輸出是:

id1,2,0,4,0,0 id2,1,1,0,2,0 id3,0,0,0,0,1

我已經試過了:

val headers = lookup_rdd.zipWithIndex().persist()  
val indexing = data_rdd.map{line =>
  val id = line._1
  val item_cnt_list = line._2
  val arr = Array.fill[Byte](6000)(0)
  item_cnt_list.map(c=>(headers.lookup(c._1),c._2))
  }
indexing.collect().foreach(println)

我得到例外:

org.apache.spark.SparkException: RDD transformations and actions can only be invoked by the driver, not inside of other transformations

壞消息是您不能在另一個內部使用RDD。

好消息是,對於您的用例,假設6000個條目很小,那么有一個理想的解決方案:在驅動程序上收集RDD,將其廣播回集群的每個節點,並在您使用其他RDD時使用它以前做過。

val sc: SparkContext = ???
val headers = sc.broadcast(lookup_rdd.zipWithIndex.collect().toMap)
val indexing = data_rdd.map { case (_, item_cnt_list ) =>
  item_cnt_list.map { case (k, v) => (headers.value(k), v) }
}
indexing.collect().foreach(println)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM