一、首先理解下面幾個函數
設定變數 length()函數 char_length() replace() 函數 max() 函數
1.1、設定變數 set @變數名=值
set @address='中國-山東省-聊城市-莘縣';select @address
1.2 、length()函數 char_length()函數區別
select length('a'),char_length('a'),length('中'),char_length('中')
1.3、 replace() 函數 和length()函數組合
set @address='中國-山東省-聊城市-莘縣';select @address,replace(@address,'-','') as address_1,length(@address) as len_add1,length(replace(@address,'-','')) as len_add2,length(@address)-length(replace(@address,'-','')) as _count
etl清洗欄位時候有明顯分割符的如何確定新的資料表增加幾個分割出的欄位
計算出com_industry中最多有幾個 - 符 以便確定增加幾個欄位 最大值+1 為可以拆分成的欄位數 此表為3 因此可以拆分出4個行業欄位 也就是4個行業等級
select max(length(com_industry)-length(replace(com_industry,'-',''))) as _max_countfrom etl1_socom_data
1.4、設定變數 substring_index()字串截取函數用法
set @address='中國-山東省-聊城市-莘縣';select substring_index(@address,'-',1) as china,substring_index(substring_index(@address,'-',2),'-',-1) as province,substring_index(substring_index(@address,'-',3),'-',-1) as city,substring_index(@address,'-',-1) as district
1.5、條件判斷函數 case when
case when then when then else 值 end as 欄位名
select case when 89>101 then '大於' else '小於' end as betl1_socom_data
二、kettle轉換etl1清洗
首先建表 步驟在視頻裡
欄位索引 沒有提 索引演算法建議用BTREE演算法增強查詢效率
2.1.kettle檔案名稱:trans_etl1_socom_data
2.2.包括控制項:表輸入>>>表輸出
2.3.資料流方向:s_socom_data>>>>etl1_socom_data
kettle轉換1截圖
2.4、表輸入2.4、SQL指令碼 初步清洗com_district和com_industry欄位
select a.*,case when com_district like '%業' or com_district like '%織' or com_district like '%育' then null else com_district end as com_district1,case when com_district like '%業' or com_district like '%織' or com_district like '%育' then concat(com_district,'-',com_industry) else com_industry end as com_industry_total,replace(com_addr,'地 址:','') as com_addr1,replace(com_phone,'電 話:','') as com_phone1,replace(com_fax,'傳 真:','') as com_fax1,replace(com_mobile,'手機:','') as com_mobile1,replace(com_url,'網址:','') as com_url1,replace(com_email,'郵箱:','') as com_email1,replace(com_contactor,'連絡人:','') as com_contactor1,replace(com_emploies_nums,'公司人數:','') as com_emploies_nums1,replace(com_reg_capital,'註冊資金:萬','') as com_reg_capital1,replace(com_type,'經濟類型:','') as com_type1,replace(com_product,'公司產品:','') as com_product1,replace(com_desc,'公司簡介:','') as com_desc1from s_socom_data as a
2.5、表輸出
表輸出設定注意事項
注意事項:
① 涉及爬蟲增量操作 不要勾選裁剪表選項
②資料連線問題 選擇表輸出中表所在的資料庫
③欄位對應問題 確保資料流中的欄位和物理表的欄位數量一致 對應一致 三、kettle轉換etl2清洗
首先建表增加了4個欄位 示範步驟在視頻裡
欄位索引 沒有提 索引演算法建議用BTREE演算法增強查詢效率
主要針對etl1 產生的新的com_industry進列欄位拆分 清洗
3.1.kettle檔案名稱:trans_etl2_socom_data
3.2.包括控制項:表輸入>>>表輸出
3.3.資料流方向:etl1_socom_data>>>>etl2_socom_data
注意事項:
① 涉及爬蟲增量操作 不要勾選裁剪表選項
②資料連線問題 選擇表輸出中表所在的資料庫
③欄位對應問題 確保資料流中的欄位和物理表的欄位數量一致 對應一致
kettle轉換2截圖
3.4、SQL指令碼 對com_industry進行拆分 完成所有欄位清洗 註冊資金欄位時間關係沒有進行細緻拆解 調整代碼即可
select a.*,case #行業為''的值 置為空白when length(com_industry)=0 then null#其他的取第一個-分隔字元之前else substring_index(com_industry,'-',1) end as com_industry1,case when length(com_industry)-length(replace(com_industry,'-',''))=0 then null#'交通運輸、倉儲和郵政業-' 這種值 行業2 也置為nullwhen length(com_industry)-length(replace(com_industry,'-',''))=1 and length(substring_index(com_industry,'-',-1))=0 then nullwhen length(com_industry)-length(replace(com_industry,'-',''))=1 then substring_index(com_industry,'-',-1)else substring_index(substring_index(com_industry,'-',2),'-',-1)end as com_industry2,case when length(com_industry)-length(replace(com_industry,'-',''))<=1 then nullwhen length(com_industry)-length(replace(com_industry,'-',''))=2 then substring_index(com_industry,'-',-1)else substring_index(substring_index(com_industry,'-',3),'-',-1)end as com_industry3,case when length(com_industry)-length(replace(com_industry,'-',''))<=2 then nullelse substring_index(com_industry,'-',-1)end as com_industry4from etl1_socom_data as a
四、清洗效果品質檢查
4.1爬蟲資料來源資料和網站資料是否相符
如果本身工作是爬蟲和資料處理在一起處理,抓取的時候其實已經判斷,此步驟可以省略,如果對接上遊爬蟲同事,這一步首先判斷,不然清洗也是無用功,一般都要求爬蟲同事儲存請求的url便於後面資料處理查看資料品質 4.2計算爬蟲資料來源和各etl清洗資料表資料量
註:SQL指令碼中沒有經過彙總過濾 3個表資料量應相等
4.2.1、sql查詢 下面表我是在同一資料庫中 如果不在同一資料庫 from 後面應加上表所在的資料庫名稱
不推薦資料量大的時候使用
select count(1) from s_socom_dataunion allselect count(1) from etl1_socom_dataunion allselect count(1) from etl2_socom_data
4.2.2 根據 kettle轉換執行完畢以後 表輸出總量對比
kettle表輸出總資料量 4.3查看etl清洗品質
確保前兩個步驟已經無誤,資料處理負責的etl清洗工作自查開始 針對資料來源清洗的欄位 寫指令碼檢查 socom網站主要是對地區 和行業進行了清洗 對其他欄位做了替換多餘欄位處理 ,因此採取指令碼檢查,
找到page_url和網站資料進行核查
where裡面這樣寫便於查看某個欄位的清洗情況
select * from etl2_socom_data where com_district is null and length(com_industry)-length(replace(com_industry,