Hive之常用函数

1.非空查找函数: COALESCE

语法: COALESCE(T v1, T v2,…)

返回值: T

说明: 返回参数中的第一个非空值;如果所有值都为NULL,那么返回NULL

举例:

hive> select COALESCE(index,-1)  as ind from table_dual;

如果index不为空,则返回index

否则返回-1

 

2.字符串连接函数concat

DT为环境变量当前日期 year-month-day
concat('${DT}', ' 00:00:00') as time
连接日期和时刻

3.字符串判断运算if

 

IF( Test Condition, True Value, False Value )

 

该语法只能用来判断单个条件,例如:

    select pd,
    if(con is null or con<0,0,1) as ct,
    from t limit 1;
con为空或者小于0,ct=0,否则ct=1。

4.字符串去掉两边空格

select trim(column_name) from table_name

5.case

(case lower(trim(coalesce(md,''))) when 'A' then 1 when 'B' then 2 when 'C' then 3 when 'D' then 4 else -1 end) as ds

6.group by去重功能

    能够将group by后面的组合进行去重

7.用max(if())将行转换列

表 

  flag

ret_1
ret_3
ret_7

max(if (flag = 'ret_1', ret_user, NULL)) as ret1_user,
max(if (flag = 'ret_3', ret_user, NULL)) as ret3_user,
max(if (flag = 'ret_7', ret_user, NULL)) as ret7_user

 

8.时间处理函数

日期函数UNIX时间戳转日期函数: from_unixtime语法:   from_unixtime(bigint unixtime[ timestamp, string format])

返回值: string

转化UNIX时间戳(从1970-01-01 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式

举例:

hive>   select from_unixtime(1323308943,’yyyyMMdd’) from tbl;

20111208

 

日期时间转日期函数: to_date语法:   to_date(string timestamp)

返回值:   string

说明: 返回日期时间字段中的日期部分。

举例:

hive>   select to_date(’2011-12-08 10:03:01′) from tbl;

2011-12-08

 

日期增加函数: date_add语法:   date_add(string startdate, int days)

返回值: string

说明: 返回开始日期startdate增加days天后的日期。

举例:

hive>   select date_add(’2012-12-08′,10) from tbl;

2012-12-18

日期减少函数: date_sub语法:   date_sub (string startdate, int days)

返回值: string

说明: 返回开始日期startdate减少days天后的日期。

举例:

hive>   select date_sub(’2012-12-08′,10) from tbl;

2012-11-28

posted @ 2018-08-08 11:07  王毅2016  阅读(545)  评论(0编辑  收藏  举报