DOC PREVIEW
UT Dallas CS 6350 - 11.Hive_UDF_toUpper

This preview shows page 1-2-3-4 out of 11 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1UDF: User-Defined FunctionsUDF cont’dUDF cont’dCustom UDF ExampleJava codeJava codeCompile, JAR and Create func.Function useDropping a temp UDFThank youHive UDFhttp://hortonworks.com/wp-content/uploads/downloads/2013/09/HWX.Qubole.Hive_.UDF_.Guide_.1.0.pdfUT Dallas1UDF: User-Defined FunctionsUDF is a Great tool for extending HiveQL.Written in Jave and then integrated to Hive as built-in functions.Can be called from a Hive query.Hive Built-in functions:hive> SHOW FUNCTIONS;hive> DESCRIBE FUNCTION concat;hive> DESCRIBE FUNCTION EXTENDED concat;concat(str1, str2, ... strN) - returns the concatenation of str1, str2, ... strN Returns NULL if any argument is NULL. Example: > SELECT concat('abc', 'def') FROM src LIMIT 1; 'abcdef'2UDF cont’dSELECT concat(column1,column2) AS x FROM table;Standard Functionsround(), floor(), abs()ucase(), reverse()Aggregate Functionssum(), avg(), count(), min()andmax()3UDF cont’dUDTFs: User Defined Table Generating Functionshive> select split(bday, '-') as bd_func from littlebigdata;["2","12","1981"]["10","10","2004"]["4","5","1974"]hive> select explode(split(bday, '-')) as bd_func from littlebigdata;2121981101020044519744Custom UDF Examplemy_to_upper functionWe will use the following:File name: littlebigdata.txt with the following content:edward capriolo,[email protected],2-12-1981,209.191.139.200,M,10bob,[email protected],10-10-2004,10.10.10.1,M,50sara connor,[email protected],4-5-1974,64.64.5.1,F,2hive > CREATE TABLE IF NOT EXISTS littlebigdata( name STRING, email STRING, bday STRING, ip STRING, gender STRING, anum INT) ROW FORMAT DELIMITED FIELDS TERMINATED BY ','; hive> LOAD DATA LOCAL INPATH ‘unix/path/to/littlebigdata.txt' INTO TABLE littlebigdata;5Java codeimport org.apache.hadoop.hive.ql.exec.UDF;import org.apache.hadoop.hive.ql.exec.Description;import org.apache.hadoop.io.Text;@Description(name = "my_to_upper",value = "_FUNC_(str) - Converts a string to uppercase",extended = "Example:\n"+ " > SELECT my_to_upper(author_name) FROM authors a;")public class ToUpper extends UDF { public Text evaluate(Text s) { Text to_value = new Text(""); if (s != null) { try { to_value.set(s.toString().toUpperCase()); } catch (Exception e) { // Should never happen to_value = new Text(s); } } return to_value; }}6Java codeExtend UDF class and write the evaluate() function.evalute() methods can be overloaded.@Description(...)Tis an optional JavaTannotation for DESCRIBE FUNCTION ... command._FUNC_Tstrings will be replaced with the function name.Arguments and return types are what Hive can serialze (e.g., for numbers, use int, Integer wrapper object, or IntWritable which Hadoop wrapper for integers).In previous example we used Text7Compile, JAR and Create func.In the Unix shell:$ mkdir udf_classes_toUpper;$ javac -classpath /usr/local/hive-0.9.0/lib/hive-exec-0.9.0.jar:/usr/local/hadoop-1.2.1/hadoop-core-1.2.1.jar -d udf_classes_toUpper ToUpper.java$ jar -cvf toupper.jar -C udf_classes_toUpper/ .In the Hive shell:hive> add jar /people/cs/l/lkhan/toupper.jar;hive> CREATE TEMPORARY FUNCTION my_to_upper as 'ToUpper'; -- ToUpper is the Jave class name8Function usehive> desc function extended my_to_upper;my_to_upper(str) - Converts a string to uppercaseExample: > SELECT my_to_upper(author_name) FROM authors a;hive> select name, my_to_upper(name) from littlebigdata;edward capriolo EDWARD CAPRIOLObob BOBsara connor SARA CONNOR9Dropping a temp UDFhive> DROP TEMPORARY FUNCTION IF EXISTS my_to_upper;To make a function permanent:Code should be added to Hive source code (FunctionRegistry class)Rebuild Hive and redeploy.10Thank youProgramming Hive


View Full Document

UT Dallas CS 6350 - 11.Hive_UDF_toUpper

Documents in this Course
HW3

HW3

5 pages

NOSQL-CAP

NOSQL-CAP

23 pages

BigTable

BigTable

39 pages

HW3

HW3

5 pages

Load more
Download 11.Hive_UDF_toUpper
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view 11.Hive_UDF_toUpper and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view 11.Hive_UDF_toUpper 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?