背景1 : 隨著接入數(shù)據(jù)和處理數(shù)據(jù)的增加,生產(chǎn)腳本也越來越多,腳本由于前期的開發(fā)人員沒有做到規(guī)范管理,導致腳本很亂。 解決方案: 1) 在lunix上規(guī)范目錄,按平臺,業(yè)務模塊分目錄存放。 2) 做好版本管理,提交到生產(chǎn)的腳本必須要commit到svn服務器。 3
背景1 : 隨著接入數(shù)據(jù)和處理數(shù)據(jù)的增加,生產(chǎn)腳本也越來越多,腳本由于前期的開發(fā)人員沒有做到規(guī)范管理,導致腳本很亂。
解決方案:
1) 在lunix上規(guī)范目錄,按平臺,業(yè)務模塊分目錄存放。
2) 做好版本管理,提交到生產(chǎn)的腳本必須要commit到svn服務器。
3) lunix上的目錄是反應到svn的目錄映射。
背景2 :腳本中很多地方有范圍,指標,參數(shù)值,怎么把這些做的更靈活,而不是寫死?
解決方案:
1)盡量把中文或英文映射為數(shù)字,不僅節(jié)省存儲資源,還使程序更靈活。
比如:平臺有 pc電腦,手機等,那就可以分別用1代表pc電腦 2 代表手機 3 代表其它。同時做一個碼表來解釋對應的關(guān)系。
主頁 0 a
電臺 1 b
語種 2 c
華語 3 d
多級目錄的解析
/主頁/電臺/語種/華語
/0/1/2/3
/1/2/3
/xxx/xxx/xxx/xxx
如果多級目錄有變化,怎么自動適應目錄變化或者 回歸二進制本質(zhì),用二進制表示。
2) 靈活應用參數(shù)列表,做一個的參數(shù)碼表,動態(tài)生成hql語句。
比如:現(xiàn)在要分時段統(tǒng)計,用戶出現(xiàn)次數(shù),時段如下:
早上 6:00 -8:00
上午 8:00-12:00
中午 12:00-14:00
下午 14:00-18:00
晚上 18:00-23:00
深夜 23:00-00:00
凌晨 00:00-6:00
做一個碼表 id comment time_region val par1 par2
1 早上 6:00 -8:00 1 6 8
2 上午 8:00-12:00 2 8 12
3 中午 12:00-14:00 3 12 14
4 下午 14:00-18:00 4 14 18
5 晚上 18:00-23:00 5 18 23
6 深夜 23:00-24:00 6 23 24
7 凌晨 00:00-6:00 7 0 6
讀取該表,動態(tài)拼hql ,用后面的val分別代表這些時段。不管以后時段怎么修改,只要修改碼表就行了。
比如要寫如下的hive_sql
insert overwrite table common.order select userid ,case when hour_time>=0 and hour_time<=2 then '00_03' when hour_time>=3 and hour_time<=5 then '03_06' when hour_time>=6 and hour_time<=7 then '06_08' when hour_time>=8 and hour_time<=11 then '08_12' when hour_time>=12 and hour_time<=13 then '12_14' when hour_time>=14 and hour_time<=17 then '14_18' when hour_time>=18 and hour_time<=23 then '18_24' else '0' end as hour_time ,count(distinct dt) hour_dt_num where dt >='2014-10-01' and dt<='2014-10-30' group by userid, case when hour_time>=0 and hour_time<=2 then '00_03' when hour_time>=3 and hour_time<=5 then '03_06' when hour_time>=6 and hour_time<=7 then '06_08' when hour_time>=8 and hour_time<=11 then '08_12' when hour_time>=12 and hour_time<=13 then '12_14' when hour_time>=14 and hour_time<=17 then '14_18' when hour_time>=18 and hour_time<=23 then '18_24' else '0' end
可以寫成這樣子:
#!/bin/bash # # add by lishc 2014-11-25 mysql=`which mysql` user="root" password="123" database="test" table="parm" command="select par1,par2,id from test.$table " $mysql -u${user} -p${password} -e "${command}" >m.txt ###初始化 echo " insert overwrite table common.order">mysql.sql echo " select ">>mysql.sql echo " userId " >>mysql.sql echo " case ">>mysql.sql sed -i -e '1d' m.txt cat m.txt |while read line do par1=$(echo "${line}"|awk -F ' ' '{print $1}') par2=$(echo "${line}"|awk -F ' ' '{print $2}') id=$(echo "${line}"|awk -F ' ' '{print $3}') echo "par1: ${par1}" echo "par2: ${par2}" echo " when hour_time >=${par1} and hour_time<=${par2} then '${id}' ">>mysql.sql Done
3) 所有的腳本存放在數(shù)據(jù)庫中,用程序解析參數(shù),并調(diào)用執(zhí)行。
可參考kettle設(shè)計:
每個步驟組件化:輸入,輸出,執(zhí)行腳本,執(zhí)行sql,管理執(zhí)行順序。
由于ETL過程或數(shù)據(jù)分析模型,都是一些有序的sql或腳本操作。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com