请查看groovy中的以下代码段:
def static void main(String... args) {
def arr = [1, 2, 3, 4, 5]
for (int f in arr) {
Thread.start { print f + ', '}
}
}
Out: 2, 3, 5, 5, 5,
Run Code Online (Sandbox Code Playgroud)
我对此输出感到惊讶.为什么"5"被多次打印?而且,在Java中运行等效代码的一切看起来都很好:
public static void main(String[] args) {
int[] arr = new int[]{1, 2, 3, 4, 5};
for (int f : arr) {
new Thread(() -> { System.out.print(f + ", "); }).start();
}
}
Out: 1, 5, 4, 3, 2,
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释为什么会这样吗?看起来groovy的问题在Closure实现中.但是,这种行为很奇怪.它是某种bug还是我没有意识到groovy是如何工作的? …
我有 RDD,其中每条记录都是 int:
[0,1,2,3,4,5,6,7,8]
Run Code Online (Sandbox Code Playgroud)
我需要做的就是将这个 RDD 分成几批。即制作另一个 RDD,其中每个元素都是固定大小的元素列表:
[[0,1,2], [3,4,5], [6,7,8]]
Run Code Online (Sandbox Code Playgroud)
这听起来微不足道,但是,我最近几天感到困惑,除了以下解决方案之外找不到任何东西:
使用 ZipWithIndex 枚举 RDD 中的记录:
[0,1,2,3,4,5] -> [(0, 0),(1, 1),(2, 2),(3, 3),(4, 4),(5, 5)]
使用 map() 迭代这个 RDD 并计算索引 index = int(index / batchSize)
[1,2,3,4,5,6] -> [(0, 0),(0, 1),(0, 2),(1, 3),(1, 4),(1, 5)]
然后按生成的索引分组。
[(0, [0,1,2]), (1, [3,4,5])]
这将为我提供我需要的东西,但是,我不想在这里使用 group。当您使用普通 Map Reduce 或某些抽象(如 Apache Crunch)时,这很简单。但是有没有办法在不使用重分组的情况下在 Spark 中产生类似的结果?