1. 程式人生 > >hive常用函式整理

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;