正文
一,引言
在查詢數據的時候,往往需要我們對數據進行清洗和處理,而HSQL提供了很多函數方便我們使用,其實很多函數和普通的SQL是一樣的,接下來我們對常用的函數進行分析。
二,類型轉換函數
語法結構:
cast(源數據 as 目標數據類型) -->注意數據類型必須可以進行相互轉換,類似於java的強轉
示例:
-->字符串轉換成整型 hive> select cast('1' as int); OK 1
三,數學運算函數
3.1 round()
功能:四舍五入
語法結構:
round(源數據,[保留小數的位數])
示例:
hive> select round(4.3); OK 4.0 Time taken: 0.142 seconds, Fetched: 1 row(s) hive> select round(4.5); OK 5.0 Time taken: 0.09 seconds, Fetched: 1 row(s) hive> select round(4.523, 2); OK 4.52
3.2 ceil()
功能:向上取整
語法結構:
ceil(需要取整的數)
示例:
hive> select ceil(4.52); OK 5 Time taken: 0.084 seconds, Fetched: 1 row(s) hive> select ceil(4.2); OK 5 Time taken: 0.094 seconds, Fetched: 1 row(s)
3.3 floor()
功能:向下取整
語法結構:
floor(需要處理的數據)
示例:
hive> select floor(3.2); OK 3 Time taken: 0.092 seconds, Fetched: 1 row(s) hive> select floor(3.5); OK 3
3.4 abs()
功能:取絕對值
語法結構:
abs(需要取絕對值得數)
示例:
hive> select abs(3); OK 3 Time taken: 0.093 seconds, Fetched: 1 row(s) hive> select abs(-3); OK 3
3.5 least()
功能:求提供數據的最小值
語法格式:
least(參數1,參數2,參數3,...)
示例:
hive> select least(1,2,3); OK 1
和min的區別:min是求一組數據的最小值。一般是配合聚合函數使用。而least是單數據的中的一些數據最小值處理。
3.6 greatest
功能:求提供數據的最大值
語法格式:
greatest(參數1,參數2,參數3,...)
示例:
hive> select greatest(3,4,5); OK 5 Time taken: 0.075 seconds, Fetched: 1 row(s) hive>
和max的區別:max是求一組數據的最大值。一般是配合聚合函數使用。而greatest是單數據的中的一些數據最大值處理。
四,字符串函數
4.1 substr()
作用:字符串截取
語法格式:
substr(string, int start, [int len]) -->中括號的參數可寫可不寫,不寫默認截取到末尾
示例:
hive> select substr('aaa', 1); OK aaa Time taken: 0.098 seconds, Fetched: 1 row(s) hive> select substr('aaa', 1, 2); OK aa
4.2 concat()和concat_ws()
功能:字符串拼接
語法格式:
-->單純的將提供的參數進行拼接 concat(string A, stringB, ...) -->字符串之間用一些字符進行拼接 concat_ws(連接字符,string A, string B, ...)
示例:
hive> select concat('aaa', 'bbb'); OK aaabbb Time taken: 0.142 seconds, Fetched: 1 row(s) hive> select concat_ws('-', 'aaa', 'bbb'); OK aaa-bbb Time taken: 0.078 seconds, Fetched: 1 row(s)
4.3 length()
功能:求字符串長度
語法格式:
length(字符串)
示例:
hive> select length('abcd'); OK 4 Time taken: 0.09 seconds, Fetched: 1 row(s)
4.4 split()
功能:字符串切割
語法格式:
split(字符串, 分割符)
示例:
hive>select split('a:b:c', ':'); Total MapReduce CPU Time Spent: 2 seconds 960 msec OK ["a","b","c"] Time taken: 15.121 seconds, Fetched: 1 row(s)
注意:對於有些分割符,需要對其進行轉義,因為要考慮到正則的問題,如下:
-->這里對·和\都進行了轉義 hive> select split('192.168.174.2', '\\.'); Total MapReduce CPU Time Spent: 3 seconds 610 msec OK ["192","168","174","2"] Time taken: 13.974 seconds, Fetched: 1 row(s)
4.5 upper()和lower()
功能:字符串的大小寫切換
語法結構:
upper(字符串) lower(字符串)
示例:
hive> select lower('AA'); OK aa Time taken: 0.089 seconds, Fetched: 1 row(s) hive> select upper('aa'); OK AA Time taken: 0.069 seconds, Fetched: 1 row(s)
五,時間函數
5.1 時間獲取
直接獲取當前時間:current_timestamp
hive> select current_timestamp; OK 2019-05-22 11:22:54.179 Time taken: 0.071 seconds, Fetched: 1 row(s)
5.2 日期獲取
直接獲取當前日期:
hive> select current_date; OK 2019-05-22 Time taken: 0.076 seconds, Fetched: 1 row(s)
5.3 時間戳獲取
直接獲取當前的時間戳:
hive> select unix_timestamp(); unix_timestamp(void) is deprecated. Use current_timestamp instead. OK 1558495354 Time taken: 0.08 seconds, Fetched: 1 row(s)
5.4 時間轉換
unix時間戳轉字符串:
hive> select from_unixtime(unix_timestamp()); unix_timestamp(void) is deprecated. Use current_timestamp instead. OK 2019-05-22 11:25:57 Time taken: 0.069 seconds, Fetched: 1 row(s)
字符串轉時間戳:unix_tmiestamp()
語法格式:
unix_timestamp(時間字符串, [時間格式]) -->默認的世界格式是 yy-MM-dd HH:mm:ss
示例:
hive> select unix_timestamp("2017-08-10 17:50:30"); OK 1502358630 Time taken: 0.074 seconds, Fetched: 1 row(s) hive> select unix_timestamp("2017/08/10 17:50:30","yyyy/MM/dd HH:mm:ss"); OK 1502358630 Time taken: 0.073 seconds, Fetched: 1 row(s)
將時間字符串轉成日期:to_date
hive> select to_date("2017-09-17 16:58:32"); OK 2017-09-17 Time taken: 0.098 seconds, Fetched: 1 row(s)