Shell for循环遍历并动态注入参数到hive
·
假设你要执行一些数据,例如根据经销商做统计,但是数组有很多个,你懒得一个个执行,那么应该怎么办?
某某经销商->L0814 L2592 L0819 L4786
shell的for循环基础语法
#常规遍历
for i in 1 2 3 4 5
do
echo "$i-->$(uptime)"
done
#批量解压缩当前文件夹下所有的.tar.gz 文件
for i in `ls ./*.tar.gz`
do
tar -zxvf $i >/dev/null
done
#求1~100 的和
sum=0
for (( i=1; i<=100; i++ ))
do
sum=$(( $sum + $i ))
done
echo "1+2+3+...+100=$sum"
如何循环遍历并动态注入参数?
当然是写成shell脚本去动态遍历,通过shell自动切割string数组(这个也是相当智能,好用的),然后注入动态参数${k}达到遍历执行的效果。注意变量赋值的时候,=两边绝对不能有空格
以下脚本选自公司大数据平台,进行一些删减,请注意动态注入参数${k}的地方
dealListString=(" L0814 L2592 L0819 L4786 ")
for k in $dealListString
do
echo '导入'${k}'开始'
hive << END_HIVE
use xxxxxx;
insert xxxxxxxxxxx.......
where store1.TCBJ_ORGTYPE = 'Store' and storex.X_MEMSTORE_FLG='Y' and store1.BCSN='${k}' or store1.ACSN='${k}' or store1.CCSN='${k}') store on mem4.x_reg_store_id = store.store_id) member
left join (select txn.member_id,txn.row_id,txn.created,txn.source_cd,prod.alias_name from (select member_id,row_id,created,source_cd,prod_id,x_store_id from ori_siebel_s_loy_txn where bu_id ='1-1JK1' and TYPE_CD = '应计' and Status_Cd = '已处理' and sub_status_cd = '成功' ) txn
inner join (select store1.row_id as store_id from CX_AWK_ALLSTORELIST_VIEW store1
inner join ori_siebel_S_ORG_PRTNR storex on store1.ROW_ID = storex.PAR_ROW_ID
inner join ori_siebel_CX_REGION_MAIN district on storex.X_DISTRICT_ID = district.ROW_ID
where store1.TCBJ_ORGTYPE = 'Store' and storex.X_MEMSTORE_FLG='Y' and store1.BCSN='${k}' or store1.ACSN='${k}' or store1.CCSN='${k}') txn_store on txn.x_store_id=txn_store.store_id
inner join ori_siebel_s_loy_acrl_itm itm on itm.TXN_ID = txn.row_id
inner join ori_siebel_S_LOY_ATTRDEFN attr on itm.ATTRIB_DEFN_ID = attr.Row_Id and attr.DISPLAY_NAME = '可用产品积分'
inner join ori_siebel_S_LOY_PTSUBTYPE typ on itm.Pt_Sub_Type_Id = typ.Row_Id and typ.NAME='产品积分'
inner join ori_siebel_S_PROD_INT prod on txn.prod_id = prod.ROW_ID
) txn1 on member.row_id = txn1.member_id) t where t.num = 1;
END_HIVE
#导出数据到CSV
python export_data_into_csv.py tmp_partner_mem_points_times_info /opt/sharedata/yyj xxxx_临时报表_${k}客户数据
echo '导入'${k}'完成'
done更多推荐
所有评论(0)