1. Configuring the Environment 1. Introduction JAR Package: Hive-exec-0.8.0.jar and Hadoop-core-0.20.2.jar
2. Writing UDF performance 1.extends UDF2. Once again, define the evaluate function of the UDF.
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 {//convert string to datestring str = datestring.split ("") [0]; SimpleDateFormat sdf= New SimpleDateFormat ("YYYY-MM-DD");D ate date =sdf.parse (str);//convert date to Calendarcalendar Calendar = Calendar.getinstance (); Calendar.settime (date);//Get date of the week int week = Calendar.get (calendar.day_of_week)-1 if (week = = 0 | | week = = 6) {return true;} else {return false;}} catch (Exception e) {///Todo:handle Exceptionreturn false;}}}
3. Bring the jar package into the Hadoop environment 1. Place the jar under the physical folder of Hive's Lib;
2. Open hiveclient, add Jar Pack Add Jar/home/hive/lib/hiveudf.jar
3. Creating a temporary file create temporary function Issundayorsaturday as ' com.qunar.hiveudf.IsSundayOrSaturday '
4. Calling function Select Issundayorsaturday (intime) from User_tag
Note: 1. Assume that the function's parameters contain a file. You first put the file in the execution environment of hive add File/home/filename
Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.
UDF Formulations for Hive