仅当有数据时,如何从另一个表在 hive 中创建表?

问题描述

create table b as
select * from table a;

添加检查条件

  • 如果表 a 有记录,则应创建表 b
  • 如果表 a 没有记录,则不应创建表 b

解决方法

您可以有条件地使脚本失败

--this will generate HiveException with message ASSERT_TRUE(): assertion failed
--it the table is empty and the script will exit
select assert_true(count(*)>0) from a;

--If previous statement executed successfully
create table b as
select * from table a;

另一种方法是使用 java_method("java.lang.System","exit",1)

select "Checking source is not empty ...";
    
select java_method("java.lang.System",1) --Exit 
from
(
select count(*) cnt 
 from a
)s where cnt=0; --select only if count=0

select "Creating the table b ...";
--Put create table here

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...