1. 程式人生 > >Shell for迴圈遍歷並動態注入引數到hive

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