开发经验总结
开发经验总结
【1】去重
Description
使用row_number() over(partition by ... order by ...)等分析函数去重时,partition by ... 中的分组字段,如果字段类型为字符串,要考虑null与空字符串''的关系,分组时是否要将这两者视为等同。如果视为等同,可以使用nvl函数进行转换;同理,在某一些数据类型中,去重时可能需要将null、空字符串‘’、'——' 等多种字符视为等同,此时可使用case when 或decode函数进行转换去重。
example
row_number() over(partition by nvl(proname,''),nvl(industrytype,''),nvl(noticetype,''),nvl(topictype,'') order by nvl(str_to_time(collectiondate),'') desc) rn
【2】两表关联条件
Description:
两表通过字段关联时,也需要像去重时候一样考虑到null或其他字符的影响,null = null 会被判定为否,导致关联不上。
【3】格式化字段
Description
在去重或两表关联条件中,一些重要字段要考虑特殊字符的影响。
(1)企业名称字段,去重和关联时都要处理特殊字符:
(2)时间字段的标准格式化,使用udf函数str_to_time();
example
(1) regexp_replace(taxpayername,'\n|\r|\t|\n\r|\\s|| | |\\(|\\)|(|)|\\[|\\]|【|】|{|}','')
(2) str_to_time(updatetime)
【4】拆json数组
Description
拆json数组的时候参考中的hive sql,一行变为多行,注意:要和Impala sql写在不同的sql脚本里
example
use fin_dw;
insert overwrite table fin_dw.t_dwd_ent_rel_shortname_dd_02
select ent_name,
ent_rel_name_s,
ent_rel_name_type,
ent_rel_name_data_src_tbl
from (select * from fin_dw.t_dwd_ent_rel_shortname_dd_01) a lateral view explode(split(ent_shortname, '[,|,]+')) t as ent_rel_name_s;
【5】excel文件直接加载到hive表
Description
怎么把excel文件直接加载到hive表,给大家提供一种快捷方式(避免花很多时间拼SQL)
example

其中临时表列数、分隔符跟csv中保持一致

浙公网安备 33010602011771号