UDF Formulations for Hive

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.