火花:如何做一个dropDuplicates上的数据帧,同时保持最高的时间戳列 [英] spark: How to do a dropDuplicates on a dataframe while keeping the highest timestamped row

查看:698
本文介绍了火花:如何做一个dropDuplicates上的数据帧,同时保持最高的时间戳列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用的情况下我需要砸数据框的重复行(在这种情况下,重复意味着它们具有相同的ID字段),同时保持最高的时间戳行(UNIX时间戳)域

I have a use case where I'd need to drop duplicate rows of a dataframe (in this case duplicate means they have the same 'id' field) while keeping the row with the highest 'timestamp' (unix timestamp) field.

我发现drop_duplicate方法(我使用pyspark),但有没有哪些项目将被保留控制权。

I found the drop_duplicate method (I'm using pyspark), but one don't have control on which item will be kept.

任何人都可以帮忙吗? THX提前

Anyone can help ? Thx in advance

推荐答案

手动映射,减少可能需要您提供所需的功能。

A manual map and reduce might be needed to provide the functionality you want.

def selectRowByTimeStamp(x,y):
    if x.timestamp > y.timestamp:
        return x
    return y

dataMap = data.map(lambda x: (x.id, x))
uniqueData = dataMap.reduceByKey(selectRowByTimeStamp) 

在这里,我们把所有基于ID的数据。然后,当我们正在减少的分组,我们通过保持纪录最高的时间戳这样做。当code做减少,只有1记录将被留给每个​​ID。

Here we are grouping all of the data based on id. Then, when we are reducing the groupings, we do so by keeping the record with the highest timestamp. When the code is done reducing, only 1 record will be left for each id.

这篇关于火花:如何做一个dropDuplicates上的数据帧,同时保持最高的时间戳列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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