Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add minutes to datetime in Hive

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?

like image 268
DevEx Avatar asked May 22 '15 14:05

DevEx


2 Answers

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.

like image 76
Sergey Sidorov Avatar answered Sep 22 '22 08:09

Sergey Sidorov


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)
like image 39
Kishore Avatar answered Sep 22 '22 08:09

Kishore