如何使用条件语句执行(或绕过)Hive/SQL 中的代码块?

How to use conditional statements to execute (or bypass) chunks of code in Hive/SQL?

我知道如何在 Hive 中使用条件语句为变量赋值,例如

CASE WHEN expression = condition1 THEN result1
     WHEN expression = condition2 THEN result2
     ...
     ELSE result
END;

现在我想用条件语句来决定是否执行一段代码。当我尝试应用上述内容时,它失败了。例如,

CASE WHEN (expression = condition1 or expression = condition2) 
     THEN
          CREATE TABLE table1;
          CREATE TABLE table2;
          CREATE TABLE table3;
     END;

我收到一条错误消息 FAILED: ParseException line 5:0 cannot recognize input near 'CASE' 'WHEN' '('

是否可以对 execute/bypass 代码块使用条件语句? 谢谢!

您不能在 Hive 中使用 DDL 条件语句 https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL

我建议您创建空表然后进行多次插入,例如

  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'
; 

有关插入的详细信息here, about creating tables here