Hive內置函數

本章介紹了在Hive中可用的內置函數。這些函數看起來非常類似於SQL的函數,除了他們的使用有點不一樣。

內置函數

Hive支持以下內置函數:

返回類型

簽名

描述

BIGINT

round(double a)

返回BIGINT最近的double值。

BIGINT

floor(double a)

返回最大BIGINT值等於或小於double。

BIGINT

ceil(double a)

它返回最小BIGINT值等於或大於double。

double

rand(), rand(int seed)

它返回一個隨機數,從行改變到行。

string

concat(string A, string B,...)

它返回從A後串聯B產生的字符串

string

substr(string A, int start)

它返回一個起始,從起始位置的子字符串,直到A.結束

string

substr(string A, int start, int length)

返回從給定長度的起始start位置開始的字符串。

string

upper(string A)

它返回從轉換的所有字符爲大寫產生的字符串。

string

ucase(string A)

和上面的一樣

string

lower(string A)

它返回轉換B的所有字符爲小寫產生的字符串。

string

lcase(string A)

和上面的一樣

string

trim(string A)

它返回字符串從A.兩端修剪空格的結果

string

ltrim(string A)

它返回A從一開始修整空格產生的字符串(左手側)

string

rtrim(string A)

rtrim(string A),它返回A從結束脩整空格產生的字符串(右側)

string

regexp_replace(string A, string B, string C)

它返回從替換所有子在B結果配合C.在Java正則表達式語法的字符串

int

size(Map<K.V>)

它返回在映射類型的元素的數量。

int

size(Array)

它返回在數組類型元素的數量。

value of

cast( as )

它把表達式的結果expr<類型>如cast('1'作爲BIGINT)代表整體轉換爲字符串'1'。如果轉換不成功,返回的是NULL。

string

from_unixtime(int unixtime)

轉換的秒數從Unix紀元(1970-01-0100:00:00 UTC)代表那一刻,在當前系統時區的時間戳字符的串格式:"1970-01-01 00:00:00"

string

to_date(string timestamp)

返回一個字符串時間戳的日期部分:to_date("1970-01-01 00:00:00") = "1970-01-01"

int

year(string date)

返回年份部分的日期或時間戳字符串:year("1970-01-01 00:00:00") = 1970, year("1970-01-01") = 1970

int

month(string date)

返回日期或時間戳記字符串月份部分:month("1970-11-01 00:00:00") = 11, month("1970-11-01") = 11

int

day(string date)

返回日期或時間戳記字符串當天部分:day("1970-11-01 00:00:00") = 1, day("1970-11-01") = 1

string

get_json_object(string json_string, string path)

提取從基於指定的JSON路徑的JSON字符串JSON對象,並返回提取的JSON字符串的JSON對象。如果輸入的JSON字符串無效,返回NULL。

示例

以下查詢演示了一些內置函數:

round() 函數

hive> SELECT round(2.6) from temp;

成功執行的查詢,能看到以下回應:

2.0

floor() 函數

hive> SELECT floor(2.6) from temp;

成功執行的查詢,能看到以下回應:

2.0

floor() 函數

hive> SELECT ceil(2.6) from temp;

成功執行的查詢,能看到以下回應:

3.0

聚合函數

Hive支持以下內置聚合函數。這些函數的用法類似於SQL聚合函數。

返回類型

簽名

描述

BIGINT

count(*), count(expr),

count(*) - 返回檢索行的總數。

DOUBLE

sum(col), sum(DISTINCT col)

返回該組或該組中的列的不同值的分組和所有元素的總和。

DOUBLE

avg(col), avg(DISTINCT col)

返回上述組或該組中的列的不同值的元素的平均值。

DOUBLE

min(col)

返回該組中的列的最小值。

DOUBLE

max(col)

返回該組中的列的最大值。