在Hive中添加日期时间分钟 [英] Add minutes to datetime in Hive
问题描述
在Hive中有一个函数可以用来在SQL Server中添加分钟(以int为单位)到类似于 DATEADD(datepart,number,date)
的日期时间,其中 datepart
可以是分钟
:
DATEADD(分钟,2,'2014-07 -06 01:28:02')
返回 2014-07-06 01:28:02
在另一个Hive的 date_add(string startdate,int days)
位于 days
中。任何对小时
?
您的问题可以轻松解决HiveUdf。
package HiveUDF;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
导入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();
日期AddingMins =新日期(时间+(分钟* ONE_MINUTE_IN_MILLIS));
String date = formatter.format(AddingMins);
返回日期;
}
}
创建AddMinuteUdf.jar后,在Hive中注册;
添加JAR /home/Kishore/AddMinuteUdf.jar;
$
将临时函数addMinute创建为'HiveUDF.addMinuteUdf';
配置单元>从ATable中选择日期;
确定
2014-07-06 01:28:02
拍摄时间:0.108秒,提取:1行
在应用函数
hive>后,从ATable中选择addMinuteUdf(date,2);
确定
2014-07-06 01:30:02
所用时间:0.094秒,提取:1行
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 wheredatepart
can beminutes
:DATEADD(minute,2,'2014-07-06 01:28:02')
returns2014-07-06 01:28:02
On the other hand, Hive'sdate_add(string startdate, int days)
is indays
. Any of such forhours
?解决方案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)
这篇关于在Hive中添加日期时间分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!