orc文件上的spark sql不返回正确的架构(列名)

nszi6y05  于 2021-06-28  发布在  Hive
关注(0)|答案(5)|浏览(457)

我有一个包含orc文件的目录。我正在使用下面的代码创建一个Dataframe

var data = sqlContext.sql("SELECT * FROM orc.`/directory/containing/orc/files`");

它返回具有此模式的Dataframe

[_col0: int, _col1: bigint]

其中预期的模式是

[scan_nbr: int, visit_nbr: bigint]

当我查询Parquet格式的文件时,我得到了正确的模式。
是否缺少任何配置?
添加更多详细信息
这是hortonworks发行版HDP2.4.2(spark 1.6.1、hadoop 2.7.1、hive 1.2.1)
我们没有改变hdp的默认配置,但是这绝对不是hadoop的普通版本。
数据是由上游配置单元作业编写的,这是一个简单的cta(create table sample stored as orc as select…)。
我用最新的2.0.0配置单元在ctas生成的文件中测试了这个功能&它保留了orc文件中的列名。

pokxtpni

pokxtpni1#

问题是hive版本是1.2.1,它有一个错误hive-4243
这是在2.0.0中修复的。

o8x7eapl

o8x7eapl2#

我们可以使用: val df = hiveContext.read.table("tableName") 你的 df.schema 或者 df.columns 将给出实际的列名。

s4n0splo

s4n0splo3#

如果你也有Parquet地板版本,你可以复制列名,这就是我做的(同样,日期列是orc的分区键,所以必须将它移到末尾):

tx = sqlContext.table("tx_parquet")
df = sqlContext.table("tx_orc")
tx_cols = tx.schema.names
tx_cols.remove('started_at_date')
tx_cols.append('started_at_date') #move it to end

# fix column names for orc

oldColumns = df.schema.names
newColumns = tx_cols
df = functools.reduce(
    lambda df, idx: df.withColumnRenamed(
        oldColumns[idx], newColumns[idx]), range(
            len(oldColumns)), df)
0aydgbwb

0aydgbwb4#

若版本升级不是一个可用的选项,那个么快速修复方法就是使用pig重写orc文件。这似乎很管用。

kulphzqa

kulphzqa5#

设置

sqlContext.setConf('spark.sql.hive.convertMetastoreOrc', 'false')

解决了这个问题。

相关问题