User Defined Aggregate Functions (UDAF) Java Example
Step 1 - Add these jar files to your java project.
Max.java
Step 2 - Compile and create a jar file of your java project. Creating a jar file is left to you.
Step 3 - Create a Numbers_List.txt file
Step 4 - Add these following lines to Numbers_List.txt file
Step 5 - Change the directory to /usr/local/hive/bin
Step 6 - Enter into hive shell
Step 7 - Create a table Num_list, load Numbers_List.txt data into the table and verify. Save and close.
Step 8 - Add jar file in distributed cache, create a function and execute udaf function.
Step 1 - Add these jar files to your java project.
hive-exe*.jar
$HIVE_HOME/lib/*.jar $HADOOP_HOME/share/hadoop/mapreduce/*.jar $HADOOP_HOME/share/hadoop/common/*.jar
import org.apache.hadoop.hive.ql.exec.UDAF; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.hive.ql.exec.UDAFEvaluator; @SuppressWarnings("deprecation") public class Max extends UDAF { public static class MaxIntUDAFEvaluator implements UDAFEvaluator { private IntWritable output; public void init() { output = null; } public boolean iterate(IntWritable maxvalue) // Process input table { if (maxvalue == null) { return true; } if (output == null) { output = new IntWritable(maxvalue.get()); } else { output.set(Math.max(output.get(), maxvalue.get())); } return true; } public IntWritable terminatePartial() { return output; } public boolean merge(IntWritable other) { return iterate(other); } public IntWritable terminate() // final result { return output; } } }
Step 3 - Create a Numbers_List.txt file
Numbers_List.txt
10 12 23 55 66 77 88 99 22 13 16
$ cd $HIVE_HOME/bin
$ hive
hive> CREATE TABLE Num_list(Num int) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\n';
hive> LOAD DATA LOCAL INPATH '/home/hduser/Desktop/HIVE/Numbers_List.txt' OVERWRITE INTO TABLE Num_list;
hive> SELECT * FROM Num_list;
hive> ADD JAR /home/hduser/Desktop/HIVE/MaxUDAF.jar;
hive> CREATE TEMPORARY FUNCTION max AS 'Max';
hive> SELECT max(Num) FROM Num_list;
Comments
Post a Comment