使用 pyspark 比较两个大数据帧

Compare two large dataframes using pyspark

我目前正在处理数据迁移任务,尝试使用 pyspark 比较来自两个不同数据库的两个数据帧,以找出两个数据帧之间的差异,并将结果记录在 csv 文件中作为数据验证的一部分。我正在尝试一种高效的解决方案,因为有两个 reasons.i.e。大数据帧和 table 键未知

#Approach 1 - Not sure about the performance and it is case-sensitive

df1.subtract(df2)

#Approach 2 - Creating row hash for each row in dataframe

piperdd=df1.rdd.map(lambda x: hash(x)) 
r=row("h_cd")
df1_new=piperdd.map(r).toDF() 

我在方法 2 中面临的问题是最终数据帧 (df1_new) 仅检索哈希列 (h_cd) 但我需要数据帧 1(df1) 的所有列和哈希码column(h_cd) 因为我需要在 csv 中报告行差异 file.Please help

试试dataframes,应该更简洁。

df1 = spark.createDataFrame([(a, a*2, a+3) for a in range(10)], "A B C".split(' '))
#df1.show()
from pyspark.sql.functions import hash
df1.withColumn('hash_value', hash('A','B', 'C')).show()

+---+---+---+-----------+
|  A|  B|  C| hash_value|
+---+---+---+-----------+
|  0|  0|  3| 1074520899|
|  1|  2|  4|-2073566230|
|  2|  4|  5| 2060637564|
|  3|  6|  6|-1286214988|
|  4|  8|  7|-1485932991|
|  5| 10|  8| 2099126539|
|  6| 12|  9| -558961891|
|  7| 14| 10| 1692668950|
|  8| 16| 11|  708810699|
|  9| 18| 12|  -11251958|
+---+---+---+-----------+