在Hive中添加分钟到日期时间

Dev*_*vEx 3 hadoop hive hiveql

是否有蜂巢一个可以用它来添加分钟(以int)以相似的日期时间的功能DATEADD (datepart,number,date)在SQL服务器里datepart可以minutes: DATEADD(minute,2,'2014-07-06 01:28:02')回报2014-07-06 01:28:02
.另一方面,蜂房的date_add(string startdate, int days)days.任何这样的hours

小智 16

您可以向datetime添加秒,将其转换为unix_timestamp(),然后将结果转换回datetime,而不是使用UDF.

例:

select from_unixtime(unix_timestamp('2015-12-12 16:15:17')+3600);
Run Code Online (Sandbox Code Playgroud)

这里我们加了一个小时:

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)
Run Code Online (Sandbox Code Playgroud)

因此,如果分钟添加,您应添加分钟数*60.

  • 使用它您将丢失时间戳的毫秒信息。最好使用基于 UDF 的方法 (2认同)

Kis*_*ore 4

您的问题可以通过 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

创建 AddMinuteUdf.jar 后,将其注册到 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)
Run Code Online (Sandbox Code Playgroud)

应用功能后

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