工作在RDD邻居元素星火 [英] Operate neighbor elements in RDD in Spark

查看:183
本文介绍了工作在RDD邻居元素星火的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我有一个集合:

List(1, 3,-1, 0, 2, -4, 6)

这很容易使得它排序为:

It's easy to make it sorted as:

List(-4, -1, 0, 1, 2, 3, 6)

3,3 - - 2,2 - 1,1 -

然后我可以计算6构造一个新的集合0,等等这样的:

Then I can construct a new collection by compute 6 - 3, 3 - 2, 2 - 1, 1 - 0, and so on like this:

for(i <- 0 to list.length -2) yield {
    list(i + 1) - list(i)
}

和得到一个向量:

Vector(3, 1, 1, 1, 1, 3)

这就是我想要的下一个元素减去当前元素。

That is, I want to make the next element minus the current element.

但如何在星火实现这RDD?

But how to implement this in RDD on Spark?

我知道了系列:

List(-4, -1, 0, 1, 2, 3, 6)

有将之收藏的一些分区,每个分区是有序的,我能做的每个分区上的类似的操作,并收集在每个分区的结果一起?

There will be some partitions of the collection, each partition is ordered, can I do the similar operation on each partition and collect results on each partition together?

推荐答案

最有效的解决方案是使用滑动方法:

The most efficient solution is to use sliding method:

import org.apache.spark.mllib.rdd.RDDFunctions._

val rdd = sc.parallelize(Seq(1, 3,-1, 0, 2, -4, 6))
  .sortBy(identity)
  .sliding(2)
  .map{case Array(x, y) => y - x}

这篇关于工作在RDD邻居元素星火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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