Hive3 detailed tutorial (VIII) hive3 custom UDF function (elipse version)

Miss Zhu 2022-02-13 08:38:43 阅读数:70

hive3 hive detailed tutorial viii

because Hive Itself is Java Developed , So we can use it Java Define functions for Hive SQL Use .
Let's explore by defining a case of converting an input string into reverse output UDF Function customization .

stay Eclipse Next to add jar Package development :
need :hadoop-common-3.2.0.jar ( stay hadoop In the installation file )、hive-exec-3.1.2.jar package ( stay hive In the installation file )
Project structure :
 Insert picture description here
Java Simple code , Realize the inverse output of a string :

package test1;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
@SuppressWarnings("deprecation")
public class RevertString extends UDF {

public Text evaluate(Text line) {

if (null != line && !line.toString().equals("")) {

String str = line.toString();
char[] rs = new char[str.length()];
for (int i = str.length() - 1; i >= 0; i--) {

rs[str.length() - 1 - i] = str.charAt(i);
}
line.set(new String(rs));
return line;
}
line.set("");
return line;
}
}

After code development , Only for this type jar Bag can :
 Insert picture description here
take jar The package is named testHive-e1.jar And then upload it to linux In the virtual machine :

stay beeline The following command is used in jar Add to hive in :

add jar /apps/testHive-e1.jar;

Defined function , Set the associated Java class :

create temporary function my_revert_e1 as 'test1.RevertString';

test :

select my_revert_e1('abc');

 Insert picture description here
View all added jar:
 Insert picture description here

Look at all the functions :

Show functions
copyright:author[Miss Zhu],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130838407247.html