hive常用函数整理

Hive常用的函数整理,方便快速查找使用,更多参考文档https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF


1.条件函数

select nvl(T v1, T default_value);     -- 如果v1不为null,返回v1否则defaultV
select if(boolean testCondition, T valueTrue, T valueFalseOrNull);	--if条件判断表达式
select coalesce(T v1, T v2, T v3, ...);  --返回第一个不为null的value值

2.时间戳格式化

select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss');
select current_timestamp;
select to_date('2016-08-09 10:09');       --格式化yyyy-MM-dd

3. 时间日期函数

select datediff('2016-09-01','2016-08-01');
select datediff(from_unixtime(unix_timestamp(),'yyyy-MM-dd'), '2016-09-01') <= 90;

select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'), 1);   --当前时间减1天
select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'), 1);   --当前时间加1天

4.字符串操作函数

select concat(substr('20170209',0,4),'-',substr('20170209',5,2),'-',substr('20170209',7,2));  --字符串截取拼接
select regexp_replace('2017-02-09', '-', '');        --字符串替换    
select concat("[",
          concat_ws(",", collect_list(
                             concat('{\"id\":\"',     nvl(id, ''),'\",', 
                                     '\"name\":\"',   nvl(name, ''),'\",',
                                     '\"score\":\"',  nvl(score, ''),'\",',
                                     '\"rank\":\"',   nvl(rank, ''),'\",',
                                     '\"rate\":\"', nvl(score * 1.0 / total_score, ''),'\"}'
                         ))
          ), 
        "]" );     --字符串拼接,转换字符串array, array分割为字符串

5.UDTF explode函数,单挑拆分出多条

select customer_id, product_id  from  rdm_recommender_user_products t 
      lateral view explode(split(t.productids,"\\|")) adtable as product_id;


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值