首页 > 解决方案 > 比较不等于比较列列表的 Spark 数据帧

问题描述

我目前正在尝试将两个数据帧一起比较,以查看 pyspark 中的字段如何不匹配。我已经能够手动编写它,但我希望能够传递字段列表以确保帧在字段上不匹配。数据帧是相同的。

到目前为止我的代码是:

key_cols = ['team_link_uuid', 'team_sat_hash']
temp_team_sat = orig.select(*key_cols)
temp_team_sat_incremental = delta.select(*key_cols)
hash_field = ['team_sat_hash']

test_update_list = temp_team_sat.join(temp_team_sat_incremental, (temp_team_sat.team_link_uuid == temp_team_sat_incremental.team_link_uuid) & (temp_team_sat.team_sat_hash != temp_team_sat_incremental.team_sat_hash))

但是现在我需要能够获取我的列表(hash_field)并且能够确保一个或多个字段彼此不相等。

标签: apache-sparkjoinpyspark

解决方案


假设fields_to_compare_list是您要比较的字段列表,

from functools import reduce

comparaison_query = reduce(
    lambda a,b : (a | b),
    [ temp_team_sat[col] != temp_team_sat_incremental[col] 
      for col 
      in fields_to_compare_list
    ]
)

test_update_list = temp_team_sat.join(
    temp_team_sat_incremental, 
    on = (temp_team_sat.team_link_uuid == temp_team_sat_incremental.team_link_uuid) \
         & comparaison_query

推荐阅读