插入覆盖目录的Hive Case语句

时间:2016-06-02 15:03:43

标签: hadoop parameters hive case hiveql

尝试使用以下逻辑运行HQL脚本时,收到错误:

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

脚本逻辑

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

如何根据我传入的tbl_name参数的值为INSERT OVERWRITE DIRECTORY正确分配select语句?

更新1: 修改为:

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

错误:

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

更新2:修改为:

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

错误:

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

1 个答案:

答案 0 :(得分:0)

快速解决方案是使用UNION ALL + WHERE。像这样:

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

将连接添加到每个选择中。