Hive Case Statement for Insert Overwrite Directory

2019-07-11 05:32发布

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条回答
We Are One
2楼-- · 2019-07-11 06:28

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.

查看更多
登录 后发表回答