gpt4 book ai didi

scala - Spark zipWithIndex 在并行实现中是否安全?

转载 作者:行者123 更新时间:2023-12-04 14:26:16 25 4
gpt4 key购买 nike

如果我有一个文件,并且我每行做了一个 RDD zipWithIndex,

([row1, id1001, name, address], 0)
([row2, id1001, name, address], 1)
...
([row100000, id1001, name, address], 100000)

如果我重新加载文件,我能否获得相同的索引顺序?由于它并行运行,其他行可能会以不同方式分区?

最佳答案

RDD s 可以排序,所以也有顺序。此命令用于创建索引 .zipWithIndex() .

每次获得相同的顺序取决于之前的调用在您的程序中执行的操作。文档提到 .groupBy()可以破坏顺序或生成不同的顺序。可能还有其他调用也可以执行此操作。

我想你可以随时调用.sortBy()在调用 .zipWithIndex() 之前如果您需要保证特定的订购。

这在 .zipWithIndex() scala API docs 中有解释。

public RDD<scala.Tuple2<T,Object>> zipWithIndex() Zips this RDD with its element indices. The ordering is first based on the partition index and then the ordering of items within each partition. So the first item in the first partition gets index 0, and the last item in the last partition receives the largest index. This is similar to Scala's zipWithIndex but it uses Long instead of Int as the index type. This method needs to trigger a spark job when this RDD contains more than one partitions.

Note that some RDDs, such as those returned by groupBy(), do not guarantee order of elements in a partition. The index assigned to each element is therefore not guaranteed, and may even change if the RDD is reevaluated. If a fixed ordering is required to guarantee the same index assignments, you should sort the RDD with sortByKey() or save it to a file.

关于scala - Spark zipWithIndex 在并行实现中是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31846233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com