vijay
vijay

Reputation: 21

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

create table b as
select * from table a;

please add check condition to this

Upvotes: 2

Views: 135

Answers (1)

leftjoin
leftjoin

Reputation: 38335

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

Upvotes: 1

Related Questions