Hive3 detailed tutorial (IX) hive3 custom UDF function (idea Maven version)

Miss Zhu 2022-02-13 08:38:41 阅读数:165

hive3 hive detailed tutorial ix

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 idea Use in maven Development , The project structure is as follows :
 Insert picture description here

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">
<!-- FIXME change it to the project's website -->
<!-- -->
<!-- -->
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->

Realize the inverse output of a string :

Java Code :

package com.alan;
import org.apache.hadoop.hive.ql.exec.UDF;
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;
return line;

Type as including dependencies jar package :
 Insert picture description here

Upload to linux In the virtual machine
 Insert picture description here

Add to hive in :

add jar /apps/testHive-v2.jar;

Set the function name , Associated class name

create temporary function my_revert_v2 as 'com.alan.RevertString';

test :

select my_revert_v2('abc');

 Insert picture description here

copyright:author[Miss Zhu],Please bring the original link to reprint, thank you.