How to use conditional statements to execute (or bypass) chunks of code in Hive/SQL?
How to use conditional statements to execute (or bypass) chunks of code in Hive/SQL?
I know how to use conditional statements in Hive to assign values to variables, for example
CASE WHEN expression = condition1 THEN result1
WHEN expression = condition2 THEN result2
...
ELSE result
END;
Now I would like to use conditional statements to decide whether to execute a chunk of code. When I try to apply the above, it fails. For example,
CASE WHEN (expression = condition1 or expression = condition2)
THEN
CREATE TABLE table1;
CREATE TABLE table2;
CREATE TABLE table3;
END;
I get an error message FAILED: ParseException line 5:0 cannot recognize input near 'CASE' 'WHEN' '('
.
FAILED: ParseException line 5:0 cannot recognize input near 'CASE' 'WHEN' '('
Is it possible to use conditional statements to execute/bypass chunks of code?
Thanks!
from init_table insert into first_table select ... insert into second_table select ...
Hi @serge_k, thanks for the idea. I haven't been able to find any doc on this. Could you share a link detailing how to do this please?
– A. Debugne
Aug 21 at 17:27
Do you need to just create tables or insert data when conditions are met as well?
– serge_k
Aug 22 at 7:32
I specify a schema and a location when creating the tables so that they're filled with data at creation. Does that answer your question?
– A. Debugne
Aug 22 at 8:03
2 Answers
2
You cannot use conditional statements in Hive with DDL https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL
Thanks for the information @Harold. And I guess you don't know of a work-around?
– A. Debugne
Aug 22 at 13:26
I suggest you to create empty tables then make multiple inserts, e.g.,
from source_table
insert overwrite
table first_table
select column1, column2
where column1 = 'something'
insert overwrite
table second_table
select column1, column2
where column1 = 'something_else'
;
More info about inserts here, about creating tables here
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Hive has multiple inserts functionality, e.g.
from init_table insert into first_table select ... insert into second_table select ...
, where you can use your conditions.– serge_k
Aug 21 at 11:34