使用Hive作數據清洗,常常須要使用正則表達式。html
比較討厭的是,正則表達式匹配失敗的時候,hive徹底不會報錯。正則表達式
原來的寫法sql
SELECT * from ahhs_product_info where product_name NOT RLIKE '([\u4e00-\u9fa5])+' ;函數
在hive裏面的寫法ui
SELECT * from ahhs_product_info where product_name NOT RLIKE '([\\u4e00-\\u9fa5])+' ;spa
另外用到了Hive的正則表達式提取數據的函數,regexp_extract()code
用法以下:regexp
regexp_extract(string subject, string pattern, int index)htm
經過下標返回正則表達式指定的部分。regexp_extract(‘foothebar’, ‘foo(.*?)(bar)’, 2) returns ‘bar.’對象
注意,這裏的index指的是:返回全部匹配的第N個。只有當匹配成功的對象>=2的時候,index能夠選1,2。通常狀況下是0.
如:
select uid, visittime, pageUrl, r_id, regexp_extract(pageUrl,'(?<=p-)\\d+(?=\\.html)',0) as pid from sitevisitlog where statdate='20141021' --根據URL提取產品ID的使用
使用了轉義字符,就能夠正常地識別正則表達式了。
正則表達式的強大功能比Oracle的LIKE牛逼多了!!