【问题标题】:Using partitionBy on a DataFrameWriter writes directory layout with column names not just values在 DataFrameWriter 上使用 partitionBy 会使用列名写入目录布局,而不仅仅是值
【发布时间】:2016-11-15 23:52:47
【问题描述】:

我使用的是 Spark 2.0。

我有一个数据框。我的代码如下所示:

df.write.partitionBy("year", "month", "day").format("csv").option("header", "true").save(s"s3://bucket/")

并且当程序执行时,它会按照以下格式写入文件:

s3://bucket/year=2016/month=11/day=15/file.csv

如何将格式配置为如下:

s3://bucket/2016/11/15/file.csv

我也想知道是否可以配置文件名。

这里是相关文档,看起来很稀疏...
http://spark.apache.org/docs/latest/api/scala/index.html#org.apache.spark.sql.DataFrameWriter

partitionBy(colNames: String*): DataFrameWriter[T]
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:

year=2016/month=01/
year=2016/month=02/
Partitioning is one of the most widely used techniques to optimize physical data layout. It provides a coarse-grained index for skipping unnecessary data reads when queries have predicates on the partitioned columns. In order for partitioning to work well, the number of distinct values in each column should typically be less than tens of thousands.

This was initially applicable for Parquet but in 1.5+ covers JSON, text, ORC and avro as well.

【问题讨论】:

  • 你有什么解决办法吗?其他然后重命名..
  • 很遗憾没有;我告诉我们的企业主这是一个技术限制,我们继续使用以“年=”和“月=”命名的文件结构。

标签: scala apache-spark configuration spark-dataframe


【解决方案1】:

这是预期和期望的行为。 Spark 使用目录结构进行分区发现和修剪,正确的结构,包括列名,对于它的工作是必要的。

您还必须记住,分区会删除用于分区的列。

如果您需要特定的目录结构,您应该使用下游进程重命名目录。

【讨论】:

  • 这只会让下游处理变得很困难,因为 s3 的 Boto lib 讨厌存储桶名称中的“=”字符 :(
【解决方案2】:

您可以使用以下脚本重新布局目录的名称:

#!/usr/bin/env bash

# Rename repartition folder: delete COLUMN=, e.g. DATE=20170708 to 20170708.

path=$1
col=$2
for f in `hdfs dfs -ls $ | awk '{print $NF}' | grep $col=`; do
    a="$(echo $f | sed s/$col=//)"
    hdfs dfs -mv "$f" "$a"
done

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 2018-02-22
    相关资源
    最近更新 更多