hive内置函数

822 阅读3分钟

关系运算

  • like A like B 如果字符串A符合表达式B 的正则语法,则为TRUE,如果字符串A或者字符串B为NULL
select 'football' like 'foot%';->true 
  • rlike A RLIKE B 如果字符串A符合JAVA正则表达式B的正则语法,则为TRUE
  • REGEXP 功能与RLIKE相同

数学函数

  • round 四舍五入 round(DOUBLE a, INT d) d表示要保留小数位数
select ceil(45.926,2) ->46.93
  • ceil 向上取整
select ceil(45.926) ->46
  • floor 向下取整
select ceil(45.926) ->45
- round 取随机数

字符串函数

  • lower 小写字母
  • upper 大写字母
  • lengh 长度
select length('你好')->2

- reverse 字符串反转
- concat 字符串拼接

select concat('程序猿',' 你好')->程序猿你好

  • concat_ws 带分隔符字符串连接函数
  concat_ws(string SEP, string A, string B…)
  SEP表示各个字符串间的分隔符

  • substr 字符串子串
select substr('程序猿你好',4,2)->你好
  • trim 去掉两端空格
  • lpad 左填充 lpad(String a,int b,String c) b表示填充后的总长度
lpad('程序猿你好', 10, *)->*****程序猿你好
  • rpad 右填充
 rpad('程序猿你好', 10, *)->程序猿你好*****
  • split 分隔字符串函数
按照pat字符串分割str,会返回分割后的字符串数组
select split('abtcdtef','t')->["ab","cd","ef"]
  • repeat 重复字符串函数
 repeat(string str, int n)  返回重复n次后的str字符串
select repeat('abc',2)->abcabc

收集函数

  • size 求map或者array的长度

转换函数

  • cast
select cast(1 as float)->1.0

日期函数

  • unix_timestamp 获得当前时区的UNIX时间戳
  • to_date 转日期函数 to_date(string time)
  • year 日期转年函数year(string date)
  • month 日期转月函数
  • day 日期转天函数
  • hour 日期转小时函数
  • minute 日期转分钟函数
  • weekofyear 日期转周函数
select weekofyear('2011-12-08')->49
  • datediff 日期比较函数
atediff(string enddate, string startdate)
select datediff('2012-12-08','2012-05-09')->213
  • date_sub 日期减少函数

  • date_add 日期增加函数

    date_add(string startdate, int days) select date_add('2012-12-08',10)->2012-12-18

  • from_unixtime UNIX时间戳转日期函数

select from_unixtime(1323308943,'yyyyMMdd')->  20111208

条件函数

  • if 函数
if(boolean testCondition, T valueTrue, T valueFalseOrNull)
 select if(1=2,100,200)->200
  • coalesce 非空查找,返回参数中第一个非空值
 COALESCE(T v1, T v2, …)
select COALESCE(null,'100','50′)->100

  • case 条件判断函数

    CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END 如果a等于b,那么返回c;如果a等于d,那么返回e;否则返回f CASE WHEN a THEN b [WHEN c THEN d]* [ELSE e] END
    如果a为TRUE,则返回b;如果c为TRUE,则返回d;否则返回e

复杂类型

  • array类型访问: A[n]
  • map类型访问: M[key]
  • struct类型访问: S.x
  • Map类型构建: map (key1, value1, key2, value2, …)
  • Struct类型构建 :struct(val1, val2, val3, …)
  • array类型构建: array(val1, val2, …)

集合统计函数

  • count:个数统计
  • sum :总和统计
  • avg:平均值统计
  • min:最小值统计
  • max:组大值统计
  • percentile:中位数统计