[英]Using partitionBy on a DataFrameWriter writes directory layout with column names not just values
[英]apache-spark partitionBy: remove column names from directory layout
我有这样的代码:
val data1 = data.withColumn("local_date_time", toLocalDateUdf('timestamp))
data1
.withColumn("year", year(col("local_date_time")))
.withColumn("month", month(col("local_date_time")))
.withColumn("day", dayofmonth(col("local_date_time")))
.withColumn("hour", hour(col("local_date_time")))
.drop("local_date_time")
.write
.mode("append")
.partitionBy("year", "month", "day", "hour")
.format("json")
.save("s3a://path/")
它创建嵌套文件夹,例如 this year=2020
/ month=5
/ day=10
是 S3( year
是列名, 2020
是它的值)。 我想创建像2020
/ 5
/ 10
这样的嵌套文件夹。 如果我使用partitionBy
方法,Spark 会将列名添加到目录名。
这是来自 Spark 源代码:
/**
* Partitions the output by the given columns on the file system. If specified, the output is
* laid out on the file system similar to Hive's partitioning scheme. As an example, when we
* partition a dataset by year and then month, the directory layout would look like:
* <ul>
* <li>year=2016/month=01/</li>
* <li>year=2016/month=02/</li>
* </ul>
*/
@scala.annotation.varargs
def partitionBy(colNames: String*): DataFrameWriter[T] = {
this.partitioningColumns = Option(colNames)
this
}
如何从目录布局中删除列名?
.partitionBy("年"、"月"、"日"、"小时")
上面的命令允许您将其保存到带有partition=value
格式的分区的 parquet 中
这不是错误,它是标准的镶木地板格式。
您可以遍历每个分区并手动保存它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.