Add minutes to datetime in Hive

狂风中的少年 提交于 2019-11-26 23:38:13

问题


Is there a function in Hive one could use to add minutes(in int) to a datetime similar to DATEADD (datepart,number,date)in sql server where datepart can be minutes: DATEADD(minute,2,'2014-07-06 01:28:02') returns 2014-07-06 01:28:02
On the other hand, Hive's date_add(string startdate, int days) is in days. Any of such for hours?


回答1:


your problem can easily solve by HiveUdf.

package HiveUDF;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.hadoop.hive.ql.exec.UDF;

public class addMinuteUdf extends UDF{
    final long ONE_MINUTE_IN_MILLIS=60000;
    private  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public String evaluate(String t, int minute) throws ParseException{
        long time=formatter.parse(t.toString()).getTime();
        Date AddingMins=new Date(time + (minute * ONE_MINUTE_IN_MILLIS));
        String date = formatter.format(AddingMins);
        return date;
    }
}

After creating AddMinuteUdf.jar , Register it in Hive;

ADD JAR /home/Kishore/AddMinuteUdf.jar; 
create temporary FUNCTION addMinute as 'HiveUDF.addMinuteUdf';


hive> select date from ATable;
OK
2014-07-06 01:28:02
Time taken: 0.108 seconds, Fetched: 1 row(s)

After applying function

hive> select addMinuteUdf(date, 2) from ATable;     
OK
2014-07-06 01:30:02
Time taken: 0.094 seconds, Fetched: 1 row(s)



回答2:


Instead of using UDF, you can add seconds to datetime, converted it to unix_timestamp() and then convert the result back to datetime.

Example:

select from_unixtime(unix_timestamp('2015-12-12 16:15:17')+3600);

Here we added one hour:

hive> select from_unixtime(unix_timestamp('2015-11-12 12:15:17')+${seconds_in_hour});
OK
2015-11-12 13:15:17
Time taken: 0.121 seconds, Fetched: 1 row(s)

So, in case of minutes addition you shall add number of minutes*60.



来源:https://stackoverflow.com/questions/30399544/add-minutes-to-datetime-in-hive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!