用於分區的列不包含在序列化數據本身中。例如,如果你創建DataFrame
這樣的:
df = sc.parallelize([
(1, "foo", 2.0, "2016-02-16"),
(2, "bar", 3.0, "2016-02-16")
]).toDF(["id", "x", "y", "date"])
,並寫出如下:
import tempfile
from pyspark.sql.functions import col, dayofmonth, month, year
outdir = tempfile.mktemp()
dt = col("date").cast("date")
fname = [(year, "year"), (month, "month"), (dayofmonth, "day")]
exprs = [col("*")] + [f(dt).alias(name) for f, name in fname]
(df
.select(*exprs)
.write
.partitionBy(*(name for _, name in fname))
.format("json")
.save(outdir))
單個文件不包含分區列:
import os
(sqlContext.read
.json(os.path.join(outdir, "year=2016/month=2/day=16/"))
.printSchema())
## root
## |-- date: string (nullable = true)
## |-- id: long (nullable = true)
## |-- x: string (nullable = true)
## |-- y: double (nullable = true)
分區數據只存儲在目錄結構中,而不是在序列化文件中重複。它只會在您讀取完整或部分目錄樹時附加:
sqlContext.read.json(outdir).printSchema()
## root
## |-- date: string (nullable = true)
## |-- id: long (nullable = true)
## |-- x: string (nullable = true)
## |-- y: double (nullable = true)
## |-- year: integer (nullable = true)
## |-- month: integer (nullable = true)
## |-- day: integer (nullable = true)
sqlContext.read.json(os.path.join(outdir, "year=2016/month=2/")).printSchema()
## root
## |-- date: string (nullable = true)
## |-- id: long (nullable = true)
## |-- x: string (nullable = true)
## |-- y: double (nullable = true)
## |-- day: integer (nullable = true)
我是新來的python。有沒有辦法做到這一點,而沒有年= =,月=,和日=在路徑中?我明白這一點 – deanw