如何在配置单元中从另一个表创建一个表,如果它有数据?

dced5bon  于 2021-07-13  发布在  Hive
关注(0)|答案(1)|浏览(495)
  1. create table b as
  2. select * from table a;

请在此基础上添加检查条件
如果表a有记录,那么应该创建表b
如果表a没有记录,则不应创建表b

xdnvmnnf

xdnvmnnf1#

可以有条件地使脚本失败

  1. --this will generate HiveException with message ASSERT_TRUE(): assertion failed
  2. --it the table is empty and the script will exit
  3. select assert_true(count(*)>0) from a;
  4. --If previous statement executed successfully
  5. create table b as
  6. select * from table a;

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

  1. select "Checking source is not empty ...";
  2. select java_method("java.lang.System", "exit", 1) --Exit
  3. from
  4. (
  5. select count(*) cnt
  6. from a
  7. )s where cnt=0; --select only if count=0
  8. select "Creating the table b ...";
  9. --Put create table here
展开查看全部

相关问题