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

Dhi*_*raj 2 hive

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

Dav*_*itz 5

断言真

hive> select assert_true (2>1);
OK
NULL
Time taken: 2.61 seconds, Fetched: 1 row(s)
Run Code Online (Sandbox Code Playgroud)
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> 
Run Code Online (Sandbox Code Playgroud)

演示

脚本文件

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';
Run Code Online (Sandbox Code Playgroud)
bash-4.1$ hive -f myscript.sql 2>/dev/null
checkpoint 1
NULL
checkpoint 2
bash-4.1$ 
Run Code Online (Sandbox Code Playgroud)