Maison > Article > base de données > hive的udf的编写
1.配置环境 1.引入jar包:hive-exec-0.8.0.jar 和 hadoop-core-0.20.2.jar 2.编写udf函数 1.extends UDF 2.重新定义UDF的evaluate函数。 package com.qunar.hiveudf;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;imp
package com.qunar.hiveudf; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import org.apache.hadoop.hive.ql.exec.UDF; public class IsSundayOrSaturday extends UDF { public Boolean evaluate(String dateString) { try { //将string转化为date String str = dateString.split(" ")[0]; SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd"); Date date =sdf.parse(str); //将date转换为calendar Calendar calendar = Calendar.getInstance(); calendar.setTime(date); //获取date的星期 int week = calendar.get(Calendar.DAY_OF_WEEK)-1; if(week == 0 || week == 6) { return true; } else { return false; } } catch (Exception e) { // TODO: handle exception return false; } } }