Hive自定義UDF函式--常用的工具類
阿新 • • 發佈:2019-02-06
註冊函式:
將自定義函式打成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; } }