使用 Hiveql 循环

2024-05-14

我正在尝试合并 2 个数据集,例如 A 和 B。数据集 A 有一个变量“Flag”,它有 2 个值。我并没有只是将两个数据合并在一起,而是尝试根据“标志”变量合并两个数据集。

合并代码如下:

create table new_data as
select a.*,b.y
from A as a left join B as b
on a.x=b.x

由于我通过 CLI 运行 Hive 代码,因此我通过以下命令调用它

hive -f new_data.hql

我调用的基于“Flag”变量合并数据的代码的循环部分如下:

for flag in 1 2;
do
  hive -hivevar flag=$flag -f new_data.hql
done

我将上面的代码放在另一个“.hql”文件中,调用它:

hive -f loop_data.hql

但它抛出错误。

无法识别“for”“flag”“in”附近的输入

谁能告诉我我哪里出错了。

Thanks!


  1. 您应该将循环逻辑添加到 shell 脚本中。

文件名:loop_data.sh

for flag in 1 2;
do
  hive -hivevar flag=$flag -f new_data.hql
done

并执行如下脚本:

sh loop_data.sh
  1. 在您的 new_data.hql 脚本中,您正在创建表。因为您应该将 DDL 和 DML 拆分为 2 个单独的脚本。喜欢

DDL:create_new_data.hql

create table new_data as
select 
  a.*,
  b.y
from 
  A as a left join 
  B as b on 
  a.x = b.x
where 
  1 = 0;

DML:insert_new_data.hql

insert into new_data 
select 
  a.*,
  b.y
from 
  A as a left join 
  B as b on 
  a.x = b.x
where
  flag = ${hiveconf:flag}

并更新你的 shell 脚本,如下所示:

文件名:loop_new_data.sh

# Create table
hive -f create_new_data.hql

# Insert data
for flag in 1 2;
do
  hive -hiveconf flag=$flag -f insert_new_data.hql
done

并像这样执行它:

sh loop_new_data.sh

如果您想了解更多信息,请告诉我。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Hiveql 循环 的相关文章

随机推荐