实战:hive orc格式纯数字使用string类型导致weher条件加该字段报错

260 阅读3分钟

hive orc格式纯数字使用string类型导致weher条件加该字段报错

在Hive中使用ORC格式存储数据时,如果纯数字字段定义为String类型,当使用该字段在WHERE条件中进行查询时,可能会导致报错。

报错 Error: java.lang.AssertionError: Index is not populated for 22 (state=,code=0)

Error: java.lang.AssertionError: Index is not populated for 22 (state=,code=0)

26605d5c7d8d12b97af9a4f8f4fde51.png

分析

show create table tablename

f57783755691a427e895e01b754bb2a.png 发现表格式是orc,orc是强数据验证,就是出数字的数据有认为是数据类型,但是原表数据类型是string,数据是使用datax抽取过来的。fee_clr_id是string类型,但是数据是纯数字。

验证

-- OK 
select * from prd_btyb_db.setlcent_db_clr_proc_fund_d where fee_clr_id ='20230114310309735' and stat_date='20240124' LIMIT 10 ; 

-- Error: java.lang.AssertionError: Index is not populated for 22 (state=,code=0)
select fee_clr_id, clr_appy_evt_id, stat_date from prd_btyb_db.setlcent_db_clr_proc_fund_d where fee_clr_id ='20230114310309735' and stat_date='20240124' LIMIT 10 ; 

-- 结果为空
select * from prd_btyb_db.setlcent_db_clr_proc_fund_d where clr_appy_evt_id ='20230114310309275' and stat_date='20240124' LIMIT 10 ;

-- OK
select fee_clr_id, clr_appy_evt_id, stat_date from prd_btyb_db.setlcent_db_clr_proc_fund_d where  stat_date='20240124' LIMIT 10 ; 

-- Error: FAILED: SemanticException Cartesian products are disabled for safety reasons. If you know what you are doing, please sethive.strict.checks.cartesian.product to false and that hive.mapred.mode is not set to 'strict' to proceed. Note that if you may get errors or incorrect results if you make a mistake while using some of the unsafe features.
select fee_clr_id, clr_appy_evt_id, stat_date from prd_btyb_db.setlcent_db_clr_proc_fund_d where fee_clr_id =20230114310309735 and stat_date='20240124' LIMIT 10 ; 

解决1(不推荐)

设置 hive.strict.checks.cartesian.product hive.mapred.mode

set hive.strict.checks.cartesian.product=false
set hive.mapred.mode=nonstrict

执行

--OK
select fee_clr_id, clr_appy_evt_id, stat_date from prd_btyb_db.setlcent_db_clr_proc_fund_d where fee_clr_id =20230114310309735 and stat_date='20240124' LIMIT 10 ; 

但是这种方法,需要修改sql查询语句where条件的除了分区字段所有纯数字字段需当成数字处理,不能有单引号‘’或双引号“”

不推荐原因

sql处理是另一个部门来做,我们没办法要求人家改sql,希望他们能无感知。

解决2

按照规范接口层数据表全部使用textfile,将表换成textfile格式,使用datax重新抽取

-- OK
select fee_clr_id, clr_appy_evt_id, stat_date from prd_btyb_db.setlcent_db_clr_proc_fund_d where fee_clr_id ='20230114310309735' and stat_date='20240124' LIMIT 10 ; 

解决3(不推荐)

修改纯数字的字段类型改为数字类型,int/bigint等

不推荐原因

1.不好确定那些字段是纯数字 2.改变了源库的数据类型,不符合规范 3.要改sql,where条件的除了分区字段所有纯数字字段需当成数字处理,不能有单引号‘’或双引号“”