hive 中简单的udf函数编写

 

1.注册函数,使用using jar方式在hdfs上引用udf库。
$hive>create function formattime as 'com.bigdata.udf.FormatTimeUDF' using jar 'hdfs://hadoop01/app/app-logs-hive-1.0-SNAPSHOT.jar';2.注销函数,只需要删除mysql的hive数据记录即可。
delete from func_ru ;
delete from funcs ;
show funcyions;
desc formatted function substring;



2.udf函数获取天开始一些简单方法
@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() throws ParseException { return evaluate(new Date()); } /** * 指定天偏移量 */ public long evaluate(int offset) throws ParseException { return evaluate(DateUtil.getDayBeginTime(new Date(), offset)); } /** * 计算某天的开始时刻(毫秒数) */ public long evaluate(Date d) throws ParseException { return DateUtil.getDayBeginTime(d).getTime(); } /** * 计算某天的开始时刻(毫秒数) */ public long evaluate(Date d, int offset) throws ParseException { return DateUtil.getDayBeginTime(d, offset).getTime(); } /** * 计算某天的起始时刻(毫秒数) */ public long evaluate(String dateStr) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date d = sdf.parse(dateStr); return evaluate(d); } /** * 计算某天的起始时刻(毫秒数) */ public long evaluate(String dateStr, int offset) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date d = sdf.parse(dateStr); return DateUtil.getDayBeginTime(d, offset).getTime(); } /** * 计算某天的起始时刻(毫秒数) */ public long evaluate(String dateStr, String fmt) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat(fmt); Date d = sdf.parse(dateStr); return DateUtil.getDayBeginTime(d).getTime(); } /** * 计算某天的起始时刻(毫秒数) */ public long evaluate(String dateStr, String fmt, int offset) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat(fmt); Date d = sdf.parse(dateStr); return DateUtil.getDayBeginTime(d, offset).getTime(); } }

 

posted @ 2019-01-25 18:03  夜半钟声到客船  阅读(1264)  评论(0编辑  收藏  举报