是否可以有条件地使 Hive 脚本失败



我想有条件地使 hive 脚本失败。就像如果某个表中没有数据一样,脚本应该失败,否则继续。我知道这可能不是理想的解决方案,但出于某种原因,这是我的要求。由于 HQL 不是程序语言,因此这是一个挑战。

assert_true

hive> select assert_true (2>1);
OK
NULL
Time taken: 2.61 seconds, Fetched: 1 row(s)

hive> select assert_true (2<1);
OK
Failed with exception java.io.IOException:org.apache.hadoop.hive.ql.metadata.HiveException: ASSERT_TRUE(): assertion failed.
Time taken: 1.063 seconds
hive> 

演示

我的脚本.sql

set hive.cli.errors.ignore=false; -- this is the default
select 'checkpoint 1';
drop table t1;
create table t1 as select 1;
select assert_true(count(*)>0) from t1;
select 'checkpoint 2';
drop table t2;
create table t2 as select 2 where false;
select assert_true(count(*)>0) from t2;
select 'checkpoint 3';
drop table t3;
create table t3 as select 3;
select assert_true(count(*)>0) from t3;
select 'checkpoint 4';

bash-4.1$ hive -f myscript.sql 2>/dev/null
checkpoint 1
NULL
checkpoint 2
bash-4.1$ 

最新更新