【问题标题】:Way to create external hive table from ORC File从 ORC 文件创建外部配置单元表的方法
【发布时间】:2020-08-14 05:28:31
【问题描述】:

我正在尝试在 ORC 文件上创建外部 Hive 表。

用于创建表的查询:

create external table fact_scanv_dly_stg (
store_nbr int,
geo_region_cd char(2),
scan_id int,
scan_type char(2),
debt_nbr string,
mds_fam_id string,
upc_nbr string,
sales_unit_qty string,
sales_amt string,
cost_amt string,
visit_dt string,
rtn_amt string,
crncy_cd string,
op_cmpny_cd string)
STORED AS ORC
location 'hdfs:///my/location/scanv_data/'; 

ORC 文件的架构细节(取自 DataFrame Spark-SQL):

 |-- _col0: integer (nullable = true)
 |-- _col1: string (nullable = true)
 |-- _col2: integer (nullable = true)
 |-- _col3: byte (nullable = true)
 |-- _col4: short (nullable = true)
 |-- _col5: integer (nullable = true)
 |-- _col6: decimal(18,0) (nullable = true)
 |-- _col7: decimal(9,2) (nullable = true)
 |-- _col8: decimal(9,2) (nullable = true)
 |-- _col9: decimal(9,2) (nullable = true)
 |-- _col10: date (nullable = true)
 |-- _col11: decimal(9,2) (nullable = true)
 |-- _col12: string (nullable = true)
 |-- _col13: string (nullable = true)

但是当我尝试在创建的表上进行选择时出现以下错误:

select * from fact_scanv_dly_stg limit 5;


OK
Failed with exception java.io.IOException:org.apache.hadoop.hive.ql.metadata.HiveException: java.lang.ClassCastException: org.apache.hadoop.hive.serde2.io.ByteWritable cannot 
be cast to org.apache.hadoop.hive.serde2.io.HiveCharWritable

有什么建议吗??

【问题讨论】:

  • char(2) 不应该是varchar(2) 吗?此外,有些事情没有加起来。您创建的表没有任何十进制字段,但架构显示为十进制。这些是同一张桌子吗?
  • 位置的数据如何:'hdfs:///my/location/scanv_data/'?它有什么格式?能不能: hdfs dfs -ls hdfs:///my/location/scanv_data/ ,看看文件是什么格式的?

标签: apache-spark hive orc hive-serde


【解决方案1】:

发生此错误是因为在将数据从 spark 写入 ORC 文件时,列的数据类型也被保留,因此在创建 hive 表时,您需要将每个列的数据类型映射到与数据帧模式相同的数据类型。

在这里,由于您在 spark 模式中的列 _col3 是字节而发生此错误,而您已将其指定为 hive 中的字符串。解决方案是将所有列转换为 spark 中所需的数据类型,然后写入 ORC 文件或将 hive 模式中的确切数据类型映射为 spark。

【讨论】:

  • 感谢您的更新,我已将 byte 数据类型更改为 tinyint 和其他列,如 orc 文件的数据类型中给出的那样,它可以正常工作
  • 请查看问题的答案,如果它解决了您的问题,请标记为答案。 What should I do when someone answers my question?
猜你喜欢
  • 2017-01-20
  • 2016-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-10
  • 2018-10-26
  • 1970-01-01
  • 2021-12-02
相关资源
最近更新 更多