Hive Case Statement for Insert Overwrite Directory

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 18:17:35

问题


When attempting to run an HQL script with the following logic, I receive the error:

ParseException line 4:0 cannot recognize input near 'CASE' 'WHEN' 'mytable' in serde properties specification 

Script Logic

INSERT OVERWRITE DIRECTORY '/example/path'
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
CASE WHEN ${hiveconf:tbl_name}='mytable'
THEN SELECT * FROM ${hiveconf:tbl_name} LEFT OUTER JOIN ...;
WHEN ${hiveconf:tbl_name}='mytable2'
THEN SELECT * FROM ${hiveconf:tbl_name} LEFT OUTER JOIN ...;
ELSE THEN
END

How can I correctly assign a select statement for the INSERT OVERWRITE DIRECTORY to use based on the value of the tbl_name parameter which I am passing in?

Update 1: When modifying as:

SELECT CASE WHEN ${hiveconf:tbl_name}='mytable'
THEN * FROM WHEN ${hiveconf:tbl_name}='mytable'
WHEN ${hiveconf:tbl_name}='mytable2'
THEN * FROM ${hiveconf:tbl_name} LEFT OUTER JOIN ...;
ELSE THEN
END

It errors with:

FAILED: ParseException line 9:9 cannot recognize input near '*' 'FROM' 'mytable' in expression specification

Update 2: When modifying as:

SELECT * CASE WHEN ${hiveconf:tbl_name}='mytable'
THEN FROM WHEN ${hiveconf:tbl_name}='mytable'
WHEN ${hiveconf:tbl_name}='mytable2'
THEN FROM ${hiveconf:tbl_name} LEFT OUTER JOIN ...;
ELSE THEN
END

It errors with:

FAILED: ParseException line 8:9 Failed to recognize predicate 'CASE'. Failed rule: 'regularBody' in statement

回答1:


Quick solution is to use UNION ALL+WHERE. Like this:

insert overwrite directory
...
select * from mytable where '${hiveconf:tbl_name}'='mytable'
union all
select * from mytable2 where '${hiveconf:tbl_name}'='mytable2'

Add your joins to each select.



来源:https://stackoverflow.com/questions/37595532/hive-case-statement-for-insert-overwrite-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!