Hive自定义UDF函数--常用的工具类

注册函数:

将自定义函数打成jar包,上传hdfs

$hive>create function formattime as 'com.air.udf.FormatTimeUDF' using jar 'hdfs://mycluster/user/centos/air-hive-1.0-SNAPSHOT.jar'

使用:

$hive>select formattime(datatime,'yyyy/MM/dd') day   from test where datatime>= getDaybegin() and datatime< getDaybegin(1) ;

自定义函数:

1,将long型的时间片格式化成指定日期格式

import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * long型的时间片格式化成指定日期格式
 */
@Description(name = "udf_formattime",
      value = "formattime",
      extended = "formattime() ;\r\n"
                     + " formattime(1234567,'yyyy/MM/01') \r\n"
                     + " formattime('1234567','yyyy/MM/dd')")
public class FormatTimeUDF extends UDF {

   /**
    * 格式化时间,long    */
   public String evaluate(long ms,String fmt) throws ParseException {
      SimpleDateFormat sdf = new SimpleDateFormat(fmt) ;
      Date d = new Date();
      d.setTime(ms);
      return sdf.format(d) ;
   }
   /**
    * 格式化时间,string类型
    */
   public String evaluate(String ms,String fmt) throws ParseException {
      SimpleDateFormat sdf = new SimpleDateFormat(fmt) ;
      Date d = new Date();
      d.setTime(Long.parseLong(ms));
      return sdf.format(d) ;
   }

   /**
    * 格式化时间,string类型
    */
   public String evaluate(long ms ,String fmt , int week) throws ParseException {
      Date d = new Date();
      d.setTime(ms);
      //周内第一天
      Date firstDay = DateUtil.getWeekBeginTime(d) ;
      SimpleDateFormat sdf = new SimpleDateFormat(fmt) ;
      return sdf.format(firstDay) ;
   }
}

2,得到今天的起始时间:

import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;

import java.text.SimpleDateFormat;
import java.util.Date;

@Description(name = "udf_getdaybegin",
        value = "getdaybegin",
        extended = "getdaybegin() ;\r\n"
                + " getdaybegin(2) \r\n"
                + " getdaybegin('2017/06/29 01:02:03') \r\n"
                + " getdaybegin('2017/06/29 01:02:03',2) \r\n"
                + " getdaybegin(date_obj) \r\n"
                + " getdaybegin(date_obj,2)")
public class DayBeginUDF extends UDF{

    /**
     * 计算现在的起始时刻(毫秒数)
     */
    public long  evaluate(){
      return DateUtil.getDayBeginTime(new Date()).getTime();
    }

    public long  evaluate(Date d){

        return DateUtil.getDayBeginTime(d).getTime();
    }

    public long  evaluate(int offset){
        return DateUtil.getDayBeginTime(new Date(),offset).getTime();
    }

    public long  evaluate(Date d,int offset){
        return DateUtil.getDayBeginTime(d,offset).getTime();
    }

    public  long  evaluate(String dateStr) throws Exception{
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date d = df.parse(dateStr);
        return evaluate(d);
    }
    public long  evaluate(String dateStr,int offset) throws Exception{
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date d = df.parse(dateStr);
        return evaluate(d,offset);
    }
    public long  evaluate(String dateStr,String fmt) throws Exception{
        SimpleDateFormat df = new SimpleDateFormat(fmt);
        Date d = df.parse(dateStr);
        return evaluate(d);
    }
    public long  evaluate(String dateStr,int offset, String fmt) throws Exception{
        SimpleDateFormat df = new SimpleDateFormat(fmt);
        Date d = df.parse(dateStr);
        return evaluate(d,offset);
    }

    
}
 
  

工具类:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
   /**
    * 得到指定date的零时刻.
    */
   public static Date getDayBeginTime(Date d) {
      try {
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd 00:00:00");
         return sdf.parse(sdf.format(d));
      } catch (Exception e) {
         e.printStackTrace();
      }
      return null;
   }

   /**
    * 得到指定date的偏移量零时刻.
    */

   public static Date getDayBeginTime(Date d,int offset) {
      try {
         Date date=getDayBeginTime(d);
         Calendar c = Calendar.getInstance();
         c.setTime(date);
         c.add(Calendar.DAY_OF_MONTH,offset);
         return c.getTime();
      } catch (Exception e) {
         e.printStackTrace();
      }
      return null;
   }

   /**
    * 得到指定date所在周的起始时刻.
    */
   public static Date getWeekBeginTime(Date d) {
      try {
         Date date=getDayBeginTime(d);
         Calendar c = Calendar.getInstance();
         c.setTime(date);
         int i = c.get(Calendar.DAY_OF_WEEK);
         c.add(Calendar.DAY_OF_MONTH,-(i-1));
         return c.getTime();
      } catch (Exception e) {
         e.printStackTrace();
      }
      return null;
   }
   /**
    * 得到指定date所在周的结束时刻.
    */
   public static Date getWeekEndTime(Date d) {
      try {
         Date date=getDayBeginTime(d);
         Calendar c = Calendar.getInstance();
         c.setTime(date);
         int i = c.get(Calendar.DAY_OF_WEEK);
         c.add(Calendar.DAY_OF_MONTH,(8-i));
         return c.getTime();
      } catch (Exception e) {
         e.printStackTrace();
      }
      return null;
   }
   /**
    * 得到指定date所在周的结束时刻.
    */
   public static Date getWeekEndTime(Date d,int offset) {
      try {
         Date date=getDayBeginTime(d);
         Calendar c = Calendar.getInstance();
         c.setTime(date);
         int i = c.get(Calendar.DAY_OF_WEEK);
         c.add(Calendar.DAY_OF_MONTH,(8-i));
         c.add(Calendar.DAY_OF_MONTH,offset*7);
         return c.getTime();
      } catch (Exception e) {
         e.printStackTrace();
      }
      return null;
   }

   /**
    * 得到指定date所在周的起始时刻.
    */
   public static Date getWeekBeginTime(Date d,int offset) {
      try {
         Date date=getDayBeginTime(d);
         Calendar c = Calendar.getInstance();
         c.setTime(date);
         int i = c.get(Calendar.DAY_OF_WEEK);
         c.add(Calendar.DAY_OF_MONTH,-(i-1));
         c.add(Calendar.DAY_OF_MONTH,offset*7);
         return c.getTime();
      } catch (Exception e) {
         e.printStackTrace();
      }
      return null;
   }
   /**
    * 得到指定date所在月的起始时刻.
    */
   public static Date getMonthBeginTime(Date d ) {
      try {
         Date date=getDayBeginTime(d);
         Calendar c = Calendar.getInstance();
         c.setTime(date);
         int i = c.get(Calendar.DAY_OF_MONTH);
         c.add(Calendar.DAY_OF_MONTH,-(i-1));
         return c.getTime();
      } catch (Exception e) {
         e.printStackTrace();
      }
      return null;
   }
   /**
    * 得到指定date所在月的起始时刻.
    */
   public static Date getMonthBeginTime(Date d,int offset) {
      try {
         Date date=getDayBeginTime(d);
         Calendar c = Calendar.getInstance();
         c.setTime(date);
         int i = c.get(Calendar.DAY_OF_MONTH);
         c.add(Calendar.DAY_OF_MONTH,-(i-1));
         c.add(Calendar.MONTH,offset);
         return c.getTime();
      } catch (Exception e) {
         e.printStackTrace();
      }
      return null;
   }

}

你可能感兴趣的:(Hive)