将分钟添加到 Hive 中的日期时间 [英] Add minutes to datetime in Hive

查看:75
本文介绍了将分钟添加到 Hive 中的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Hive 中是否有一个函数可以用来将分钟(int)添加到类似于 DATEADD (datepart,number,date) in sql server where datepart 的日期时间> 可以是 minutes:DATEADD(minute,2,'2014-07-06 01:28:02') 返回 2014-07-06 01:28:02
另一方面,Hive 的 date_add(string startdate, int days)days 中.hours 中的任何一个?

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?

推荐答案

您的问题可以通过 HiveUdf 轻松解决.

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;
    }
}

创建 AddMinuteUdf.jar 后,在 Hive 中注册;

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)

应用函数后

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆