shell中迴圈呼叫hive sql 指令碼
阿新 • • 發佈:2018-12-22
指令碼tt.sh的內容如下:
#!/bin/bash
params=$1for param in $params
do
echo $param
done
執行方式為:sh tt.sh "1 2 3 4 5"
輸出為:
1
2
3
4
5
----------------------------------------------------------------------------------------------------------------------------------------------------------
所以參考上面的命令,可以把hql的指令碼寫為如下方式,就可以迴圈執行sql:功能:查詢字串 comments 中的param第一次出現的位置 ,返回的是位置數字
#!/bin/bash
params=$1for param in $params
do
hive -e "insert overwrite local directory '/tmp/$param'
row format delimited fields terminated by '\t'
select locate('$param',comments) as position from tb_a;"
done
----------------------------------------------------------------------------------------------------------------------------------------------------------
params=$1
for param in $params
do
hive -e "insert overwrite local directory '/tmp/$param'
row format delimited fields terminated by '\t'
select position from
(select locate('$param',comments) as position from tb_a where position != '0') a
where a.position !='0' ;"
done