hive 學習系列之七 hive 常用數據清洗函數


1,case when 的利用,清洗諸如評分等的內容,用例如下。

case 
   when new.comment_grade = '五星商戶' then 50
   when new.comment_grade = '准五星商戶' then 45
   when new.comment_grade = '四星商戶' then 40
   when new.comment_grade = '准四星商戶' then 35
   when new.comment_grade = '三星商戶' then 30
   when new.comment_grade = '准三星商戶' then 25
   when new.comment_grade = '二星商戶' then 20
   when new.comment_grade = '准二星商戶' then 15
   when new.comment_grade = '一星商戶' then 10
   when new.comment_grade = '准一星商戶' then 5
   when new.comment_grade = '該商戶暫無星級' then 0
   when new.comment_grade is NULL then old.comment_grade
   else new.comment_grade
END as `new.comment_grade`,

2, 替換字符串中的一些內容。

regexp_replace(new.avg_price, '-', '')
替換 avg_price 中的中划線。

3, 字符串切分函數

split(a.tag_flag, '>')[1],
具體例子:
select split('a,b', ',')[0]  ===> 結果 a

4, 字符串拼接函數

SELECT concat('1', '2');     ====》 結果 12
SELECT concat('1', '2', '3');   ===> 結果 123 


### 5, 去除字符串兩端空格

trim(a.city)



6, 使用left join 或者 right join 補全數據

例如根據兩張表,其中一張表格table2含有省份和城市的信息,
其中一張表table1只有城市信息,需要補全table1 中的省份信息,可以像如下做法:
select 
	a.name,
	b.province,
	a.city
from table1 a left join table2 b on  a.city = b.city;

7,其他:清除一些不符合條件的數據

可以使用等值判斷來處理數據
清除一些不符合條件的數據。
INSERT OVERWRITE table ods.js_beauty_tmp
SELECT *
from ods.js_beauty_tmp
WHERE map_lat != ''
AND map_lng != ''
AND map_lat IS NOT NULL
AND map_lng IS NOT NULL
AND map_lat != 0
AND map_lng != 0
AND map_lat not like '-%'
AND map_lng not like '-%'
and city != '其他城市'
and city != '點評實驗室';


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM