【问题标题】:How to create a table in hive from another table only if it has data?只有当它有数据时,如何从另一个表中创建一个表?
【发布时间】:2021-07-08 21:15:31
【问题描述】:
create table b as
select * from table a;

请添加检查条件

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

【问题讨论】:

  • 您是否有其他代码可以显示您到目前为止所尝试的内容?

标签: hive hiveql create-table hiveddl


【解决方案1】:

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

--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", "exit", 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

【讨论】:

    猜你喜欢
    • 2021-12-23
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多