hive 正則表示式詳解
阿新 • • 發佈:2019-02-01
hive中的正則表示式還是很強大的。資料工作者平時也離不開正則表示式。對此,特意做了個hive正則表示式的小結。所有程式碼都經過親測,正常執行。
1.regexp
語法: A REGEXP B
操作型別: strings
描述: 功能與RLIKE相同
select count(*) from olap_b_dw_hotelorder_f where create_date_wid not regexp '\\d{8}'
與下面查詢的效果是等效的:
select count(*) from olap_b_dw_hotelorder_f where create_date_wid not rlike '\\d{8}';
2.regexp_extract
語法: regexp_extract(string subject, string pattern, int index)
返回值: string
說明:將字串subject按照pattern正則表示式的規則拆分,返回index指定的字元。
hive> select regexp_extract('IloveYou','I(.*?)(You)',1) from test1 limit 1;
Total jobs = 1
...
Total MapReduce CPU Time Spent: 7 seconds 340 msec
OK
love
Time taken: 28.067 seconds, Fetched: 1 row(s)
hive> select regexp_extract('IloveYou','I(.*?)(You)',2) from test1 limit 1;
Total jobs = 1
...
OK
You
Time taken: 26.067 seconds, Fetched: 1 row(s)
hive> select regexp_extract('IloveYou','(I)(.*?)(You)',1) from test1 limit 1;
Total jobs = 1
...
OK
I
Time taken: 26.057 seconds, Fetched: 1 row(s)
hive> select regexp_extract('IloveYou','(I)(.*?)(You)',0) from test1 limit 1;
Total jobs = 1
...
OK
IloveYou
Time taken: 28.06 seconds, Fetched: 1 row(s)
hive> select regexp_replace("IloveYou","You","") from test1 limit 1;
Total jobs = 1
...
OK
Ilove
Time taken: 26.063 seconds, Fetched: 1 row(s)
3.regexp_replace
語法: regexp_replace(string A, string B, string C)
返回值: string
說明:將字串A中的符合java正則表示式B的部分替換為C。注意,在有些情況下要使用轉義字元,類似oracle中的regexp_replace函式。
hive> select regexp_replace("IloveYou","You","") from test1 limit 1;
Total jobs = 1
...
OK
Ilove
Time taken: 26.063 seconds, Fetched: 1 row(s)
hive> select regexp_replace("IloveYou","You","lili") from test1 limit 1;
Total jobs = 1
...
OK
Ilovelili