繁体   English   中英

Spark转换,可从RDD [ListBuffer]中的ListBuffer提取对象并在RDD中创建新条目

[英]Spark transformation that withdraws objects from a ListBuffer in a RDD[ListBuffer] and creates new entries in the RDD

所以我有一个RDD是RDD[ListBuffer[(Array[String], Long)]] 为简单起见,我们可以称其为RDD[X] ,其中X是一些变量的列表。 Xobj对象X[obj]的列表。

我的想法是我想要一个函数,该函数将RDD[X]作为输入并输出一个新的RDD[X] ,即转换。 这种转换将通过从一个X取出obj并创建一个新的X列表来创建新的X列表,然后像将其“附加”到RDD一样。

我没有在Spark中找到任何直接支持此功能的东西。 现在,我能想到的唯一解决方案是通过执行collect()并在驱动程序中管理其中的大多数功能,但这显然不是一件好事。 有任何想法吗?

基本上是这样的:

val data = RDD[ListBuffer[(Array[String], Long)]]
// some transformation that calls some function
// what will happen is some (Array[String], Long) will be moved into an entirely new ListBuffer in outData while some may be completely removed
val outData = RDD[ListBuffer[(Array[String], Long)]]

假设我们有一个包含一个由7个元素组成的ListBuffer的起始RDD:

Element1(在ListBuffer1中)

Element2(在ListBuffer1中)

Element3(在ListBuffer1中)

Element4(在ListBuffer1中)

Element5(在ListBuffer1中)

Element6(在ListBuffer1中)

Element7(在ListBuffer1中)

转换后,RDD将具有以下内容:

Element1(在ListBuffer1中)

Element2(在ListBuffer1中)

Element4(在ListBuffer2中)

Element5(在ListBuffer2中)

Element6(在ListBuffer2中)

一些元素已被移至RDD中的新ListBuffer中,而两个元素已被完全删除。

我正在使用Spark 1.6.0。

您可以在每个做改造ListBuffer来收集ListBuffer让说, ListListBuffer然后做flatMap上RDD。

下面是虚拟POC。

val rdd = spark.sparkContext.parallelize(Seq(List(1,2,3,4), List(11,22,76,44)))
val flattenRdd = rdd.map(s => List(s.filter(_%2 == 1), s.filter(_%2 == 0)))
    .flatMap(s => s)
flattenRdd.collect().foreach(s => println(s.mkString(",")))

1,3
2,4
11
22,76,44

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM