按文件系统上的给定列对输出进行分区。 如果指定,输出将布局在文件系统上,类似于 Hive 的分区方案。
Syntax
partitionBy(*cols)
参数
| 参数 | 类型 | Description |
|---|---|---|
*cols |
str 或 list | 要分区依据的列的名称。 |
退货
DataFrameWriter
示例
以分区方式将数据帧写入 Parquet 文件,并将其读回。
import tempfile, os
with tempfile.TemporaryDirectory(prefix="partitionBy") as d:
spark.createDataFrame(
[{"age": 100, "name": "Alice"}, {"age": 120, "name": "Ruifeng Zheng"}]
).write.partitionBy("name").mode("overwrite").format("parquet").save(d)
spark.read.parquet(d).sort("age").show()
# +---+-------------+
# |age| name|
# +---+-------------+
# |100| Alice|
# |120|Ruifeng Zheng|
# +---+-------------+
# Read one partition as a DataFrame.
spark.read.parquet(f"{d}{os.path.sep}name=Alice").show()
# +---+
# |age|
# +---+
# |100|
# +---+