无法在 Spark 中配置 ORC 属性

Unable to configure ORC properties in Spark

我正在使用 Spark 1.6 (Cloudera 5.8.2) 并尝试了以下方法来配置 ORC 属性。但是不影响输出。

下面是我试过的代码片段。

 DataFrame dataframe =
                hiveContext.createDataFrame(rowData, schema);
dataframe.write().format("orc").options(new HashMap(){
            {

                put("orc.compress","SNAPPY");
                put("hive.exec.orc.default.compress","SNAPPY");

                put("orc.compress.size","524288");
                put("hive.exec.orc.default.buffer.size","524288");


                put("hive.exec.orc.compression.strategy", "COMPRESSION");

            }
        }).save("spark_orc_output");

除此之外,我还尝试了在 hive-site.xml 和 hiveContext 对象中设置的这些属性。

hive --orcfiledump 输出确认未应用配置。 Orcfiledump 片段如下。

Compression: ZLIB
Compression size: 262144

您在这里犯了两个不同的错误。我不怪你;我去过那里...

第 1 期
orc.compress 和其余的不是 Spark DataFrameWriter 选项。它们是 Hive 配置属性,必须在 创建 hiveContext 对象之前定义...

  • 在启动时可供 Spark 使用的hive-site.xml
  • 或在您的代码中,通过 re-creating SparkContext...

sc.getConf.get("orc.compress","<undefined>") // depends on Hadoop conf
sc.stop
val scAlt = new org.apache.spark.SparkContext((new org.apache.spark.SparkConf).set("orc.compress","snappy"))
scAlt.getConf.get("orc.compress","<undefined>") // will now be Snappy
val hiveContextAlt = new org.apache.spark.sql.SQLContext(scAlt)

[编辑] 使用 Spark 2.x 脚本将变为...
spark.sparkContext.getConf.get("orc.compress","<undefined>") // depends on Hadoop conf
spark.close
val sparkAlt = org.apache.spark.sql.SparkSession.builder().config("orc.compress","snappy").getOrCreate()
sparkAlt.sparkContext.getConf.get("orc.compress","<undefined>") // will now be Snappy

第 2 期
Spark 为 ORC(和 Parquet、JSON、CSV 等)使用自己的 SerDe 库,因此它不必遵守标准 Hadoop/Hive 属性。

Parquet 有一些 Spark-specific 个属性,它们是 well documented。但同样,这些属性必须在 创建(或 re-creating)hiveContext.

之前设置

对于 ORC 和其他格式,您必须求助于 format-specific DataFrameWriter 选项;引用最新 JavaDoc...

You can set the following ORC-specific option(s) for writing ORC files:
compression (default snappy): compression codec to use when saving to file. This can be one of the known case-insensitive shorten names (none, snappy, zlib, and lzo). This will override orc.compress

请注意,默认压缩编解码器已随 Spark 2 发生变化;在此之前是 zlib

所以你唯一可以设置的就是压缩编解码器,使用

dataframe.write().format("orc").option("compression","snappy").save("wtf")