[英]How to drop duplicates while using write.partitionBy in a pyspark dataframe?
我有一個 dataframe,如下所示:
|------------|-----------|---------------|---------------|
| Name | Type | Attribute 1 | Attribute 2 |
|------------|-----------|---------------|---------------|
| Roger | A | X | Y |
|------------|-----------|---------------|---------------|
| Roger | A | X | Y |
|------------|-----------|---------------|---------------|
| Roger | A | X | Y |
|------------|-----------|---------------|---------------|
| Rafael | A | G | H |
|------------|-----------|---------------|---------------|
| Rafael | A | G | H |
|------------|-----------|---------------|---------------|
| Rafael | B | G | H |
|------------|-----------|---------------|---------------|
我想對這個 dataframe 進行分區並根據名稱和類型將其保存到磁盤
這行代碼目前看起來像這樣,
df.write.partitionBy("Name", "Type").mode("append").csv("output/", header=True)
output 已正確保存,但具有重復的行,如下所述
在文件夾中
/輸出/羅傑/A
|---------------|---------------|
| Attribute 1 | Attribute 2 |
|---------------|---------------|
| X | Y |
|---------------|---------------|
| X | Y |
|---------------|---------------|
| X | Y |
|---------------|---------------|
/輸出/拉斐爾/A
|---------------|---------------|
| Attribute 1 | Attribute 2 |
|---------------|---------------|
| G | H |
|---------------|---------------|
| G | H |
|---------------|---------------|
/輸出/拉斐爾/B
|---------------|---------------|
| Attribute 1 | Attribute 2 |
|---------------|---------------|
| G | H |
|---------------|---------------|
如您所見,這個 csv 包含重復項。 如何在使用 write.partitionbY 時刪除這些重復項?
在編寫之前使用.distinct()
。
df.distinct().write.partitionBy("Name", "Type").mode("append").csv("output/", header=True)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.