hive2.0中常用的內建函式大全
目錄
- 數學函式
- abs(x):返回絕對值
- greatest(T v1, T v2, ...):求最大值
- least(T v1, T v2, ...):求最小值
- round(DOUBLE a):四捨五入
- round(DOUBLE a, INT d):指定精度的四捨五入
- bround(DOUBLE a):銀行家舍入法
- bround(DOUBLE a, INT d):銀行家指定精度的舍入法,保留d位小數
- floor(DOUBLE a):向下取整
- ceil(DOUBLE a), ceiling(DOUBLE a):向上取整
- rand(), rand(INT seed):取隨機數
- exp(DOUBLE a), exp(DECIMAL a):自然指數
- ln(DOUBLE a), ln(DECIMAL a):自然數為底的對數
- log10(DOUBLE a), log10(DECIMAL a):以10為底對數
- log(DOUBLE base, DOUBLE a),log(DECIMAL base, DECIMAL a):對數函式
- log2(DOUBLE a), log2(DECIMAL a):以2為底對數函式
- pow(DOUBLE a, DOUBLE p), power(DOUBLE a, DOUBLE p):冪運算函式
- sqrt(DOUBLE a), sqrt(DECIMAL a):開平方函式
- bin(BIGINT a):二進位制函式
- hex(BIGINT a) hex(STRING a) hex(BINARY a):十六進位制函式
- unhex(STRING a):反轉十六進位制函式
- conv(BIGINT num, INT from_base, INT to_base), conv(STRING num, INT from_base, INT to_base):進位制轉換函式
- pmod(INT a, INT b), pmod(DOUBLE a, DOUBLE b):取餘函式
- sin(DOUBLE a), sin(DECIMAL a):正弦函式
- asin(DOUBLE a), asin(DECIMAL a):反正弦函式
- cos(DOUBLE a), cos(DECIMAL a):餘弦函式
- acos(DOUBLE a), acos(DECIMAL a):反餘弦函式
- tan(DOUBLE a), tan(DECIMAL a):正切函式
- atan(DOUBLE a), atan(DECIMAL a):反正切函式
- degrees(DOUBLE a), degrees(DECIMAL a):弧度值轉換角度值
- radians(DOUBLE a), radians(DOUBLE a):角度值轉換成弧度值
- positive(INT a), positive(DOUBLE a):返回a
- negative(INT a), negative(DOUBLE a):返回a的相反數
- sign(DOUBLE a), sign(DECIMAL a):判斷數值是正數,0或負數
- e():數學常數e
- pi():數學常數pi
- factorial(INT a):求a的階乘
- cbrt(DOUBLE a):求a的立方根
- shiftleft(TINYINT|SMALLINT|INT a, INT b),shiftleft(BIGINT a, INT b):按位左移
- shiftright(TINYINT|SMALLINT|INT a, INTb),shiftright(BIGINT a, INT b):按拉右移
- shiftrightunsigned(TINYINT|SMALLINT|INTa, INT b),shiftrightunsigned(BIGINT a, INT b):無符號按位右移(<<<)
- 關係運算
- 等值比較: =
- 不等值比較: <>
- 小於比較: <
- 小於等於比較: <=
- 大於比較: >
- 大於等於比較: >=
- 空值判斷: IS NULL
- 非空判斷: IS NOT NULL
- LIKE比較: LIKE
- JAVA的LIKE操作: RLIKE
- REGEXP操作: REGEXP
- 數學運算:
- 邏輯運算:
- 型別轉換函式
- 日期函式
- UNIX時間戳轉日期函式: from_unixtime
- 獲取當前UNIX時間戳函式: unix_timestamp
- 日期轉UNIX時間戳函式: unix_timestamp
- 指定格式日期轉UNIX時間戳函式: unix_timestamp
- 日期時間轉日期函式: to_date
- 日期轉年函式: year
- 日期轉月函式: month
- 日期轉天函式: day
- 日期轉小時函式: hour
- 日期轉分鐘函式: minute
- 日期轉秒函式: second
- 日期轉周函式: weekofyear
- 日期比較函式: datediff
- 日期增加函式: date_add
- 日期減少函式: date_sub
- 條件函式
- 字元函式
- 字串長度函式:length
- 字串反轉函式:reverse
- 字串連線函式:concat
- 帶分隔符字串連線函式:concat_ws
- 字串擷取函式:substr,substring
- 字串擷取函式:substr,substring
- 字串轉大寫函式:upper,ucase
- 去空格函式:trim
- 左邊去空格函式:ltrim
- 右邊去空格函式:rtrim
- 正則表示式替換函式:regexp_replace
- 正則表示式解析函式:regexp_extract
- URL解析函式:parse_url
- json解析函式:get_json_object
- 空格字串函式:space
- 重複字串函式:repeat
- 首字元ascii函式:ascii
- 左補足函式:lpad
- 右補足函式:rpad
- 分割字串函式: split
- 集合查詢函式: find_in_set
- 聚合函式
- 個數統計函式: count
- 總和統計函式: sum
- 平均值統計函式: avg
- 最小值統計函式: min
- 最大值統計函式: max
- 非空集合總體變數函式: var_pop
- 非空集合樣本變數函式: var_samp
- 總體標準偏離函式: stddev_pop
- 樣本標準偏離函式: stddev_samp
- 中位數函式: percentile
- 中位數函式: percentile
- 近似中位數函式: percentile_approx
- 近似中位數函式: percentile_approx
- 直方圖: histogram_numeric
- 複合型別構建操作
- 複雜型別訪問操作
- 複雜型別長度統計函式
數學函式
abs(x):返回絕對值
返回值型別:DOUBLE
說明:返回x的絕對值
> select abs(-12);
+------+--+
| _c0 |
+------+--+
| 12 |
+------+--+
> select abs(12);
+------+--+
| _c0 |
+------+--+
| 12 |
+------+--+
greatest(T v1, T v2, …):求最大值
返回值:泛型
說明:求函式中所有引數的最大值
> select greatest(12, 32, 64, 2);
+------+--+
| _c0 |
+------+--+
| 64 |
+------+--+
> select greatest('sql', 'spark', 'hive', 'hbase');
+------+--+
| _c0 |
+------+--+
| sql |
+------+--+
least(T v1, T v2, …):求最小值
返回值:泛型
說明:求函式中所有引數的最小值
> select least(12, 32, 64, 2);
+------+--+
| _c0 |
+------+--+
| 2 |
+------+--+
> select least('sql', 'spark', 'hive', 'hbase');
+--------+--+
| _c0 |
+--------+--+
| hbase |
+--------+--+
round(DOUBLE a):四捨五入
返回值:BIGINT
說明:返回對a四捨五入的BIGINT值
> select round(4.6);
+------+--+
| _c0 |
+------+--+
| 5.0 |
+------+--+
> select round(4.3);
+------+--+
| _c0 |
+------+--+
| 4.0 |
+------+--+
round(DOUBLE a, INT d):指定精度的四捨五入
返回值:DOUBLE
說明:返回DOUBLE型d的保留n位小數的DOUBLE型的近似值
> select round(4.62345, 2);
+-------+--+
| _c0 |
+-------+--+
| 4.62 |
+-------+--+
> select round(4.623456, 5);
+----------+--+
| _c0 |
+----------+--+
| 4.62346 |
+----------+--+
bround(DOUBLE a):銀行家舍入法
返回值:BIGINT
說明:銀行家舍入法(1~4:舍,6~9:進,5->前位數是偶:舍,5->前位數是奇:進)
> select bround(8.25, 1);
+----------+--+
| _c0 |
+----------+--+
| 8.2 |
+----------+--+
> select bround(5.25);
+----------+--+
| _c0 |
+----------+--+
| 5 |
+----------+--+
> select bround(4.5);
+----------+--+
| _c0 |
+----------+--+
| 4 |
+----------+--+
bround(DOUBLE a, INT d):銀行家指定精度的舍入法,保留d位小數
返回值:DOUBLE
說明:銀行家指定精度的舍入法,保留d位小數
> select bround(2.35, 1);
+----------+--+
| _c0 |
+----------+--+
| 2.4 |
+----------+--+
> select bround(2.25, 1);
+----------+--+
| _c0 |
+----------+--+
| 2.2 |
+----------+--+
floor(DOUBLE a):向下取整
返回值: BIGINT
說明: 返回等於或者小於該double變數的最大的整數
> select floor(8.21);
+------+--+
| _c0 |
+------+--+
| 8 |
+------+--+
> select floor(-6.5);
+------+--+
| _c0 |
+------+--+
| -7 |
+------+--+
ceil(DOUBLE a), ceiling(DOUBLE a):向上取整
返回值: BIGINT
說明: 返回等於或者大於該double變數的最小的整數
> select ceil(2.3);
+------+--+
| _c0 |
+------+--+
| 3 |
+------+--+
> select ceil(2.9);
+------+--+
| _c0 |
+------+--+
| 3 |
+------+--+
rand(), rand(INT seed):取隨機數
返回值: double
說明: 返回一個0到1範圍內的隨機數。如果指定種子seed,則會等到一個穩定的隨機數序列
> select rand();
+--------------------+--+
| _c0 |
+--------------------+--+
| 0.638574497022045 |
+--------------------+--+
> select rand(6);
+---------------------+--+
| _c0 |
+---------------------+--+
| 0.7307886238322471 |
+---------------------+--+
exp(DOUBLE a), exp(DECIMAL a):自然指數
返回值: double
說明: 返回自然對數e的a次方
> select exp(3.4);
+--------------------+--+
| _c0 |
+--------------------+--+
| 29.96410004739701 |
+--------------------+--+
ln(DOUBLE a), ln(DECIMAL a):自然數為底的對數
返回值:DOUBLE
說明:以自然數為底的對數,a可為小數
> select ln(3.5);
+--------------------+--+
| _c0 |
+--------------------+--+
| 1.252762968495368 |
+--------------------+--+
log10(DOUBLE a), log10(DECIMAL a):以10為底對數
返回值: double
說明: 返回以10為底的a的對數
> select log10(2.5);
+---------------------+--+
| _c0 |
+---------------------+--+
| 0.3979400086720376 |
+---------------------+--+
log(DOUBLE base, DOUBLE a),log(DECIMAL base, DECIMAL a):對數函式
返回值: double
說明: 返回以base為底的a的對數
> select log(3, 9);
+------+--+
| _c0 |
+------+--+
| 2.0 |
+------+--+
log2(DOUBLE a), log2(DECIMAL a):以2為底對數函式
返回值: double
說明: 返回以2為底的a的對數
> select log2(4);
+------+--+
| _c0 |
+------+--+
| 2.0 |
+------+--+
pow(DOUBLE a, DOUBLE p), power(DOUBLE a, DOUBLE p):冪運算函式
返回值: double
說明: 返回a的p次冪
> select pow(2, 3);
+------+--+
| _c0 |
+------+--+
| 8.0 |
+------+--+
> select power(3.0, 2.0);
+------+--+
| _c0 |
+------+--+
| 9.0 |
+------+--+
sqrt(DOUBLE a), sqrt(DECIMAL a):開平方函式
返回值: double
說明: 返回a的平方根
> select sqrt(16.0);
+------+--+
| _c0 |
+------+--+
| 4.0 |
+------+--+
bin(BIGINT a):二進位制函式
返回值: string
說明: 返回a的二進位制程式碼表示
> select bin(3245);
+---------------+--+
| _c0 |
+---------------+--+
| 110010101101 |
+---------------+--+
hex(BIGINT a) hex(STRING a) hex(BINARY a):十六進位制函式
返回值: string
說明: 如果變數是int型別,那麼返回a的十六進位制表示;如果變數是string型別,則返回該字串的十六進位制表示
> select hex(36);
+------+--+
| _c0 |
+------+--+
| 24 |
+------+--+
> select hex('a');
+------+--+
| _c0 |
+------+--+
| 61 |
+------+--+
unhex(STRING a):反轉十六進位制函式
返回值: string
說明: 返回該十六進位制字串所程式碼的字串
> select unhex(61);
+------+--+
| _c0 |
+------+--+
| a |
+------+--+
conv(BIGINT num, INT from_base, INT to_base), conv(STRING num, INT from_base, INT to_base):進位制轉換函式
返回值: string
說明: 將數值num從from_base進位制轉化到to_base進位制
> select conv(20, 10, 2);
+--------+--+
| _c0 |
+--------+--+
| 10100 |
+--------+--+
> select conv(10100, 2, 10);
+------+--+
| _c0 |
+------+--+
| 20 |
+------+--+
pmod(INT a, INT b), pmod(DOUBLE a, DOUBLE b):取餘函式
返回值: int double
說明: 返回正的a除以b的餘數
> select pmod(9, 6);
+------+--+
| _c0 |
+------+--+
| 3 |
+------+--+
sin(DOUBLE a), sin(DECIMAL a):正弦函式
返回值: double
說明: 返回a的正弦值
> select sin(0.5);
+--------------------+--+
| _c0 |
+--------------------+--+
| 0.479425538604203 |
+--------------------+--+
asin(DOUBLE a), asin(DECIMAL a):反正弦函式
返回值: double
說明: 返回a的反正弦值
> select asin(0.479425538604203);
+------+--+
| _c0 |
+------+--+
| 0.5 |
+------+--+
cos(DOUBLE a), cos(DECIMAL a):餘弦函式
返回值: double
說明: 返回a的餘弦值
> select cos(0.5);
+---------------------+--+
| _c0 |
+---------------------+--+
| 0.8775825618903728 |
+---------------------+--+
acos(DOUBLE a), acos(DECIMAL a):反餘弦函式
返回值: double
說明: 返回a的反餘弦值
> select acos(0.8775825618903728)
+---------------------+--+
| _c0 |
+---------------------+--+
| 0.4999999999999999 |
+---------------------+--+
tan(DOUBLE a), tan(DECIMAL a):正切函式
返回值: double
說明: 返回a的正切值
> select tan(0.5);
+---------------------+--+
| _c0 |
+---------------------+--+
| 0.5463024898437905 |
+---------------------+--+
atan(DOUBLE a), atan(DECIMAL a):反正切函式
返回值: double
說明: 返回a的反正切值
> select atan(0.5463024898437905);
+------+--+
| _c0 |
+------+--+
| 0.5 |
+------+--+
degrees(DOUBLE a), degrees(DECIMAL a):弧度值轉換角度值
返回值: double
說明: 返回弧度a的角度值
> select degrees(0.6);
+--------------------+--+
| _c0 |
+--------------------+--+
| 34.37746770784939 |
+--------------------+--+
radians(DOUBLE a), radians(DOUBLE a):角度值轉換成弧度值
返回值: double
說明: 返回角度a的弧度值
> select radians(34.37746770784939);
+------+--+
| _c0 |
+------+--+
| 0.6 |
+------+--+
positive(INT a), positive(DOUBLE a):返回a
返回值: int double
說明: 返回a
> select positive(3.5);
+------+--+
| _c0 |
+------+--+
| 3.5 |
+------+--+
negative(INT a), negative(DOUBLE a):返回a的相反數
返回值: int double
說明: 返回-a
> select negative(3.6);
+-------+--+
| _c0 |
+-------+--+
| -3.6 |
+-------+--+
> select negative(-3.6);
+------+--+
| _c0 |
+------+--+
| 3.6 |
+------+--+
sign(DOUBLE a), sign(DECIMAL a):判斷數值是正數,0或負數
返回值: double
說明: 如果a是正數則返回1.0,是負數則返回-1.0,否則返回0.0
> select sign(6);
+------+--+
| _c0 |
+------+--+
| 1.0 |
+------+--+
> select sign(-60);
+-------+--+
| _c0 |
+-------+--+
| -1.0 |
+-------+--+
> select sign(0);
+------+--+
| _c0 |
+------+--+
| 0.0 |
+------+--+
e():數學常數e
返回值: double
說明: 返回數學常數e
> select e();
+--------------------+--+
| _c0 |
+--------------------+--+
| 2.718281828459045 |
+--------------------+--+
pi():數學常數pi
返回值: double
說明: 返回數學常數pi
> select pi();
+--------------------+--+
| _c0 |
+--------------------+--+
| 3.141592653589793 |
+--------------------+--+
factorial(INT a):求a的階乘
返回值: int
說明: 返回a的階乘
> select factorial(4);
+------+--+
| _c0 |
+------+--+
| 24 |
+------+--+
cbrt(DOUBLE a):求a的立方根
返回值: double
說明: 返回a的立方根
> select cbrt(27.0);
+------+--+
| _c0 |
+------+--+
| 3.0 |
+------+--+
shiftleft(TINYINT|SMALLINT|INT a, INT b),shiftleft(BIGINT a, INT b):按位左移
返回值: int bigint
說明: 返回a按位左移b位
> select shiftleft(2, 2);
+------+--+
| _c0 |
+------+--+
| 8 |
+------+--+
shiftright(TINYINT|SMALLINT|INT a, INTb),shiftright(BIGINT a, INT b):按拉右移
返回值: int bigint
說明: 返回a按位右移b位
> select shiftright(22, 2);
+------+--+
| _c0 |
+------+--+
| 5 |
+------+--+
shiftrightunsigned(TINYINT|SMALLINT|INTa, INT b),shiftrightunsigned(BIGINT a, INT b):無符號按位右移(<<<)
返回值: int bigint
說明: 返回a按位右移b位
> select shiftrightunsigned(32, 2);
+------+--+
| _c0 |
+------+--+
| 8 |
+------+--+
> select shiftrightunsigned(-32, 2);
+--------------------+--+
| _c0 |
+--------------------+--+
| 1073741823 |
+--------------------+--+
關係運算
等值比較: =
語法:A=B
操作型別:所有基本型別
描述: 如果表示式A與表示式B相等,則為TRUE;否則為FALSE
> select 1=1;
+-------+--+
| _c0 |
+-------+--+
| true |
+-------+--+
不等值比較: <>
語法: A <> B
操作型別: 所有基本型別
描述: 如果表示式A為NULL,或者表示式B為NULL,返回NULL;如果表示式A與表示式B不相等,則為TRUE;否則為FALSE
> select 1<>2;
+-------+--+
| _c0 |
+-------+--+
| true |
+-------+--+
> select 1<>1;
+--------+--+
| _c0 |
+--------+--+
| false |
+--------+--+
> select NULL<>2;
+-------+--+
| _c0 |
+-------+--+
| NULL |
+-------+--+
小於比較: <
語法: A < B
操作型別:所有基本型別
描述: 如果表示式A為NULL,或者表示式B為NULL,返回NULL;如果表示式A小於表示式B,則為TRUE;否則為FALSE
> select NULL < 2;
+-------+--+
| _c0 |
+-------+--+
| NULL |
+-------+--+
> select 1 < 2;
+-------+--+
| _c0 |
+-------+--+
| true |
+-------+--+
> select 2 < 1;
+--------+--+
| _c0 |
+--------+--+
| false |
+--------+--+
小於等於比較: <=
語法: A <= B
操作型別: 所有基本型別
描述: 如果表示式A為NULL,或者表示式B為NULL,返回NULL;如果表示式A小於或者等於表示式B,則為TRUE;否則為FALSE
> select NULL <= 2;
+-------+--+
| _c0 |
+-------+--+
| NULL |
+-------+--+
> select 0 <= 2;
+-------+--+
| _c0 |
+-------+--+
| true |
+-------+--+
大於比較: >
語法: A > B
操作型別: 所有基本型別
描述: 如果表示式A為NULL,或者表示式B為NULL,返回NULL;如果表示式A大於表示式B,則為TRUE;否則為FALSE
> select null > 2;
+-------+--+
| _c0 |
+-------+--+
| NULL |
+-------+--+
> select 3 > 2;
+-------+--+
| _c0 |
+-------+--+
| true |
+-------+--+
大於等於比較: >=
語法: A >= B
操作型別: 所有基本型別
描述: 如果表示式A為NULL,或者表示式B為NULL,返回NULL;如果表示式A大於或者等於表示式B,則為TRUE;否則為FALSE
> select null >= 2;
+-------+--+
| _c0 |
+-------+--+
| NULL |
+-------+--+
> select 6 >= 2;
+-------+--+
| _c0 |
+-------+--+
| true |
+-------+--+
注意:String的比較要注意(常用的時間比較可以先 to_date 之後再比較)
> select '20181208 00:00:00' < '20181208', '20181208 00:00:00' > '20181208', '20181208 00:00:00' = '20181208';
+--------+-------+--------+--+
| _c0 | _c1 | _c2 |
+--------+-------+--------+--+
| false | true | false |
+--------+-------+--------+--+
空值判斷: IS NULL
語法: A IS NULL
操作型別: 所有型別
描述: 如果表示式A的值為NULL,則為TRUE;否則為FALSE
> select null is null;
+-------+--+
| _c0 |
+-------+--+
| true |
+-------+--+
> select '' is null;
+--------+--+
| _c0 |
+--------+--+
| false |
+--------+--+
> select 0 is null;
+--------+--+
| _c0 |
+--------+--+
| false |
+--------+--+
非空判斷: IS NOT NULL
語法: A IS NOT NULL
操作型別: 所有型別
描述: 如果表示式A的值為NULL,則為FALSE;否則為TRUE
> select 0 is not null;
+-------+--+
| _c0 |
+-------+--+
| true |
+-------+--+
> select null is not null;
+--------+--+
| _c0 |
+--------+--+
| false |
+--------+--+
LIKE比較: LIKE
語法: A LIKE B
操作型別: strings
描述: 如果字串A或者字串B為NULL,則返回NULL;如果字串A符合表示式B 的正則語法,則為TRUE;否則為FALSE。B中字元”_”表示任意單個字元,而字元”%”表示任意數量的字元。
> select 'hive' like 'h%';
+-------+--+
| _c0 |
+-------+--+
| true |
+-------+--+
> select 'hive' like '_______';
+--------+--+
| _c0 |
+--------+--+
| false |
+--------+--+
> select 'hive' like '____';
+-------+--+
| _c0 |
+-------+--+
| true |
+-------+--+
> select 'fuyun' like null;
+-------+--+
| _c0 |
+-------+--+
| NULL |
+-------+--+
注意:否定比較時候用NOT A LIKE B
> select not 'hive' like '____';
+--------+--+
| _c0 |
+--------+--+
| false |
+--------+--+
JAVA的LIKE操作: RLIKE
語法: A RLIKE B
操作型別: strings
描述: 如果字串A或者字串B為NULL,則返回NULL;如果字串A符合JAVA正則表示式B的正則語法,則為TRUE;否則為FALSE。
> select 'fuyun' rlike '^f.*n$';
+-------+--+
| _c0 |
+-------+--+
| true |
+-------+--+
注意:判斷一個字串是否全為數字:
> select '12345' rlike '^\\d+$';
+-------+--+
| _c0 |
+-------+--+
| true |
+-------+--+
> select '12a34b' rlike '^\\d+$';
+--------+--+
| _c0 |
+--------+--+
| false |
+--------+--+
REGEXP操作: REGEXP
語法: A REGEXP B
操作型別: strings
描述: 功能與RLIKE相同
> select 'fuyun' regexp '^f.*n$';
+-------+--+
| _c0 |
+-------+--+
| true |
+-------+--+
數學運算:
加法操作: +
語法: A + B
操作型別:所有數值型別
說明:返回A與B相加的結果。結果的數值型別等於A的型別和B的型別的最小父型別(詳見資料型別的繼承關係)。比如,int + int 一般結果為int型別,而 int + double 一般結果為double型別
+------+--+
| _c0 |
+------+--+
| 10 |
+------+--+
> select 2 + 20.2;
+-------+--+
| _c0 |
+-------+--+
| 22.2 |
+-------+--+
減法操作: -
語法: A – B
操作型別:所有數值型別
說明:返回A與B相減的結果。結果的數值型別等於A的型別和B的型別的最小父型別(詳見資料型別的繼承關係)。比如,int – int 一般結果為int型別,而 int – double 一般結果為double型別
> select 2 - 3;
+------+--+
| _c0 |
+------+--+
| -1 |
+------+--+
> select 6.6 - 3;
+---------------------+--+
| _c0 |
+---------------------+--+
| 3.5999999999999996 |
+---------------------+--+
乘法操作:
語法: A * B
操作型別:所有數值型別
說明:返回A與B相乘的結果。結果的數值型別等於A的型別和B的型別的最小父型別(詳見資料型別的繼承關係)。注意,如果A乘以B的結果超過預設結果型別的數值範圍,則需要通過cast將結果轉換成範圍更大的數值型別
> select 6.2 * 2;
+-------+--+
| _c0 |
+-------+--+
| 12.4 |
+-------+--+
除法操作: /
語法: A / B
操作型別:所有數值型別
說明:返回A除以B的結果。結果的數值型別為double
> select 80 / 20;
+------+--+
| _c0 |
+------+--+
| 4.0 |
+------+--+
注意:hive中最高精度的資料型別是double,只精確到小數點後16位,在做除法運算的時候要特別注意
> select ceil(28.0/6.999999999999999999999);
+------+--+
| _c0 |
+------+--+
| 4 |
+------+--+
> select ceil(28.0/6.99999999999999);
+------+--+
| _c0 |
+------+--+
| 5 |
+------+--+
> select 28.0/6.99999999999999;
+--------------------+--+
| _c0 |
+--------------------+--+
| 4.000000000000005 |
+--------------------+--+
> select 28.0/6.999999999999999999999;
+------+--+
| _c0 |
+------+--+
| 4.0 |
+------+--+
取餘操作: %
語法: A % B
操作型別:所有數值型別
說明:返回A除以B的餘數。結果的數值型別等於A的型別和B的型別的最小父型別(詳見資料型別的繼承關係)。
> select 11 % 2;
+------+--+
| _c0 |
+------+--+
| 1 |
+------+--+
> select 6.8 % 2;
+---------------------+--+
| _c0 |
+---------------------+--+
| 0.7999999999999998 |
+---------------------+--+
注意:精度在hive中是個很大的問題,類似這樣的操作最好通過round指定精度
> select round(6.8 % 2, 2);
+------+--+
| _c0 |
+------+--+
| 0.8 |
+------+--+
位與操作: &
語法: A & B
操作型別:所有數值型別
說明:返回A和B按位進行與操作的結果(兩個正數的二進位制同位數都為1取1,有一個數為0取0)。結果的數值型別等於A的型別和B的型別的最小父型別(詳見資料型別的繼承關係)。
> select 3 & 2;
+------+--+
| _c0 |
+------+--+
| 2 |
+------+--+
> select 4 & 8;
+------+--+
| _c0 |
+------+--+
| 0 |
+------+--+
位或操作: |
語法: A | B
操作型別:所有數值型別
說明:返回A和B按位進行或操作的結果(兩個正數的二進位制同位數有一個為1取1,兩個數為0取0)。結果的數值型別等於A的型別和B的型別的最小父型別(詳見資料型別的繼承關係)。
> select 3 | 2;
+------+--+
| _c0 |
+------+--+
| 3 |
+------+--+
> select 4 | 8;
+------+--+
| _c0 |
+------+--+
| 12 |
+------+--+
位異或操作: ^
語法: A ^ B
操作型別:所有數值型別
說明:返回A和B按位進行異或操作的結果(兩個正數的二進位制同位數相同取0,不同取1)。結果的數值型別等於A的型別和B的型別的最小父型別(詳見資料型別的繼承關係)。
> select 2 ^ 3;
+------+--+
| _c0 |
+------+--+
| 1 |
+------+--+
hive> select 6 ^ 4 from iteblog;
2
位取反操作: ~
語法: ~A
操作型別:所有數值型別
說明:返回A按位取反操作的結果。結果的數值型別等於A的型別。
> select ~3;
+------+--+
| _c0 |
+------+--+
| -4 |
+------+--+
> select ~(-4);
+------+--+
| _c0 |
+------+--+
| 3 |
+------+--+
邏輯運算:
邏輯與操作: AND
語法: A AND B
操作型別:boolean
說明:如果A和B均為TRUE,則為TRUE;否則為FALSE。如果A為NULL或B為NULL,則為NULL
> select 2 = 1 and 3 = 3;
+--------+--+
| _c0 |
+--------+--+
| false |
+--------+--+
邏輯或操作: OR
語法: A OR B
操作型別:boolean
說明:如果A為TRUE,或者B為TRUE,或者A和B均為TRUE,則為TRUE;否則為FALSE
> select 2 = 1 or 3 = 3;
+-------+--+
| _c0 |
+-------+--+
| true |
+-------+--+
邏輯非操作: NOT
語法: NOT A
操作型別:boolean
說明:如果A為FALSE,或者A為NULL,則為TRUE;否則為FALSE
> select not 2 = 3;
+-------+--+
| _c0 |
+-------+--+
| true |
+-------+--+
型別轉換函式
binary(string|binary):將輸入的值轉換成二進位制
語法: binary(string|binary)
返回值: binary
說明: 將輸入的值轉換成二進位制
> select binary('fuyun');
+--------+--+
| _c0 |
+--------+--+
| fuyun |
+--------+--+
cast(expr as ):將expr轉換成type型別
語法: cast(expr as )
返回值: Expected “=” to follow “type”
說明:返回轉換後的資料型別
字串轉日期
> select cast('2018-12-08' as date);
+-------------+--+
| _c0 |
+-------------+--+
| 2018-12-08 |
+-------------+--+
字串轉時間戳
> select cast('2018-12-08 23:30:56' as timestamp);
+------------------------+--+
| _c0 |
+------------------------+--+
| 2018-12-08 23:30:56.0 |
+------------------------+--+
時間戳轉換為日期
> select cast(timestamp('2018-12-08 23:30:56.0') as date);
+-------------+--+
| _c0 |
+-------------+--+
| 2018-12-08 |
+-------------+--+
時間戳轉換為日期
> select cast(timestamp(1544211242351) as date);
+-------------+--+
| _c0 |
+-------------+--+
| 2018-12-08 |
+-------------+--+
日期轉換為字串
> select cast(date('2018-12-08') as string);
+-------------+--+
| _c0 |
+-------------+--+
| 2018-12-08 |
+-------------+--+
日期函式
UNIX時間戳轉日期函式: from_unixtime
語法: from_unixtime(bigint unixtime[, string format])
返回值: string
說明: 轉化UNIX時間戳(從1970-01-01 00:00:00 UTC到指定時間的秒數)到當前時區的時間格式
> select from_unixtime(unix_timestamp(), 'yyyyMMdd');
+-------------+--+
| _c0 |
+-------------+--+
| 20181208 |
+-------------+--+
獲取當前UNIX時間戳函式: unix_timestamp
語法: unix_timestamp()
返回值: bigint
說明: 獲得當前時區的UNIX時間戳
> select unix_timestamp();
+---------------+--+
| _c0 |
+---------------+--+
| 1544283929 |
+---------------+--+
日期轉UNIX時間戳函式: unix_timestamp
語法: unix_timestamp(string date)
返回值: bigint
說明: 轉換格式為"yyyy-MM-dd HH:mm:ss"的日期到UNIX時間戳。如果轉化失敗,則返回0。
> select unix_timestamp('2018-12-08 23:50:00');
+-------------+--+
| _c0 |
+-------------+--+
| 1544284200 |
+-------------+--+
指定格式日期轉UNIX時間戳函式: unix_timestamp
語法: unix_timestamp(string date, string pattern)
返回值: bigint
說明: 轉換pattern格式的日期到UNIX時間戳。如果轉化失敗,則返回0。
> select unix_timestamp('20181208 23:51:03','yyyyMMdd HH:mm:ss');
+-------------+--+
| _c0 |
+-------------+--+
| 1544284263 |
+-------------+--+
日期時間轉日期函式: to_date
語法: to_date(string timestamp)
返回值: string
說明: 返回日期時間欄位中的日期部分。
> select to_date('2018-12-08 23:59:00');
+-------------+--+
| _c0 |
+-------------+--+
| 2018-12-08 |
+-------------+--+
日期轉年函式: year
語法: year(string date)
返回值: int
說明: 返回日期中的年。
> select year('2018-12-08 23:59:00');
+-------+--+
| _c0 |
+-------+--+
| 2018 |
+-------+--+
日期轉月函式: month
語法: month (string date)
返回值: int
說明: 返回日期中的月份。
> select month('2018-12-08 23:59:00');
+------+--+
| _c0 |
+------+--+
| 12 |
+------+--+
日期轉天函式: day
語法: day (string date)
返回值: int
說明: 返回日期中的天。
> select day('2018-12-08 23:59:00');
+------+--+
| _c0 |
+------+--+
| 8 |
+------+--+
日期轉小時函式: hour
語法: hour (string date)
返回值: int
說明: 返回日期中的小時。
> select hour('2018-12-08 23:59:00');
+------+--+
| _c0 |
+------+--+
| 23 |
+------+--+
日期轉分鐘函式: minute
語法: minute (string date)
返回值: int
說明: 返回日期中的分鐘。
> select minute('2018-12-08 23:59:00');
+------+--+
| _c0 |
+------+--+
| 59 |
+------+--+
日期轉秒函式: second
語法: second (string date)
返回值: int
說明: 返回日期中的秒。
> select second('2018-12-08 23:59:00');
+------+--+
| _c0 |
+------+--+
| 0 |
+------+--+
日期轉周函式: weekofyear
語法: weekofyear (string date)
返回值: int
說明: 返回日期在當前的週數。
> select weekofyear('2018-12-08 23:59:00');
+------+--+
| _c0 |
+------+--+
| 49 |
+------+--+
日期比較函式: datediff
語法: datediff(string enddate, string startdate)
返回值: int
說明: 返回結束日期減去開始日期的天數。
> select datediff('2018-12-08', '2018-12-01');
+------+--+
| _c0 |
+------+--+
| 7 |
+------+--+
日期增加函式: date_add
語法: date_add(string startdate, int days)
返回值: string
說明: 返回開始日期startdate增加days天后的日期。
> select date_add('2018-12-08', 30);
+-------------+--+
| _c0 |
+-------------+--+
| 2019-01-07 |
+-------------+--+
日期減少函式: date_sub
語法: date_sub (string startdate, int days)
返回值: string
說明: 返回開始日期startdate減少days天后的日期。
> select date_sub('2018-12-08', 30);
+-------------+--+
| _c0 |
+-------------+--+
| 2018-11-08 |
+-------------+--+
條件函式
If函式: if
語法: if(boolean testCondition, T valueTrue, T valueFalseOrNull)
返回值: T
說明: 當條件testCondition為TRUE時,返回valueTrue;否則返回valueFalseOrNull
> select if(2 = 2, 63, 69);
+------+--+
| _c0 |
+------+--+
| 63 |
+------+--+
> select if(2 = 1, 63, 69);
+------+--+
| _c0 |
+------+--+
| 69 |
+------+--+
非空查詢函式: COALESCE
語法: COALESCE(T v1, T v2, …)
返回值: T
說明: 返回引數中的第一個非空值;如果所有值都為NULL,那麼返回NULL
> select coalesce(null, 63, 69);
+------+--+
| _c0 |
+------+--+
| 63 |
+------+--+
條件判斷函式:CASE
語法: CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END
返回值: T
說明:如果a等於b,那麼返回c;如果a等於d,那麼返回e;否則返回f
> select case 100 when 50 then 'tom' when 100 then 'mary' else 'tim' end;
+-------+--+
| _c0 |
+-------+--+
| mary |
+-------+--+
> select case 200 when 50 then 'tom' when 100 then 'mary' else 'tim' end;
+------+--+
| _c0 |
+------+--+
| tim |
+------+--+
條件判斷函式:CASE
語法: CASE WHEN a THEN b [WHEN c THEN d]* [ELSE e] END
返回值: T
說明:如果a為TRUE,則返回b;如果c為TRUE,則返回d;否則返回e
> select case when 1=2 then 'tom' when 2=2 then 'mary' else 'tim' end;
+-------+--+
| _c0 |
+-------+--+
| mary |
+-------+--+
> select case when 1=1 then 'tom' when 2=2 then 'mary' else 'tim' end ;
+------+--+
| _c0 |
+------+--+
| tom |
+------+--+
字元函式
字串長度函式:length
語法: length(string A)
返回值: int
說明:返回字串A的長度
> select length('fuyun');
+------+--+
| _c0 |
+------+--+
| 5 |
+------+--+
字串反轉函式:reverse
語法: reverse(string A)
返回值: string
說明:返回字串A的反轉結果
> select reverse('fuyun');
+--------+--+
| _c0 |
+--------+--+
| nuyuf |
+--------+--+
字串連線函式:concat
語法: concat(string A, string B…)
返回值: string
說明:返回輸入字串連線後的結果,支援任意個輸入字串
> select concat('fu', 'yun');
+--------+--+
| _c0 |
+--------+--+
| fuyun |
+--------+--+
> select concat('my', 'name', 'is', 'fuyun');
+----------------+--+
| _c0 |
+----------------+--+
| mynameisfuyun |
+----------------+--+
帶分隔符字串連線函式:concat_ws
語法: concat_ws(string SEP, string A, string B…)
返回值: string
說明:返回輸入字串連線後的結果,SEP表示各個字串間的分隔符
> select concat_ws(' ', 'my', 'name', 'is', 'fuyun');
+-------------------+--+
| _c0 |
+-------------------+--+
| my name is fuyun |
+-------------------+--+
字串擷取函式:substr,substring
語法: substr(string A, int start),substring(string A, int start)
返回值: string
說明:返回字串A從start位置到結尾的字串
> select substr('abcdefg', 5);
+------+--+
| _c0 |
+------+--+
| efg |
+------+--+
> select substring('abcdefg', 5);
+------+--+
| _c0 |
+------+--+
| efg |
+------+--+
> select substring('abcdefg', -1);
+------+--+
| _c0 |
+------+--+
| g |
+------+--+
字串擷取函式:substr,substring
語法: substr(string A, int start, int len),substring(string A, int start, int len)
返回值: string
說明:返回字串A從start位置開始,長度為len的字串
> select substr('abcde',3,2);
+------+--+
| _c0 |
+------+--+
| cd |
+------+--+
> select substring('abcde',3,2);
+------+--+
| _c0 |
+------+--+
| cd |
+------+--+
> select substring('abcde',-2,2);
+------+--+
| _c0 |
+------+--+
| de |
+------+--+
字串轉大寫函式:upper,ucase
語法: upper(string A) ucase(string A)
返回值: string
說明:返回字串A的大寫格式
> select upper('abSEd');
+--------+--+
| _c0 |
+--------+--+
| ABSED |
+--------+--+
> select ucase('abSEd');
+--------+--+
| _c0 |
+--------+--+
| ABSED |
+--------+--+
8、字串轉小寫函式:lower,lcase
語法: lower(string A) lcase(string A)
返回值: string
說明:返回字串A的小寫格式
> select lower('abSEd');
+--------+--+
| _c0 |
+--------+--+
| absed |
+--------+--+
> select lcase('abSEd');
+--------+--+
| _c0 |
+--------+--+
| absed |
+--------+--+
去空格函式:trim
語法: trim(string A)
返回值: string
說明:去除字串兩邊的空格
> select trim(' abc ');
+------+--+
| _c0 |
+------+--+
| abc |
+------+--+
左邊去空格函式:ltrim
語法: ltrim(string A)
返回值: string
說明:去除字串左邊的空格
> select ltrim(' abc ');
+-------+--+
| _c0 |
+-------+--+
| abc |
+-------+--+
右邊去空格函式:rtrim
語法: rtrim(string A)
返回值: string
說明:去除字串右邊的空格
> select rtrim(' abc ');
+-------+--+
| _c0 |
+-------+--+
| abc |
+-------+--+
正則表示式替換函式:regexp_replace
語法: regexp_replace(string A, string B, string C)
返回值: string
說明:將字串A中的符合java正則表示式B的部分替換為C。注意,在有些情況下要使用轉義字元,類似oracle中的regexp_replace函式。
> select regexp_replace('fuyun', 'u|n', '');
+------+--+
| _c0 |
+------+--+
| fy |
+------+--+
正則表示式解析函式:regexp_extract
語法: regexp_extract(string subject, string pattern, int index)
返回值: string
說明:將字串subject按照pattern正則表示式的規則拆分,返回index指定的字元。
> select regexp_extract('foothebar', 'foo(.*?)(bar)', 1);
+------+--+
| _c0 |
+------+--+
| the |
+------+--+
> select regexp_extract('foothebar', 'foo(.*?)(bar)', 2);
+------+--+
| _c0 |
+------+--+
| bar |
+------+--+
> select regexp_extract('foothebar', 'foo(.*?)(bar)', 0);
+------------+--+
| _c0 |
+------------+--+
| foothebar |
+------------+--+
注意,在有些情況下要使用轉義字元,下面的等號要用雙豎線轉義,這是java正則表示式的規則。
> select regexp_extract('isStartDate=2018-12-08', '.*?StartDate\\=([^&]+)',1);
+-------------+--+
| _c0 |
+-------------+--+
| 2018-12-08 |
+-------------+--+
URL解析函式:parse_url
語法: parse_url(string urlString, string partToExtract [, string keyToExtract])
返回值: string
說明:返回URL中指定的部分。partToExtract的有效值為:HOST, PATH, QUERY, REF, PROTOCOL, AUTHORITY, FILE, and USERINFO.
> select parse_url('https://me.csdn.net/lz6363', 'HOST');
+--------------+--+
| _c0 |
+--------------+--+
| me.csdn.net |
+--------------+--+
> select parse_url('https://me.csdn.net/lz6363/?k1=v1&k2=v2#Ref1', 'QUERY', 'k1');
+------+--+
| _c0 |
+------+--+
| v1 |
+------+--+
json解析函式:get_json_object
語法: get_json_object(string json_string, string path)
返回值: string
說明:解析json的字串json_string,返回path指定的內容。如果輸入的json字串無效,那麼返回NULL。
hive> select get_json_object('{"store":
{"fruit":\[{"weight":8,"type":"apple"},{"weight":9,"type":"pear"}],
"bicycle":{"price":19.95,"color":"red"}
},
"email":"[email protected]_for_json_udf_test.net",
"owner":"amy"
}
','$.owner')
+------+--+
| _c0 |
+------+--+
| amy |
+------+--+
空格字串函式:space
語法: space(int n)
返回值: string
說明:返回長度為n的字串
> select space(10) ;
+-------------+--+
| _c0 |
+-------------+--+
| |
+-------------+--+
> select length(space(10));
+------+--+
| _c0 |
+------+--+
| 10 |
+------+--+
重複字串函式:repeat
語法: repeat(string str, int n)
返回值: string
說明:返回重複n次後的str字串
> select repeat('abc',5);
+------------------+--+
| _c0 |
+------------------+--+
| abcabcabcabcabc |
+------------------+--+
首字元ascii函式:ascii
語法: ascii(string str)
返回值: int
說明:返回字串str第一個字元的ascii碼
> select ascii('abcde');
+------+--+
| _c0 |
+------+--+
| 97 |
+------+--+
左補足函式:lpad
語法: lpad(string str, int len, string pad)
返回值: string
說明:將str進行用pad進行左補足到len位
> select lpad('abc',10,'td');
+-------------+--+
| _c0 |
+-------------+--+
| tdtdtdtabc |
+-------------+--+
注意:與GP,ORACLE不同,pad 不能預設
右補足函式:rpad
語法: rpad(string str, int len, string pad)
返回值: string
說明:將str進行用pad進行右補足到len位
> select rpad('abc',10,'td');
+-------------+--+
| _c0 |
+-------------+--+
| abctdtdtdt |
+-------------+--+
分割字串函式: split
語法: split(string str, string pat)
返回值: array
說明: 按照pat字串分割str,會返回分割後的字串陣列
> select split('abtcdtef','t');
+-------------------+--+
| _c0 |
+-------------------+--+
| ["ab","cd","ef"] |
+-------------------+--+
集合查詢函式: find_in_set
語法: find_in_set(string str, string strList)
返回值: int
說明: 返回str在strlist第一次出現的位置,strlist是用逗號分割的字串。如果沒有找該str字元,則返回0
> select find_in_set('ab','ef,ab,de');
+------+--+
| _c0 |
+------+--+
| 2 |
+------+--+
> select find_in_set('at','ef,ab,de');
+------+--+
| _c0 |
+------+--+
| 0 |
+------+--+
聚合函式
個數統計函式: count
語法: count(), count(expr), count(DISTINCT expr[, expr_.])
返回值: int
說明: count()統計檢索出的行的個數,包括NULL值的行;count(expr)返回指定欄位的非空值的個數;count(DISTINCT expr[, expr_.])返回指定欄位的不同的非空值的個數
總和統計函式: sum
語法: sum(col), sum(DISTINCT col)
返回值: double
說明: sum(col)統計結果集中col的相加的結果;sum(DISTINCT col)統計結果中col不同值相加的結果
平均值統計函式: avg
語法: avg(col), avg(DISTINCT col)
返回值: double
說明: avg(col)統計結果集中col的平均值;avg(DISTINCT col)統計結果中col不同值相加的平均值
最小值統計函式: min
語法: min(col)
返回值: double
說明: 統計結果集中col欄位的最小值
最大值統計函式: max
語法: maxcol)
返回值: double
說明: 統計結果集中col欄位的最大值
非空集合總體變數函式: var_pop
語法: var_pop(col)
返回值: double
說明: 統計結果集中col非空集合的總體變數(忽略null)
非空集合樣本變數函式: var_samp
語法: var_samp (col)
返回值: double
說明: 統計結果集中col非空集合的樣本變數(忽略null)
總體標準偏離函式: stddev_pop
語法: stddev_pop(col)
返回值: double
說明: 該函式計算總體標準偏離,並返回總體變數的平方根,其返回值與VAR_POP函式的平方根相同
樣本標準偏離函式: stddev_samp
語法: stddev_samp (col)
返回值: double
說明: 該函式計算樣本標準偏離
中位數函式: percentile
語法: percentile(BIGINT col, p)
返回值: double
說明: 求準確的第pth個百分位數,p必須介於0和1之間,但是col欄位目前只支援整數,不支援浮點數型別
中位數函式: percentile
語法: percentile(BIGINT col, array(p1 [, p2]…))
返回值: array
說明: 功能和上述類似,之後後面可以輸入多個百分位數,返回型別也為array,其中為對應的百分位數。
近似中位數函式: percentile_approx
語法: percentile_approx(DOUBLE col, p [, B])
返回值: double
說明: 求近似的第pth個百分位數,p必須介於0和1之間,返回型別為double,但是col欄位支援浮點型別。引數B控制記憶體消耗的近似精度,B越大,結果的準確度越高。預設為10,000。當col欄位中的distinct值的個數小於B時,結果為準確的百分位數
近似中位數函式: percentile_approx
語法: percentile_approx(DOUBLE col, array(p1 [, p2]…) [, B])
返回值: array
說明: 功能和上述類似,之後後面可以輸入多個百分位數,返回型別也為array,其中為對應的百分位數。
直方圖: histogram_numeric
語法: histogram_numeric(col, b)
返回值: array<struct {‘x’,‘y’}>
說明: 以b為基準計算col的直方圖資訊。
複合型別構建操作
Map型別構建: map
語法: map (key1, value1, key2, value2, …)
說明:根據輸入的key和value對構建map型別
> create table test2 as select map('100','tom','200','mary') as t;
> > desc test2;
+-----------+---------------------+----------+--+
| col_name | data_type | comment |
+-----------+---------------------+----------+--+
| t | map<string,string> | |
+-----------+---------------------+----------+--+
> select t from test2;
+-----------------------------+--+
| t |
+-----------------------------+--+
| {"100":"tom","200":"mary"} |
+-----------------------------+--+
Struct型別構建: struct
語法: struct(val1, val2, val3, …)
說明:根據輸入的引數構建結構體struct型別
> create table test1 as select struct('tom','mary','tim') as t;
> desc test1;
+-----------+----------------------------------------------+----------+--+
| col_name | data_type | comment |
+-----------+----------------------------------------------+----------+--+
| t | struct<col1:string,col2:string,col3:string> | |
+-----------+----------------------------------------------+----------+--+
> select t from test1;
+--------------------------------------------+--+
| t |
+--------------------------------------------+--+
| {"col1":"tom","col2":"mary","col3":"tim"} |
+--------------------------------------------+--+
array型別構建: array
語法: array(val1, val2, …)
說明:根據輸入的引數構建陣列array型別
> create table test as select array("tom","mary","tim") as t;
> desc test;
+-----------+----------------+----------+--+
| col_name | data_type | comment |
+-----------+----------------+----------+--+
| t | array<string> | |
+-----------+----------------+----------+--+
> select t from test;
+-----------------------+--+
| t |
+-----------------------+--+
| ["tom","mary","tim"] |
+-----------------------+--+
複雜型別訪問操作
array型別訪問: A[n]
語法: A[n]
操作型別: A為array型別,n為int型別
說明:返回陣列A中的第n個變數值。陣列的起始下標為0。比如,A是個值為[‘foo’, ‘bar’]的陣列型別,那麼A[0]將返回’foo’,而A[1]將返回’bar’
> create table test as select array("tom","mary","tim") as t;
> select t[0],t[1],t[2] from test;
+------+-------+------+--+
| _c0 | _c1 | _c2 |
+------+-------+------+--+
| tom | mary | tim |
+------+-------+------+--+
map型別訪問: M[key]
語法: M[key]
操作型別: M為map型別,key為map中的key值
說明:返回map型別M中,key值為指定值的value值。比如,M是值為{‘f’ -> ‘foo’, ‘b’ -> ‘bar’, ‘all’ -> ‘foobar’}的map型別,那麼M[‘all’]將會返回’foobar’