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;