使用Hiveql循环

时间:2016-02-25 11:20:29

标签: hive hiveql

我试图合并2个数据集,例如A和B.数据集A有一个变量" Flag"它取2个值。我没有将这两个数据合并在一起,而是试图基于" 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

代码I的循环部分调用基于" Flag"来合并数据。变量如下:

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

我将上面的代码放在另一个" .hql"文件asn调用它:

hive -f loop_data.hql

但它引发了错误。

  

无法识别'附近的输入。 '标志' '在'

任何人都可以告诉我我在哪里弄错了。

谢谢!

1 个答案:

答案 0 :(得分:9)

  1. 您应该将循环逻辑添加到shell脚本。
  2. 文件名: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个单独的脚本中。像
    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
      

      如果您想了解更多信息,请与我们联系。