Pyspark删除没有筛选的RDD行

时间:2019-07-18 18:23:57

标签: python pyspark

我编写了一个Pyspark程序,该程序获取同一输入文件的两个相同副本,并将数据转换为两个新文件,每个文件具有各自的格式。我将两个文件读入包含相同行数的数据框。之后,我将该数据帧改回RDD,并应用不同的映射逻辑来转换行的字段(映射时不应用任何过滤器)。但是,输出数据帧包含的行数不相同-丢弃时没有说明。

我试图更改逻辑顺序,在各个阶段打印出行数,等等。日志中没有错误或警告,只有我自己的打印语句显示行数减少。

print("Input rows (f2): " + str(f2_df_count))
print("Input rows (f1): " + str(f1_df_count))


f2_rdd = f2_temp_df.rdd.map(list).map(lambda line:
    ("A",
    line[52].strip(),
    ...
    line[2].zfill(5))
f2_df = sqlContext.createDataFrame(f2_rdd, f2_SCHEMA).dropDuplicates()
f2_df.write.format(OUTPUT_FORMAT).options(delimiter='|').save(f2_OUTPUT)
f2_count = f2_df.count()


f1_rdd = f1_temp_df.rdd.map(list).map(lambda line:
    ("B",
    line[39],
    ...
    line[13] if line[16] != "D" else "C")
f1_df = sqlContext.createDataFrame(f1_rdd, f1_SCHEMA).dropDuplicates()
f1_df.write.format(OUTPUT_FORMAT).options(delimiter='|').save(f1_OUTPUT)
f1_count = f1_df.count()


print("F2 output rows: " + str(f2_count) + " rows (dropped " + str(f2_df_count - f2_count) + ").")
print("F1 output rows: " + str(f1_count) + " rows (dropped " + str(f1_df_count - f1_count) + ").")

没有错误消息,但是我的日志清楚地表明行已删除。更奇怪的是,它们被不一致地掉落了。 f1与f2丢失的行数不同。

Input rows (f2): 261
Input rows (f1): 261
F2 output rows: 260 rows (dropped 1).
F1 output rows: 259 rows (dropped 2).

有时在较大的运行量上差异较大,大约为100-200行。如果有人能解释可能发生的事情以及如何解决这个问题,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

答案是,我假设以前删除了重复项,但是在将RDD重新创建为数据帧之后,我添加了一个额外的dropDuplicate()调用。抱歉,不必要地花时间在此上的任何人!

相关问题