如何通过指数星火RDD(Java)来获取元素 [英] How to get element by Index in Spark RDD (Java)

查看:408
本文介绍了如何通过指数星火RDD(Java)来获取元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道方法rdd.first(),它给我的第一个元素RDD。

I know the method rdd.first() which gives me the first element in an RDD.

还有就是方法rdd.take(NUM),这也是我的第一个民的元素。

Also there is the method rdd.take(num) Which gives me the first "num" elements.

不过,是不是有可能通过指数来获得一个元素?

But isn't there a possibility to get an element by index?

感谢。

推荐答案

这应该由第一索引的RDD是可能的。转型'zipWithIndex提供了一个稳定的索引,编号在其原来的顺序每个元素。

This should be possible by first indexing the RDD. The transformation 'zipWithIndex' provides a stable indexing, numbering each element in its original order.

由于: RDD =(A,B,C)

val withIndex = rdd.zipWithIndex // ((a,0),(b,1),(c,2))

要通过查找索引的元素,这种形式是没有用的。首先,我们需要使用索引的关键:

To lookup an element by index, this form is not useful. First we need to use the index as key:

val indexKey = withIndex.map{case (k,v) => (v,k)}  //((0,a),(1,b),(2,c))

现在,它可能使用PairRDD的'查找'行动,找到关键的元素:

Now, it's possible to use the 'lookup' action in PairRDD to find an element by key:

val b = indexKey.lookup(1) // Array(b)

如果你希望使用查找经常在同一RDD,我建议缓存 indexKey RDD以提高性能。

If you're expecting to use lookup often on the same RDD, I'd recommend to cache the indexKey RDD to improve performance.

如何做到这一点使用的Java API 是留给读者的练习。

How to do this using the Java API is an exercise left for the reader.

这篇关于如何通过指数星火RDD(Java)来获取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆