Hive SQL代碼的格式化,之前一直靠規範、自律和手工調整。每個人的書寫習慣都不相同,要達到完全統一併非易事。
現在,通過HUE裏面的代碼格式化快捷鍵操作,提高代碼格式化效率和美化標準,方便好用。
主要快捷鍵:
Command + i (代碼美化、格式化)
Tab (Indent)
shift + Tab (Outdent)
Command + / (Toggle comment)
Ctrl + Shift + U (Change to lower case)
Ctrl + U (Change to upper case)
格式化流程:
1、按通用要求編寫SQL代碼,如表別名統一、了查詢使用方式統一等
2、將Hue編輯框中的代碼,通過 Command + i,總體格式化
3、根據需要具體微調,如Tab對齊調整、Command + / 註釋添加和去除、大小寫調整等
4、複製代碼到具體etl腳本,發佈上線
樣例:
1、原格式
--nisj
--2020-04-13
--Describe:推廣者信息
insert overwrite table ${c_to_dwt}.dwt_promotion_promoters_full_1d partition (dt='${dt}')
select
coalesce(a1.id,a2.id) promoters_id,
coalesce(a1.pid,a2.pid) p_id,
coalesce(a1.pname,a2.pname) p_name,
coalesce(a1.username,a2.username) user_name,
coalesce(a1.platform,a2.platform) plat_form,
coalesce(a1.uri,a2.uri) uri,
coalesce(a1.customnumber,a2.customnumber) custom_number,
coalesce(a1.state,a2.state) state,
coalesce(uri_signal,'') uri_signal,coalesce(customnumber_signal,'') customnumber_signal,
coalesce(a1.createtime,a2.createtime) create_time,
coalesce(a1.updatetime,a2.updatetime) update_time,
coalesce(a1.other_platform,a2.other_platform) other_platform,
coalesce(a1.total_income,a2.total_income) total_income,
coalesce(a1.apply_withdraw,a2.apply_withdraw) apply_withdraw,
coalesce(a1.proportion,a2.proportion) proportion,
coalesce(a1.is_frozen,a2.is_frozen) is_frozen
from (select id,pid,pname,username,platform,uri,customnumber,state,from_unixtime(int(createtime/1000)) createtime,from_unixtime(int(updatetime/1000)) updatetime,
reverse(uri_signal) uri_signal,row_number()over(partition by id) uri_rn,
otherplatform other_platform,totalincome total_income,applywithdraw apply_withdraw,proportion proportion,isfrozen is_frozen
from ${c_from_sds}.mysql_api_cfg__promoters_view
LATERAL VIEW explode(split(reverse(uri),',')) uri_list AS uri_signal
where from_unixtime(int(createtime/1000),'yyyy-MM-dd')<='${dt}' and dc__op<>'D') a1
full join (
select id,pid,pname,username,platform,uri,customnumber,state,from_unixtime(int(createtime/1000)) createtime,from_unixtime(int(updatetime/1000)) updatetime,
reverse(customnumber_signal) customnumber_signal,row_number()over(partition by id) customnumber_rn,
otherplatform other_platform,totalincome total_income,applywithdraw apply_withdraw,proportion proportion,isfrozen is_frozen
from ${c_from_sds}.mysql_api_cfg__promoters_view
LATERAL VIEW explode(split(reverse(customnumber),',')) customnumber_list AS customnumber_signal
where from_unixtime(int(createtime/1000),'yyyy-MM-dd')<='${dt}' and dc__op<>'D') a2
on a1.id=a2.id and a1.uri_rn=a2.customnumber_rn
;
2、格式化後
--nisj
--2020-04-13
--Describe:推廣者信息
INSERT overwrite TABLE ${c_to_dwt}.dwt_promotion_promoters_full_1d partition (dt='${dt}')
SELECT coalesce(a1.id,a2.id) promoters_id,
coalesce(a1.pid,a2.pid) p_id,
coalesce(a1.pname,a2.pname) p_name,
coalesce(a1.username,a2.username) user_name,
coalesce(a1.platform,a2.platform) plat_form,
coalesce(a1.uri,a2.uri) uri,
coalesce(a1.customnumber,a2.customnumber) custom_number,
coalesce(a1.state,a2.state) STATE,
coalesce(uri_signal,'') uri_signal,
coalesce(customnumber_signal,'') customnumber_signal,
coalesce(a1.createtime,a2.createtime) create_time,
coalesce(a1.updatetime,a2.updatetime) update_time,
coalesce(a1.other_platform,a2.other_platform) other_platform,
coalesce(a1.total_income,a2.total_income) total_income,
coalesce(a1.apply_withdraw,a2.apply_withdraw) apply_withdraw,
coalesce(a1.proportion,a2.proportion) proportion,
coalesce(a1.is_frozen,a2.is_frozen) is_frozen
FROM
(SELECT id,
pid,
pname,
username,
platform,
uri,
customnumber,
STATE,
from_unixtime(int(createtime/1000)) createtime,
from_unixtime(int(updatetime/1000)) updatetime,
reverse(uri_signal) uri_signal,
row_number()over(partition BY id) uri_rn,
otherplatform other_platform,
totalincome total_income,
applywithdraw apply_withdraw,
proportion proportion,
isfrozen is_frozen
FROM ${c_from_sds}.mysql_api_cfg__promoters_view LATERAL VIEW explode(split(reverse(uri),',')) uri_list AS uri_signal
WHERE from_unixtime(int(createtime/1000),'yyyy-MM-dd')<='${dt}'
AND dc__op<>'D') a1
FULL JOIN
(SELECT id,
pid,
pname,
username,
platform,
uri,
customnumber,
STATE,
from_unixtime(int(createtime/1000)) createtime,
from_unixtime(int(updatetime/1000)) updatetime,
reverse(customnumber_signal) customnumber_signal,
row_number()over(partition BY id) customnumber_rn,
otherplatform other_platform,
totalincome total_income,
applywithdraw apply_withdraw,
proportion proportion,
isfrozen is_frozen
FROM ${c_from_sds}.mysql_api_cfg__promoters_view LATERAL VIEW explode(split(reverse(customnumber),',')) customnumber_list AS customnumber_signal
WHERE from_unixtime(int(createtime/1000),'yyyy-MM-dd')<='${dt}'
AND dc__op<>'D') a2 ON a1.id=a2.id AND a1.uri_rn=a2.customnumber_rn ;