问题
Say I have a pyspark dataframe 'data' as follows. I want to partition the data by "Period". Rather I want each period of data to be stored on it's own partition (see the example below the 'data' dataframe below).
data = sc.parallelize([[1,1,0,14277.4,0], \
[1,2,0,14277.4,0], \
[2,1,0,4741.91,0], \
[2,2,0,4693.03,0], \
[3,1,2,9565.93,0], \
[3,2,2,9566.05,0], \
[4,2,0,462.68,0], \
[5,1,1,3549.66,0], \
[5,2,5,3549.66,1], \
[6,1,1,401.52,0], \
[6,2,0,401.52,0], \
[7,1,0,1886.24,0], \
[7,2,0,1886.24,0]]) \
.toDF(("Acct","Period","Status","Bal","CloseFlag"))
data.show(100)
+----+------+------+-------+---------+
|Acct|Period|Status| Bal|CloseFlag|
+----+------+------+-------+---------+
| 1| 1| 0|14277.4| 0|
| 1| 2| 0|14277.4| 0|
| 2| 1| 0|4741.91| 0|
| 2| 2| 0|4693.03| 0|
| 3| 1| 2|9565.93| 0|
| 3| 2| 2|9566.05| 0|
| 4| 2| 0| 462.68| 0|
| 5| 1| 1|3549.66| 0|
| 5| 2| 5|3549.66| 1|
| 6| 1| 1| 401.52| 0|
| 6| 2| 0| 401.52| 0|
+----+------+------+-------+---------+
For Example
Partition 1:
+----+------+------+-------+---------+
|Acct|Period|Status| Bal|CloseFlag|
+----+------+------+-------+---------+
| 1| 1| 0|14277.4| 0|
| 2| 1| 0|4741.91| 0|
| 3| 1| 2|9565.93| 0|
| 5| 1| 1|3549.66| 0|
| 6| 1| 1| 401.52| 0|
+----+------+------+-------+---------+
Partition 2:
+----+------+------+-------+---------+
|Acct|Period|Status| Bal|CloseFlag|
+----+------+------+-------+---------+
| 1| 2| 0|14277.4| 0|
| 2| 2| 0|4693.03| 0|
| 3| 2| 2|9566.05| 0|
| 4| 2| 0| 462.68| 0|
| 5| 2| 5|3549.66| 1|
| 6| 2| 0| 401.52| 0|
+----+------+------+-------+---------+
回答1:
The approach should be to repartition first, to have the right number of partitions(number of unique periods), and then partition by the Period column before saving it.
from pyspark.sql import functions as F
n = data.select(F.col('Period')).distinct().count()
data.repartition(n)\
.write \
.partitionBy("Period")\
.mode("overwrite")\
.format("parquet")\
.saveAsTable("testing")
来源:https://stackoverflow.com/questions/60048027/how-to-manage-physical-data-placement-of-a-dataframe-across-the-cluster-with-pys