hive函數的分類
hive的客戶端
顯示當前會話有多少函數可用
SHOW FUNCTIONS;
如:

OK
!
!=
%
DESC FUNCTION concat;
OK
concat(str1, str2, ... strN) - returns the concatenation of str1, str2, ... strN or concat(bin1, bin2, ... binN) - returns the concatenation of bytes in binary data bin1, bin2, ... binN
Time taken: 0.005 seconds, Fetched: 1 row(s)
DESC FUNCTION EXTENDED concat;
一.簡單函數
1.數學函數
返回對a四舍五入的BIGINT值
1 返回值: 2 hive> select round(2.5); 3 OK 4 3.0 5 Time taken: 0.093 seconds, Fetched: 1 row(s)
返回DOUBLE型d的保留n位小數的DOUBLW型的近似值 round(DOUBLE a, INT d)
1 返回值:double 2 hive> select round(0.5002,2); 3 OK 4 0.5 5 Time taken: 0.074 seconds, Fetched: 1 row(s)
銀行家舍入法(1~4:舍,6~9:進,5->前位數是偶:舍,5->前位數是奇:進) bround(DOUBLE a)
返回值:double bround(2.5) = 2, bround(3.5) = 4.
銀行家舍入法,保留d位小數 bround(DOUBLE a, INT d)
1 返回值:double 2 bround(8.25, 1) = 8.2, bround(8.35, 1) = 8.4
向下取整,最數軸上最接近要求的值的左邊的值 如:6.10->6 -3.4->-4 floor(DOUBLE a)
1 返回值:double 2 hive> select floor(6.10); 3 OK 4 6 5 Time taken: 0.07 seconds, Fetched: 1 row(s) 6 hive> select floor(-3.4); 7 OK 8 -4 9 Time taken: 0.104 seconds, Fetched: 1 row(s)
求其不小於小給定實數的最小整數如:ceil(6) = ceil(6.1)= ceil(6.9) = 6 ceil(DOUBLE a), ceiling(DOUBLE a)
1 返回值:BIGINT 2 hive> select ceil(6); 3 OK 4 6 5 Time taken: 0.2 seconds, Fetched: 1 row(s) 6 hive> select ceil(6.1); 7 OK 8 7 9 Time taken: 0.061 seconds, Fetched: 1 row(s) 10 hive> select ceil(6.9); 11 OK 12 7 13 Time taken: 0.153 seconds, Fetched: 1 row(s)
每行返回一個DOUBLE型隨機數seed是隨機因子 rand(), rand(INT seed)
1 返回值:DOUBLE 2 hive> select rand(2); 3 OK 4 0.7311469360199058 5 Time taken: 0.068 seconds, Fetched: 1 row(s) 6 hive> select rand(); 7 OK 8 0.7859071491095923 9 Time taken: 0.064 seconds, Fetched: 1 row(s)
返回e的a冪次方, a可為小數 exp(DOUBLE a), exp(DECIMAL a)
1 返回值:double 2 hive> select exp(2); 3 OK 4 7.38905609893065 5 Time taken: 0.1 seconds, Fetched: 1 row(s)
以自然數為底d的對數,a可為小數 ln(DOUBLE a), ln(DECIMAL a)
1 返回值:double 2 ln(DOUBLE a), ln(DECIMAL a) 3 > select ln(3); 4 OK 5 1.0986122886681098 6 Time taken: 0.081 seconds, Fetched: 1 row(s) 7 hive> select ln(3.2); 8 OK 9 1.1631508098056809 10 Time taken: 0.067 seconds, Fetched: 1 row(s)
以10為底d的對數,a可為小數 log10(DOUBLE a), log10(DECIMAL a)
1 返回值:double 2 hive> select log10(3.2); 3 OK 4 0.505149978319906 5 Time taken: 0.084 seconds, Fetched: 1 row(s) 6 hive> select log10(3); 7 OK 8 0.47712125471966244 9 Time taken: 0.075 seconds, Fetched: 1 row(s)
以2為底數d的對數,a可為小數 log2(DOUBLE a), log2(DECIMAL a)
1 返回值:double 2 hive> 3 > select log2(3); 4 OK 5 1.5849625007211563 6 Time taken: 0.083 seconds, Fetched: 1 row(s) 7 hive> select log2(3.2); 8 OK 9 1.6780719051126378 10 Time taken: 0.07 seconds, Fetched: 1 row(s)
以base為底的對數,base 與 a都是DOUBLE類型
log(DOUBLE base, DOUBLE a)
log(DECIMAL base, DECIMAL a)
1 返回值:double 2 hive> select log(2,3.2); 3 OK 4 1.6780719051126378 5 Time taken: 0.084 seconds, Fetched: 1 row(s) 6 hive> select log(2,3); 7 OK 8 1.5849625007211563 9 Time taken: 0.066 seconds, Fetched: 1 row(s)
計算a的p次冪 pow(DOUBLE a, DOUBLE p), power(DOUBLE a, DOUBLE p)
1 返回值:double 2 hive> select pow(2,4); 3 OK 4 16.0 5 Time taken: 0.065 seconds, Fetched: 1 row(s)
計算a的平方根 sqrt(DOUBLE a), sqrt(DECIMAL a)
1 返回值:double 2 select sqrt(2);
計算二進制a的STRING類型,a為BIGINT類型 bin(BIGINT a)
返回值:string hive> select bin(2); OK 10 Time taken: 0.194 seconds, Fetched: 1 row(s)
計算十六進制a的STRING類型,如果a為STRING類型就轉換成字符相對應的十六進制 hex(BIGINT a) hex(STRING a) hex(BINARY a)
1 返回值:STRING 2 hive> select hex(2); 3 OK 4 2 5 Time taken: 0.097 seconds, Fetched: 1 row(s)
hex的逆方法
unhex(STRING a)
1 返回值:BINARY 2 hive> select unhex(2); 3 OK 4 5 Time taken: 0.077 seconds, Fetched: 1 row(s)
將GIGINT/STRING類型的num從from_base進制轉換成to_base進制 conv(BIGINT num, INT from_base, INT to_base), conv(STRING num, INT from_base, INT to_base)
1 返回值:STRING 2 hive> select conv(2,10,2); 3 OK 4 10 5 Time taken: 0.075 seconds, Fetched: 1 row(s)
計算a的絕對值 abs(DOUBLE a)
1 返回值:DOUBLE 2 hive> select abs(-2); 3 OK 4 2 5 Time taken: 0.077 seconds, Fetched: 1 row(s)
a對b取模 pmod(INT a, INT b), pmod(DOUBLE a, DOUBLE b)
1 返回值:double 2 hive> select pmod(4,2); 3 OK 4 0 5 Time taken: 0.077 seconds, Fetched: 1 row(s)
求a的正弦值 sin(DOUBLE a), sin(DECIMAL a)
1 返回值:double 2 hive> select sin(2.5); 3 OK 4 0.5984721441039564 5 Time taken: 0.092 seconds, Fetched: 1 row(s)
求d的反正弦值 asin(DOUBLE a), asin(DECIMAL a)
1 返回值:double 2 hive> select asin(2.5); 3 OK 4 NaN 5 Time taken: 0.097 seconds, Fetched: 1 row(s)
求余弦值 cos(DOUBLE a), cos(DECIMAL a)
1 返回值:double 2 hive> select cos(2.5); 3 OK 4 -0.8011436155469337 5 Time taken: 0.087 seconds, Fetched: 1 row(s)
求反余弦值 acos(DOUBLE a), acos(DECIMAL a)
1 返回值:double 2 hive> select acos(2.5); 3 OK 4 NaN 5 Time taken: 0.091 seconds, Fetched: 1 row(s)
求正切值 tan(DOUBLE a), tan(DECIMAL a)
1 返回值:double 2 hive> select tan(2.5); 3 OK 4 -0.7470222972386603 5 Time taken: 0.076 seconds, Fetched: 1 row(s)
求反正切值 atan(DOUBLE a), atan(DECIMAL a)
1 返回值:double 2 hive> select atan(2.5); 3 OK 4 1.1902899496825317 5 Time taken: 0.074 seconds, Fetched: 1 row(s)
獎弧度值轉換角度值 degrees(DOUBLE a), degrees(DECIMAL a)
1 返回值:DOUBLE 2 hive> select degrees(30); 3 OK 4 1718.8733853924698 5 Time taken: 0.114 seconds, Fetched: 1 row(s)
將角度值轉換成弧度值 radians(DOUBLE a), radians(DOUBLE a)
1 返回值:double 2 hive> select radians(30); 3 OK 4 0.5235987755982988 5 Time taken: 0.093 seconds, Fetched: 1 row(s)
返回a positive(INT a), positive(DOUBLE a)
1 返回值:INT or DOUBLE 2 hive> select positive(2); 3 OK 4 2 5 Time taken: 0.124 seconds, Fetched: 1 row(s)
返回a的相反數 negative(INT a), negative(DOUBLE a)
1 返回值:double 2 hive> select negative(2); 3 OK 4 -2 5 Time taken: 0.066 seconds, Fetched: 1 row(s)
如果a是正數則返回1.0,是負數則返回-1.0,否則返回0.0 sign(DOUBLE a), sign(DECIMAL a)
1 返回值:DOUBLE or INT 2 hive> select sign(2); 3 OK 4 1.0 5 Time taken: 0.091 seconds, Fetched: 1 row(s)
數學常數e e()
1 返回值:double 2 hive> select e(); 3 OK 4 2.718281828459045 5 Time taken: 0.07 seconds, Fetched: 1 row(s)
數學常數pi pi()
1 返回值:double 2 hive> select pi(); 3 OK 4 3.141592653589793 5 Time taken: 0.082 seconds, Fetched: 1 row(s)
求a的階乘 factorial(INT a)
1 返回值:BIGINT 2 select factorial(2);
求a的立方根 cbrt(DOUBLE a)
1 返回值:DOUBLE 2 select cbrt(2);
按位左移
shiftleft(TINYINT|SMALLINT|INT a, INT b)
shiftleft(BIGINT a, INT b)
1 返回值:int bigint 2 hive> select shiftleft(2,3);
按拉右移
shiftright(TINYINT|SMALLINT|INT a, INTb)
shiftright(BIGINT a, INT b)
1 返回值:INT BIGINT 2 hive> select shiftrigth(2,3);
無符號按位右移(<<<)
shiftrightunsigned(TINYINT|SMALLINT|INTa, INT b),
shiftrightunsigned(BIGINT a, INT b)
1 返回值:INT BIGINT 2 select shiftrightunsigned(2,3);
求最大值 greatest(T v1, T v2, ...)
1 返回值:T 2 hive> select greatest(2,3,6,7); 3 OK 4 7 5 Time taken: 0.072 seconds, Fetched: 1 row(s)
求最小值 least(T v1, T v2, ...)
1 返回值:double 2 hive> select least(2,3,6,7); 3 OK 4 2 5 Time taken: 0.079 seconds, Fetched: 1 row(s)
2.類型轉換函數
將輸入的值轉換成二進制 binary(string|binary)
1 返回值:binary 2 hive> select binary('4'); 3 OK 4 4 5 Time taken: 0.08 seconds, Fetched: 1 row(s)
將expr轉換成type類型 如:cast("1" as BIGINT) 將字符串1轉換成了BIGINT類型,如果轉換失敗將返回NULL cast(expr as <type>)
1 返回值:Expected "=" to follow "type" 2 hive> select cast("1" as BIGINT) ; 3 OK 4 1 5 Time taken: 0.266 seconds, Fetched: 1 row(s)
3.日期函數
日期函數UNIX時間戳轉日期函數: from_unixtime語法: from_unixtime(bigint unixtime[, string format])
1 返回值: string 2 說明: 轉化UNIX時間戳(從1970-01-01 00:00:00 UTC到指定時間的秒數)到當前時區的時間格式 3 舉例: 4 hive> select from_unixtime(1323308943,'yyyyMMdd'); 5 OK 6 20111208 7 Time taken: 0.152 seconds, Fetched: 1 row(s)
獲取當前UNIX時間戳函數: unix_timestamp語法: unix_timestamp()
1 返回值: bigint 2 說明: 獲得當前時區的UNIX時間戳 3 舉例: 4 Time taken: 0.152 seconds, Fetched: 1 row(s) 5 hive> select unix_timestamp(); 6 OK 7 1487931871 8 Time taken: 0.106 seconds, Fetched: 1 row(s)
日期轉UNIX時間戳函數: unix_timestamp語法: unix_timestamp(string date)
1 返回值: bigint 2 說明: 轉換格式為“yyyy-MM-dd HH:mm:ss“的日期到UNIX時間戳。如果轉化失敗,則返回0。 3 舉例: 4 hive> select unix_timestamp('2011-12-07 13:01:03'); 5 OK 6 1323234063 7 Time taken: 0.083 seconds, Fetched: 1 row(s)
指定格式日期轉UNIX時間戳函數: unix_timestamp語法: unix_timestamp(string date, string pattern)
1 返回值: bigint 2 說明: 轉換pattern格式的日期到UNIX時間戳。如果轉化失敗,則返回0。 3 舉例: 4 hive> select unix_timestamp('20111207 13:01:03','yyyyMMdd HH:mm:ss'); 5 OK 6 1323234063 7 Time taken: 0.079 seconds, Fetched: 1 row(s)
日期時間轉日期函數: to_date語法: to_date(string timestamp)
1 返回值: string 2 說明: 返回日期時間字段中的日期部分。 3 舉例: 4 hive> select to_date('2011-12-08 10:03:01') ; 5 OK 6 2011-12-08 7 Time taken: 0.194 seconds, Fetched: 1 row(s)
日期轉年函數: year語法: year(string date)
1 返回值: int 2 說明: 返回日期中的年。 3 舉例: 4 hive> select year('2011-12-08 10:03:01'); 5 OK 6 2011 7 Time taken: 0.168 seconds, Fetched: 1 row(s)
日期轉月函數: month語法: month (string date)
1 返回值: int 2 說明: 返回日期中的月份。 3 舉例: 4 hive> select month('2011-12-08 10:03:01'); 5 OK 6 12 7 Time taken: 0.084 seconds, Fetched: 1 row(s)
1 hive> select month('2011-08-08'); 2 OK 3 8 4 Time taken: 0.095 seconds, Fetched: 1 row(s)
日期轉天函數: day語法: day (string date)
1 返回值: int 2 說明: 返回日期中的天。 3 舉例: 4 hive> select day('2011-12-08 10:03:01'); 5 OK 6 8 7 Time taken: 0.115 seconds, Fetched: 1 row(s)
1 hive> select day('2011-12-24'); 2 OK 3 24 4 Time taken: 0.294 seconds, Fetched: 1 row(s)
日期轉小時函數: hour語法: hour (string date)
1 返回值: int 2 說明: 返回日期中的小時。 3 舉例: 4 hive> select hour('2011-12-08 10:03:01'); 5 OK 6 10 7 Time taken: 0.082 seconds, Fetched: 1 row(s)
日期轉分鍾函數: minute語法: minute (string date)
1 返回值: int 2 說明: 返回日期中的分鍾。 3 舉例: 4 hive> select minute('2011-12-08 10:03:01'); 5 OK 6 3 7 Time taken: 0.181 seconds, Fetched: 1 row(s)
日期轉秒函數: second語法: second (string date)
1 返回值: int 2 說明: 返回日期中的秒。 3 舉例: 4 hive> select second('2011-12-08 10:03:01'); 5 OK 6 1 7 Time taken: 0.693 seconds, Fetched: 1 row(s)
日期轉周函數: weekofyear語法: weekofyear (string date)
1 返回值: int 2 說明: 返回日期在當前的周數。 3 舉例: 4 hive> select weekofyear('2011-12-08 10:03:01') 5 > ; 6 OK 7 49 8 Time taken: 0.119 seconds, Fetched: 1 row(s)
日期比較函數: datediff語法: datediff(string enddate, string startdate)
1 返回值: int 2 說明: 返回結束日期減去開始日期的天數。 3 舉例: 4 hive> select datediff('2012-12-08','2012-05-09'); 5 OK 6 213 7 Time taken: 0.082 seconds, Fetched: 1 row(s)
日期增加函數: date_add語法: date_add(string startdate, int days)
1 返回值: string 2 說明: 返回開始日期startdate增加days天后的日期。 3 舉例: 4 hive> select date_add('2012-12-08',10); 5 OK 6 2012-12-18 7 Time taken: 0.201 seconds, Fetched: 1 row(s)
日期減少函數: date_sub語法: date_sub (string startdate, int days)
1 返回值: string 2 說明: 返回開始日期startdate減少days天后的日期。 3 舉例: 4 hive> select date_sub('2012-12-08',10); 5 OK 6 2012-11-28 7 Time taken: 0.125 seconds, Fetched: 1 row(s)
返回這個月的最后一天的日期,忽略時分秒部分(HH:mm:ss) last_day(string date)
1 返回值:string 2 hive> select last_day('2017-02-17 08:34:23'); 3 OK 4 2017-02-28 5 Time taken: 0.082 seconds, Fetched: 1 row(s)
返回當前時間的下一個星期X所對應的日期 如:next_day('2015-01-14', 'TU') = 2015-01-20 以2015-01-14為開始時間,其下一個星期二所對應的日期為2015-01-20
1 返回值:string 2 hive> select next_day('2015-01-14', 'TU') ; 3 OK 4 2015-01-20 5 Time taken: 0.319 seconds, Fetched: 1 row(s)
返回當前時間屬性哪個季度 如quarter('2015-04-08') = 2
1 返回值:int 2 quarter(date/timestamp/string)
返回當前時間日期
返回值:date hive> select current_date; OK 2017-02-25 Time taken: 0.087 seconds, Fetched: 1 row(s)
如果給定的時間戳並非UTC,則將其轉化成指定的時區下時間戳
1 返回值:timestamp 2 from_utc_timestamp(timestamp, string timezone) 3 hive> select from_utc_timestamp('1970-01-01 08:00:00','PST'); 4 OK 5 1970-01-01 00:00:00 6 Time taken: 0.122 seconds, Fetched: 1 row(s)
如果給定的時間戳指定的時區下時間戳,則將其轉化成UTC下的時間戳 to_utc_timestamp(timestamp, string timezone)
1 返回值:timestamp 2 hive> select to_utc_timestamp('1970-01-01 00:00:00','PST'); 3 OK 4 1970-01-01 08:00:00 5 Time taken: 0.099 seconds, Fetched: 1 row(s)
返回當前時間戳
1 返回值:timestamp 2 hive> select current_timestamp; 3 OK 4 2017-02-25 00:28:46.724 5 Time taken: 0.069 seconds, Fetched: 1 row(s)
返回當前時間下再增加num_months個月的日期 add_months(string start_date, int num_months)
1 返回值:string 2 hive> select add_months('2017-02-10', 2); 3 OK 4 2017-04-10 5 Time taken: 0.061 seconds, Fetched: 1 row(s)
5.條件函數
如果testCondition 為true就返回valueTrue,否則返回valueFalseOrNull ,(valueTrue,valueFalseOrNull為泛型)
if(boolean testCondition, T valueTrue, T valueFalseOrNull)
1 返回值:T
如果value值為NULL就返回default_value,否則返回value
nvl(T value, T default_value)
1 返回值:T
返回第一非null的值,如果全部都為NULL就返回NULL 如:COALESCE (NULL,44,55)=44/strong>
COALESCE(T v1, T v2, ...)
1 返回值:T
如果a=b就返回c,a=d就返回e,否則返回f 如CASE 4 WHEN 5 THEN 5 WHEN 4 THEN 4 ELSE 3 END 將返回4
CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END
1 返回值:T
如果a=ture就返回b,c= ture就返回d,否則返回e 如:CASE WHEN 5>0 THEN 5 WHEN 4>0 THEN 4 ELSE 0 END 將返回5;CASE WHEN 5<0 THEN 5 WHEN 4<0 THEN 4 ELSE 0 END 將返回0
CASE WHEN a THEN b [WHEN c THEN d]* [ELSE e] END
1 返回值:T
如果a為null就返回true,否則返回false
isnull( a )
1 返回值:boolean
如果a為非null就返回true,否則返回false
isnotnull ( a )
1 返回值:boolean
6.字符函數
返回str中首個ASCII字符串的整數值
ascii(string str)
1 返回值:int
將二進制bin轉換成64位的字符串
base64(binary bin)
1 返回值:string
對二進制字節碼或字符串按次序進行拼接
concat(string|binary A, string|binary B...)
1 返回值:string
與ngram類似,但context_ngram()允許你預算指定上下文(數組)來去查找子序列,具體看StatisticsAndDataMining(這里的解釋更易懂)
context_ngrams(array<array<string>>, array<string>, int K, int pf)
1 返回值:array<struct<string,double>>
與concat()類似,但使用指定的分隔符喜進行分隔
concat_ws(string SEP, string A, string B...)
1 返回值:string
拼接Array中的元素並用指定分隔符進行分隔
concat_ws(string SEP, array<string>)
1 返回值:string
使用指定的字符集charset將字符串編碼成二進制值,支持的字符集有:'US-ASCII', 'ISO-8859-1', 'UTF-8', 'UTF-16BE', 'UTF-16LE', 'UTF-16',如果任一輸入參數為NULL都將返回NULL
encode(string src, string charset)
1 返回值:binary
返回以逗號分隔的字符串中str出現的位置,如果參數str為逗號或查找失敗將返回0,如果任一參數為NULL將返回NULL回
find_in_set(string str, string strList)
1 返回值:int
將數值X轉換成"#,###,###.##"格式字符串,並保留d位小數,如果d為0,將進行四舍五入且不保留小數
format_number(number x, int d)
1 返回值:string
從指定路徑上的JSON字符串抽取出JSON對象,並返回這個對象的JSON格式,如果輸入的JSON是非法的將返回NULL,注意此路徑上JSON字符串只能由數字 字母 下划線組成且不能有大寫字母和特殊字符,且key不能由數字開頭,這是由於Hive對列名的限制
get_json_object(string json_string, string path)
1 返回值:string
如果文件名為filename的文件中有一行數據與字符串str匹配成功就返回true
in_file(string str, string filename)
1 返回值:boolean
查找字符串str中子字符串substr出現的位置,如果查找失敗將返回0,如果任一參數為Null將返回null,注意位置為從1開始的
instr(string str, string substr)
1 返回值:int
返回字符串的長度
length(string A)
1 返回值:int
查找字符串str中的pos位置后字符串substr第一次出現的位置
locate(string substr, string str[, int pos])
1 返回值:int
將字符串A的所有字母轉換成小寫字母
lower(string A) lcase(string A)
1 返回值:string
從左邊開始對字符串str使用字符串pad填充,最終len長度為止,如果字符串str本身長度比len大的話,將去掉多余的部分
lpad(string str, int len, string pad)
1 返回值:string
去掉字符串A前面的空格
ltrim(string A)
1 返回值:string
返回出現次數TOP K的的子序列,n表示子序列的長度,具體看StatisticsAndDataMining (這里的解釋更易懂)
ngrams(array<array<string>>, int N, int K, int pf)
1 返回值:array<struct<string,double>>
返回從URL中抽取指定部分的內容,參數url是URL字符串,而參數partToExtract是要抽取的部分,這個參數包含(HOST, PATH, QUERY, REF, PROTOCOL, AUTHORITY, FILE, and USERINFO,例如:parse_url('http://facebook.com/path1/p.php?k1=v1&k2=v2#Ref1', 'HOST') ='facebook.com',如果參數partToExtract值為QUERY則必須指定第三個參數key 如:parse_url('http://facebook.com/path1/p.php?k1=v1&k2=v2#Ref1', 'QUERY', 'k1') =‘v1’
parse_url(string urlString, string partToExtract [, string keyToExtract])
1 返回值:string
按照printf風格格式輸出字符串
printf(String format, Obj... args)
1 返回值:string
抽取字符串subject中符合正則表達式pattern的第index個部分的子字符串,注意些預定義字符的使用,如第二個參數如果使用'\s'將被匹配到s,'\\s'才是匹配空格
regexp_extract(string subject, string pattern, int index)
1 返回值:string
按照Java正則表達式PATTERN將字符串INTIAL_STRING中符合條件的部分成REPLACEMENT所指定的字符串,如里REPLACEMENT這空的話,抽符合正則的部分將被去掉 如:regexp_replace("foobar", "oo|ar", "") = 'fb.' 注意些預定義字符的使用,如第二個參數如果使用'\s'將被匹配到s,'\\s'才是匹配空格
regexp_replace(string INITIAL_STRING, string PATTERN, string REPLACEMENT)
1 返回值:string
重復輸出n次字符串str
repeat(string str, int n)
1 返回值: 2 string
反轉字符串
reverse(string A)
1 返回值:string
從右邊開始對字符串str使用字符串pad填充,最終len長度為止,如果字符串str本身長度比len大的話,將去掉多余的部分
rpad(string str, int len, string pad)
1 返回值:string
去掉字符串后面出現的空格
rtrim(string A)
1 返回值:string
字符串str將被轉換成單詞數組,如:sentences('Hello there! How are you?') =( ("Hello", "there"), ("How", "are", "you") )
sentences(string str, string lang, string locale)
1 返回值:array<array<string>>
返回n個空格
space(int n)
返回值:string
按照正則表達式pat來分割字符串str,並將分割后的數組字符串的形式返回
split(string str, string pat)
1 返回值:string
將字符串str按照指定分隔符轉換成Map,第一個參數是需要轉換字符串,第二個參數是鍵值對之間的分隔符,默認為逗號;第三個參數是鍵值之間的分隔符,默認為"="
str_to_map(text[, delimiter1, delimiter2])
1 返回值:map<string,string>
對於字符串A,從start位置開始截取字符串並返回
substr(string|binary A, int start) substring(string|binary A, int start)
1 返回值:string
對於二進制/字符串A,從start位置開始截取長度為length的字符串並返回
substr(string|binary A, int start, int len) substring(string|binary A, int start, int len)
1 返回值:string
截取第count分隔符之前的字符串,如count為正則從左邊開始截取,如果為負則從右邊開始截取
substring_index(string A, string delim, int count)
1 返回值:string
將input出現在from中的字符串替換成to中的字符串 如:translate("MOBIN","BIN","M")="MOM"
translate(string|char|varchar input, string|char|varchar from, string|char|varchar to)
1 返回值:string
將64位的字符串轉換二進制值
unbase64(string str)
1 返回值:binary
將字符串A中的字母轉換成大寫字母
upper(string A) ucase(string A)
1 返回值:string
將字符串A轉換第一個字母大寫其余字母的字符串
initcap(string A)
1 返回值:string
計算兩個字符串之間的差異大小 如:levenshtein('kitten', 'sitting') = 3
levenshtein(string A, string B)
1 返回值:int
將普通字符串轉換成soundex字符串
soundex(string A)
1 返回值:string
7.字符串函數
分割字符串函數: split
語法: split(string str, string pat)
1 返回值: array 2 說明: 按照pat字符串分割str,會返回分割后的字符串數組 3 舉例: 4 hive> select split('abtcdtef','t'); 5 OK 6 ["ab","cd","ef"] 7 Time taken: 0.118 seconds, Fetched: 1 row(s)
與GP,ORACLE不同,pad 不能默認
右補足函數:rpad
語法: rpad(string str, int len, string pad)
1 返回值: string 2 說明:將str進行用pad進行右補足到len位 3 舉例: 4 hive> 5 > select rpad('abc',10,'td'); 6 OK 7 abctdtdtdt 8 Time taken: 0.077 seconds, Fetched: 1 row(s)
左補足函數:lpad
語法: lpad(string str, int len, string pad)
1 hive> 2 > select rpad('abc',10,'td'); 3 OK 4 abctdtdtdt 5 Time taken: 0.077 seconds, Fetched: 1 row(s)
首字符ascii函數:ascii
語法: ascii(string str)
1 返回值: int 2 說明:返回字符串str第一個字符的ascii碼 3 舉例: hive> select ascii('abcde'); 4 OK 5 97 6 Time taken: 0.066 seconds, Fetched: 1 row(s)
重復字符串函數:repeat
語法: repeat(string str, int n)
1 返回值: string 2 說明:返回重復n次后的str字符串 3 舉例: 4 hive> select repeat('abc',5); 5 OK 6 abcabcabcabcabc 7 Time taken: 0.064 seconds, Fetched: 1 row(s)
空格字符串函數:space
語法: space(int n)
1 返回值: string 2 說明:返回長度為n的字符串 3 舉例:hive> select space(10); 4 OK 5 6 Time taken: 0.101 seconds, Fetched: 1 row(s) 7 hive> select length(space(10)); 8 OK 9 10 10 Time taken: 1.905 seconds, Fetched: 1 row(s)
字符串長度函數:length
1 語法: length(string A) 2 返回值: int 3 說明:返回字符串A的長度 4 舉例:hive> select length('abcedfg'); 5 OK 6 7 7 Time taken: 0.065 seconds, Fetched: 1 row(s)
字符串反轉函數:reverse
語法: reverse(string A) 返回值: string 說明:返回字符串A的反轉結果 舉例: hive> select reverse('abcedfg'); OK gfdecba Time taken: 0.077 seconds, Fetched: 1 row(s)
字符串連接函數:concat
1 語法: concat(string A, string B…) 2 返回值: string 3 說明:返回輸入字符串連接后的結果,支持任意個輸入字符串 4 舉例:hive> select concat('abc','def','gh'); 5 OK 6 abcdefgh 7 Time taken: 0.063 seconds, Fetched: 1 row(s)
帶分隔符字符串連接函數:concat_ws
語法: concat_ws(string SEP, string A, string B…) 返回值: string 說明:返回輸入字符串連接后的結果,SEP表示各個字符串間的分隔符 舉例: hive> select concat_ws('-','abc','def','gh'); OK abc-def-gh Time taken: 0.06 seconds, Fetched: 1 row(s)
字符串截取函數:substr,substring
1 語法: substr(string A, int start),substring(string A, int start) 2 返回值: string 3 說明:返回字符串A從start位置到結尾的字符串 4 舉例: hive> select substr('abcde',3); 5 OK 6 cde 7 Time taken: 0.062 seconds, Fetched: 1 row(s) 8 hive> select substring('abcde',3); 9 OK 10 cde 11 Time taken: 0.05 seconds, Fetched: 1 row(s) 12 hive> select substr('abcde',-1); 13 OK 14 e 15 Time taken: 0.061 seconds, Fetched: 1 row(s)
字符串截取函數:substr,substring
1 語法: substr(string A, int start, int len),substring(string A, int start, int len) 2 返回值: string 3 說明:返回字符串A從start位置開始,長度為len的字符串 4 舉例: 5 hive> select substr('abcde',3,2); 6 OK 7 cd 8 Time taken: 0.07 seconds, Fetched: 1 row(s) 9 hive> select substring('abcde',3,2); 10 OK 11 cd 12 Time taken: 0.062 seconds, Fetched: 1 row(s) 13 hive> select substring('abcde',-2,2); 14 OK 15 de 16 Time taken: 0.113 seconds, Fetched: 1 row(s)
字符串轉大寫函數:upper,ucase
1 語法: upper(string A) ucase(string A) 2 返回值: string 3 說明:返回字符串A的大寫格式 4 舉例:hive> select upper('abSEd'); 5 OK 6 ABSED 7 Time taken: 0.059 seconds, Fetched: 1 row(s) 8 hive> select ucase('abSEd'); 9 OK 10 ABSED 11 Time taken: 0.058 seconds, Fetched: 1 row(s)
字符串轉小寫函數:lower,lcase
1 語法: lower(string A) lcase(string A) 2 返回值: string 3 說明:返回字符串A的小寫格式 4 舉例: 5 hive> select lower('abSEd'); 6 OK 7 absed 8 Time taken: 0.068 seconds, Fetched: 1 row(s) 9 hive> select lcase('abSEd'); 10 OK 11 absed 12 Time taken: 0.057 seconds, Fetched: 1 row(s)
去空格函數:trim
1 語法: trim(string A) 2 返回值: string 3 說明:去除字符串兩邊的空格 4 舉例:hive> select trim(' abc '); 5 OK 6 abc 7 Time taken: 0.058 seconds, Fetched: 1 row(s)
左邊去空格函數:ltrim
1 語法: ltrim(string A) 2 返回值: string 3 說明:去除字符串左邊的空格 4 舉例: hive> select ltrim(' abc '); 5 OK 6 abc 7 Time taken: 0.059 seconds, Fetched: 1 row(s)
右邊去空格函數:rtrim
1 語法: rtrim(string A) 2 返回值: string 3 說明:去除字符串右邊的空格 4 舉例:hive> select rtrim(' abc '); 5 OK 6 abc 7 Time taken: 0.058 seconds, Fetched: 1 row(s)
正則表達式解析函數:regexp_extract
其中的index,是按照正則字符串()的位置
1 語法: regexp_extract(string subject, string pattern, int index) 2 返回值: string 3 說明:將字符串subject按照pattern正則表達式的規則拆分,返回index指定的字符。注意,在有些情況下要使用轉義字符 4 舉例: 5 hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 1); 6 OK 7 the 8 Time taken: 0.389 seconds, Fetched: 1 row(s) 9 hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 2); 10 OK 11 bar 12 Time taken: 0.051 seconds, Fetched: 1 row(s) 13 hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 0); 14 OK 15 foothebar 16 Time taken: 0.058 seconds, Fetched: 1 row(s)
函數parse_url,解析URL字符串
1 parse_url(url, partToExtract[, key]) - extracts a part from a URL 2 解析URL字符串,partToExtract的選項包含[HOST,PATH,QUERY,REF,PROTOCOL,FILE,AUTHORITY,USERINFO]。 3 舉例: 4 hive> select parse_url('http://facebook.com/path/p1.php?query=1', 'HOST') ; 5 OK 6 facebook.com 7 Time taken: 0.286 seconds, Fetched: 1 row(s) 8 hive> select parse_url('http://facebook.com/path/p1.php?query=1', 'PATH'); 9 OK 10 /path/p1.php 11 Time taken: 0.069 seconds, Fetched: 1 row(s) 12 hive> select parse_url('http://facebook.com/path/p1.php?query=1', 'QUERY'); 13 OK 14 query=1 15 可以指定key來返回特定參數,例如 16 Time taken: 0.21 seconds, Fetched: 1 row(s) 17 hive> select parse_url('http://facebook.com/path/p1.php?query=1', 'QUERY','query'); 18 OK 19 1 20 Time taken: 0.057 seconds, Fetched: 1 row(s) 21 hive> select parse_url('http://facebook.com/path/p1.php?query=1#Ref', 'REF'); 22 OK 23 Ref 24 Time taken: 0.055 seconds, Fetched: 1 row(s) 25 hive> select parse_url('http://facebook.com/path/p1.php?query=1#Ref', 'PROTOCOL'); 26 OK 27 http 28 Time taken: 0.06 seconds, Fetched: 1 row(s)
1 hive> select parse_url_tuple('http://facebook.com/path1/p.php?k1=v1&k2=v2#Ref1', 'QUERY:k1', 'QUERY:k2'); 2 OK 3 v1 v2 4 Time taken: 0.2 seconds, Fetched: 1 row(s)
json解析函數:get_json_object
語法: get_json_object(string json_string, string path)
1 返回值: string 2 說明:解析json的字符串json_string,返回path指定的內容。如果輸入的json字符串無效,那么返回NULL。 3 舉例: hive> select get_json_object('{"store":{"fruit":\[{"weight":8,"type":"apple"},{"weight":9,"type":"pear"}],"bicycle":{"price":19.95,"color":"red"} }, "email":"amy@only_for_json_udf_test.net","owner":"amy"}','$.store'); 4 OK 5 {"fruit":[{"weight":8,"type":"apple"},{"weight":9,"type":"pear"}],"bicycle":{"price":19.95,"color":"red"}} 6 Time taken: 0.108 seconds, Fetched: 1 row(s) 7 8 hive> select get_json_object('{"store":{"fruit":\[{"weight":8,"type":"apple"},{"weight":9,"type":"pear"}],"bicycle":{"price":19.95,"color":"red"} }, "email":"amy@only_for_json_udf_test.net","owner":"amy"}','$.email'); 9 OK 10 amy@only_for_json_udf_test.net 11 12 hive> select get_json_object('{"store":{"fruit":\[{"weight":8,"type":"apple"},{"weight":9,"type":"pear"}],"bicycle":{"price":19.95,"color":"red"} }, "email":"amy@only_for_json_udf_test.net","owner":"amy"}','$.owner'); 13 OK 14 amy 15 Time taken: 0.499 seconds, Fetched: 1 row(s)
行轉列:explode (posexplode Available as of Hive 0.13.0)
1 說明:將輸入的一行數組或者map轉換成列輸出 2 語法:explode(array (or map)) 3 舉例: 4 5 hive> select explode(split(concat_ws('-','1','2','3','4','5','6','7','8','9'),'-')); 6 OK 7 1 8 2 9 3 10 4 11 5 12 6 13 7 14 8 15 9 16 Time taken: 0.095 seconds, Fetched: 9 row(s)
二.集合函數
集合查找函數: find_in_set
語法: find_in_set(string str, string strList)
返回值: int 說明: 返回str在strlist第一次出現的位置,strlist是用逗號分割的字符串。如果沒有找該str字符,則返回0 舉例: hive> select find_in_set('ab','ef,ab,de'); OK 2 Time taken: 2.336 seconds, Fetched: 1 row(s) hive> select find_in_set('at','ef,ab,de'); OK 0 Time taken: 0.094 seconds, Fetched: 1 row(s)
1 create table qa_test.student_test(id INT, info struct<name:STRING, age:INT>) 2 ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' 3 COLLECTION ITEMS TERMINATED BY ':'; 4 5 hive> desc qa_test.student_test; 6 OK 7 id int 8 info struct<name:string,age:int> 9 Time taken: 0.048 seconds, Fetched: 2 row(s) 10 11 12 $cat test5.txt 13 1,zhou:30 14 2,yan:30 15 3,chen:20 16 4,li:80 17 18 19 LOAD DATA LOCAL INPATH '/home/hadoop/test/test5' INTO TABLE qa_test.student_test; 20 21 hive> select * from qa_test.student_test; 22 OK 23 1 {"name":"zhou","age":30} 24 2 {"name":"yan","age":30} 25 3 {"name":"chen","age":20} 26 4 {"name":"li","age":80} 27 28 29 hive> select info.age from qa_test.student_test; 30 OK 31 30 32 30 33 20 34 80 35 Time taken: 0.234 seconds, Fetched: 4 row(s) 36 hive> select info.name from qa_test.student_test; 37 OK 38 zhou 39 yan 40 chen 41 li 42 Time taken: 0.08 seconds, Fetched: 4 row(s)
1 create table qa_test.class_test(name string, student_id_list array<INT>) 2 ROW FORMAT DELIMITED 3 FIELDS TERMINATED BY ',' 4 COLLECTION ITEMS TERMINATED BY ':'; 5 6 hive>desc qa_test.class_test; 7 OK 8 name string 9 student_id_list array<int> 10 Time taken: 0.052 seconds, Fetched: 2 row(s) 11 12 $ cat test6.txt 13 034,1:2:3:4 14 035,5:6 15 036,7:8:9:10 16 17 LOAD DATA LOCAL INPATH '/home/hadoop/test/test6' INTO TABLE qa_test.class_test; 18 19 hive> select * from qa_test.class_test; 20 OK 21 034 [1,2,3,4] 22 035 [5,6] 23 036 [7,8,9,10] 24 Time taken: 0.076 seconds, Fetched: 3 row(s) 25 26 select student_id_list[3] from qa_test.class_test; 27 28 hive> select student_id_list[3] from qa_test.class_test; 29 OK 30 4 31 NULL 32 10 33 Time taken: 0.12 seconds, Fetched: 3 row(s) 34 35 hive> select size(student_id_list) from qa_test.class_test; 36 OK 37 4 38 2 39 4 40 Time taken: 0.086 seconds, Fetched: 3 row(s) 41 42 43 hive> select array_contains(student_id_list,4) from qa_test.class_test; 44 OK 45 true 46 false 47 false 48 Time taken: 0.129 seconds, Fetched: 3 row(s) 49 50 hive> 51 > select sort_array(student_id_list) from qa_test.class_test; 52 OK 53 [1,2,3,4] 54 [5,6] 55 [7,8,9,10] 56 Time taken: 0.085 seconds, Fetched: 3 row(s)
1 create table qa_test.employee(id string, perf map<string, int>) 2 ROW FORMAT DELIMITED 3 FIELDS TERMINATED BY '\t' 4 COLLECTION ITEMS TERMINATED BY ',' 5 MAP KEYS TERMINATED BY ':'; 6 7 8 $ cat test7.txt 9 1 job:80,team:60,person:70 10 2 job:60,team:80 11 3 job:90,team:70,person:100 12 13 LOAD DATA LOCAL INPATH '/home/hadoop/test/test7' INTO TABLE qa_test.employee; 14 15 hive> select * from qa_test.employee; 16 OK 17 1 {"job":80,"team":60,"person":70} 18 2 {"job":60,"team":80} 19 3 {"job":90,"team":70,"person":100} 20 Time taken: 0.075 seconds, Fetched: 3 row(s) 21 22 hive> select perf['job'] from qa_test.employee where perf['job'] is not null; 23 OK 24 80 25 60 26 90 27 Time taken: 0.096 seconds, Fetched: 3 row(s) 28 29 hive> select size(perf) from qa_test.employee; 30 OK 31 3 32 2 33 3 34 Time taken: 0.091 seconds, Fetched: 3 row(s) 35 36 hive> select map_keys(perf) from qa_test.employee; 37 OK 38 ["job","team","person"] 39 ["job","team"] 40 ["job","team","person"] 41 Time taken: 0.136 seconds, Fetched: 3 row(s) 42 43 hive> select map_values(perf) from qa_test.employee; 44 OK 45 [80,60,70] 46 [60,80] 47 [90,70,100] 48 Time taken: 0.077 seconds, Fetched: 3 row(s)
求map的長度 size(Map<K.V>)
1 返回值:int 2 hive> select size(perf) from qa_test.employee; 3 OK 4 3 5 2 6 3 7 Time taken: 0.091 seconds, Fetched: 3 row(s)
求數組的長度 size(Array<T>)
1 返回值:int 2 hive> select size(student_id_list) from qa_test.class_test; 3 OK 4 4 5 2 6 4 7 Time taken: 0.086 seconds, Fetched: 3 row(s)
返回map中的所有key
map_keys(Map<K.V>)
1 返回值:array<K> 2 hive> select map_keys(perf) from qa_test.employee; 3 OK 4 ["job","team","person"] 5 ["job","team"] 6 ["job","team","person"] 7 Time taken: 0.136 seconds, Fetched: 3 row(s)
返回map中的所有value map_values(Map<K.V>)
1 返回值:array<V> 2 hive> select map_values(perf) from qa_test.employee; 3 OK 4 [80,60,70] 5 [60,80] 6 [90,70,100] 7 Time taken: 0.077 seconds, Fetched: 3 row(s)
如該數組Array<T>包含value返回true。,否則返回false array_contains(Array<T>, value)
1 返回值:boolean 2 hive> select array_contains(student_id_list,4) from qa_test.class_test; 3 OK 4 true 5 false 6 false 7 Time taken: 0.129 seconds, Fetched: 3 row(s)
按自然順序對數組進行排序並返回 sort_array(Array<T>)
1 返回值:array 2 hive> select sort_array(student_id_list) from qa_test.class_test; 3 OK 4 [1,2,3,4] 5 [5,6] 6 [7,8,9,10] 7 Time taken: 0.085 seconds, Fetched: 3 row(s)
二.聚合函數
統計總行數,包括含有NULL值的行 count(*)
統計提供非NULL的expr表達式值的行數 count(expr)
統計提供非NULL且去重后的expr表達式值的行數 count(DISTINCT expr[, expr])
1 返回值:BIGINT
sum(col),表示求指定列的和,sum(DISTINCT col)表示求去重后的列的和
1 返回值:DOUBLE
avg(col),表示求指定列的平均值,avg(DISTINCT col)表示求去重后的列的平均值
1 返回值:DOUBLE
求指定列的最小值 min(col)
1 返回值:DOUBLE
求指定列的最大值 max(col)
1 返回值:DOUBLE
求指定列數值的方差 variance(col), var_pop(col)
1 返回值:DOUBLE
求指定列數值的樣本方差
var_samp(col)
1 返回值:DOUBLE
求指定列數值的標准偏差
stddev_pop(col)
1 返回值:DOUBLE
求指定列數值的樣本標准偏差
stddev_samp(col)
1 返回值:DOUBLE
求指定列數值的協方差 covar_pop(col1, col2)
1 返回值:DOUBLE
求指定列數值的樣本協方差 covar_samp(col1, col2)
1 返回值:DOUBLE
返回兩列數值的相關系數 corr(col1, col2)
1 返回值:DOUBLE
返回col的p%分位數 percentile(BIGINT col, p)
1 返回值:DOUBLE
三.特殊函數
窗口函數
分析函數
混合函數
UDTF
多行轉換:lateral view
說明:lateral view用於和json_tuple,parse_url_tuple,split, explode等UDTF一起使用,它能夠將一行數據拆成多行數據,在此基礎上可以對拆分后的數據進行聚合。
舉例:
1 說明:lateral view用於和json_tuple,parse_url_tuple,split, explode等UDTF一起使用,它能夠將一行數據拆成多行數據,在此基礎上可以對拆分后的數據進行聚合。 2 舉例: 3 4 hive> select s.x,sp from test.dual s lateral view explode(split(concat_ws(',','1','2','3','4','5','6','7','8','9'),',')) t as sp; 5 x sp 6 a 1 7 b 2 8 a 3 9 解釋一下,from后面是你的表名,在表名后面加lateral view explode。。。(你的行轉列sql) ,還必須要起一個別名,我這個字段的別名為sp。然后再看看select后面的 s.*,就是原表的字段,我這里面只有一個字段,且為X 10 11 多個lateral view的sql類如: 12 13 SELECT * FROM exampleTable LATERAL VIEW explode(col1) myTable1 AS myCol1 LATERAL VIEW explode(myCol1) myTable2 AS myCol2;
抽取一行數據轉換到新表的多列樣例:
http_referer是獲取的帶參數請求路徑,其中非法字符用\做了轉義,根據路徑解析出地址,查詢條件等存入新表中,
1 drop table if exists t_ods_tmp_referurl; 2 create table t_ ods _tmp_referurl as 3 SELECT a.*,b.* 4 FROM ods_origin_weblog a LATERAL VIEW parse_url_tuple(regexp_replace(http_referer, "\"", ""), 'HOST', 'PATH','QUERY', 'QUERY:id') b as host, path, query, query_id; 5 6 復制表,並將時間截取到日: 7 drop table if exists t_ods_tmp_detail; 8 create table t_ods_tmp_detail as 9 select b.*,substring(time_local,0,10) as daystr, 10 substring(time_local,11) as tmstr, 11 substring(time_local,5,2) as month, 12 substring(time_local,8,2) as day, 13 substring(time_local,11,2) as hour 14 From t_ ods _tmp_referurl b;
表生成函數
對於a中的每個元素,將生成一行且包含該元素
explode(array<TYPE> a)
1 返回值:Array Type
每行對應數組中的一個元素 explode(ARRAY)
1 返回值:N rows
每行對應每個map鍵-值,其中一個字段是map的鍵,另一個字段是map的值
explode(MAP)
1 返回值:N rows
explode類似,不同的是還返回各元素在數組中的位置
posexplode(ARRAY)
1 返回值:N rows
把M列轉換成N行,每行有M/N個字段,其中n必須是個常數
stack(INT n, v_1, v_2, ..., v_k)
1 返回值:N rows
從一個JSON字符串中獲取多個鍵並作為一個元組返回,與get_json_object不同的是此函數能一次獲取多個鍵值
json_tuple(jsonStr, k1, k2, ...)
1 返回值:tuple
返回從URL中抽取指定N部分的內容,參數url是URL字符串,而參數p1,p2,....是要抽取的部分,這個參數包含HOST, PATH, QUERY, REF, PROTOCOL, AUTHORITY, FILE, USERINFO, QUERY:<KEY>
1 返回值;tuple
將結構體數組提取出來並插入到表中 inline(ARRAY<STRUCT[,STRUCT]>)