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
?
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With