设置变量 length()函数 char_length() replace() 函数 max() 函数
1.一、设置变量 set @变量名=值html
set @address='中国-山东省-聊城市-莘县'; select @address
1.2 、length()函数 char_length()函数区别nginx
select length('a') ,char_length('a') ,length('中') ,char_length('中')
1.三、 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清洗字段时候有明显分割符的如何肯定新的数据表增长几个分割出的字段sql
计算出com_industry中最多有几个 - 符 以便肯定增长几个字段 最大值+1 为能够拆分红的字段数 此表为3 所以能够拆分出4个行业字段 也就是4个行业等级数据库
select max(length(com_industry)-length(replace(com_industry,'-',''))) as _max_count from etl1_socom_data
1.四、设置变量 substring_index()字符串截取函数用法api
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.五、条件判断函数 case when
case when then when then else 值 end as 字段名数组
select case when 89>101 then '大于' else '小于' end as betl1_socom_data
首先建表 步骤在视频里
字段索引 没有提 索引算法建议用BTREE算法加强查询效率函数
2.1.kettle文件名:trans_etl1_socom_data
2.2.包括控件:表输入>>>表输出
2.3.数据流方向:s_socom_data>>>>etl1_socom_data学习
2.四、表输入2.四、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_desc1 from s_socom_data as a
2.五、表输出
注意事项:
① 涉及爬虫增量操做 不要勾选裁剪表选项
②数据链接问题 选择表输出中表所在的数据库
③字段映射问题 确保数据流中的字段和物理表的字段数量一致 对应一致
首先建表增长了4个字段 演示步骤在视频里
字段索引 没有提 索引算法建议用BTREE算法加强查询效率
主要针对etl1 生成的新的com_industry进行字段拆分 清洗
3.1.kettle文件名:trans_etl2_socom_data
3.2.包括控件:表输入>>>表输出
3.3.数据流方向:etl1_socom_data>>>>etl2_socom_data
注意事项:
① 涉及爬虫增量操做 不要勾选裁剪表选项
②数据链接问题 选择表输出中表所在的数据库
③字段映射问题 确保数据流中的字段和物理表的字段数量一致 对应一致
3.四、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 也置为null when length(com_industry)-length(replace(com_industry,'-',''))=1 and length(substring_index(com_industry,'-',-1))=0 then null when 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 null when 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 null else substring_index(com_industry,'-',-1) end as com_industry4 from etl1_socom_data as a
若是自己工做是爬虫和数据处理在一块儿处理,抓取的时候其实已经判断,此步骤能够省略,若是对接上游爬虫同事,这一步首先判断,否则清洗也是无用功,通常都要求爬虫同事存储请求的url便于后面数据处理查看数据质量
注:SQL脚本中没有通过聚合过滤 3个表数据量应相等
4.2.一、sql查询 下面表我是在同一数据库中 若是不在同一数据库 from 后面应加上表所在的数据库名称
不推荐数据量大的时候使用
select count(1) from s_socom_data union all select count(1) from etl1_socom_data union all select count(1) from etl2_socom_data
4.2.2 根据 kettle转换执行完毕之后 表输出总量对比
确保前两个步骤已经无误,数据处理负责的etl清洗工做自查开始 针对数据源清洗的字段 写脚本检查 socom网站主要是对地区 和行业进行了清洗 对其余字段作了替换多余字段处理 ,所以采起脚本检查,
找到page_url和网站数据进行核查
where里面这样写便于查看某个字段的清洗状况
select * from etl2_socom_data where com_district is null and length(com_industry)-length(replace(com_industry,'-',''))=3
http://www.socom.cn/company/7320798.html
此页面数据和etl2_socom_data表最终清洗数据对比
清洗工做完成。
学习过程当中遇到什么问题或者想获取学习资源的话,欢迎加入学习交流群626062078,咱们一块儿学Python!