简体   繁体   English

删除每个分区的重复项

[英]Drop duplicates for each partition

origin data原始数据

cls, id  
----
a, 1
a, 1
----
b, 3
b, 3
b, 4

expected output预期产出

cls, id  
----
a, 1
----
b, 3
b, 4

id can be duplicates only in same cls, It means same id do not exist across clses. id 只能在相同的 cls 中重复,这意味着跨 clses 不存在相同的 id。

In that case.在这种情况下。

df.dropDuplicates($id) 

will shuffle across all partitions to check duplicates over cls.将在所有分区中随机播放以检查 cls 上的重复项。 and repartitioned to 200(default value)并重新分区为 200(默认值)

Now, How can I run dropDuplicates for each partition seperately to reduce computing cost?现在,如何分别为每个分区运行 dropDuplicates 以降低计算成本?

something like就像是

df.foreachPartition(_.dropDuplicates())

You're probably after something like this:你可能在追求这样的事情:

val distinct = df.mapPartitions(it => {
    val set = Set();
    while (it.hasNext) {
        set += it.next()
    }
    return set.iterator
});

Not not a with set.不是没有设置。 In fact Set is too dangerous if the size of the data is huge.事实上,如果数据量很大,Set 就太危险了。 One option that you can think of is adding mapPartitionsWithIndex and add the index as an output iterator.您可以想到的一种选择是添加 mapPartitionsWithIndex 并将索引添加为输出迭代器。 This way in your DF, the partition index exist.这样在你的 DF 中,分区索引就存在了。 Later, apply drop duplicates by passing partition number and the other key.稍后,通过传递分区号和另一个键来应用删除重复项。 Ideally, for the combination of the key and map partition the duplicate records get removed.理想情况下,对于键和映射分区的组合,重复记录被删除。

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

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