How to create a table in hive from another table only if it has data?

Viewed 92
create table b as
select * from table a;

please add check condition to this

  • if table a has records then table b should get created
  • if table a has no records then table b should not be created
1 Answers

You can conditionally fail the script

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

One more method is using 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
Related