gpt4 book ai didi

java - 无法在 Spark 中配置 ORC 属性

转载 作者:可可西里 更新时间:2023-11-01 14:25:31 29 4
gpt4 key购买 nike

我正在使用 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 之前定义对象...

  • 要么在hive-site.xml在启动时可供 Spark 使用
  • 或在您的代码中,通过重新创建 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 将自己的 SerDe 库用于 ORC(以及 Parquet、JSON、CSV 等),因此它不必遵守标准的 Hadoop/Hive 属性。

Parquet 有一些 Spark 特定的属性,它们是 well documented .但同样,必须在创建(或重新创建)hiveContext 之前设置这些属性。 .

对于 ORC 和其他格式,您必须求助于格式特定的 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")

关于java - 无法在 Spark 中配置 ORC 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41756775/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com