User-Defined Functions in hive

Source: Internet
Author: User

The default hive functions are not completely complete. We must add some additional functions for future use.
The following example is a simple test about user-defined functions.

Function Code
Package com. example. hive. UDF;

Import org.apache.hadoop.hive.ql.exe C. UDF;
Import org. Apache. hadoop. Io. text;

Public final class lower extends UDF {
Public text evaluate (Final Text S ){
If (S = NULL) {return NULL ;}
Return new text (S. tostring (). tolowercase ());
}
}

Package
Javac-D Lower. Java
Jar-CVF lower. jar-C lower /.

Add a package in hive
Hive> Add JAR/home/hjl/sunwg/lower. jar;
Added/home/hjl/sunwg/lower. jar to class path

Create a function in hive
Hive> create temporary function my_lower as 'com. example. hive. UDF. Lower ';
OK
Time taken: 0.407 seconds

Use Functions
Hive> select my_lower (name) from test10;

 

The user-defined functions in hive are described above. Some functions are relatively basic and common. It is too troublesome to create the temporary function every time.
Such basic functions need to be directly integrated into hive to avoid creation every time.

1. Add the function File $ hive_home/src/QL/src/Java/org/Apache/hadoop/hive/QL/UDF/udfsunwg. java.
Package org. Apache. hadoop. hive. QL. UDF;

Import org.apache.hadoop.hive.ql.exe C. description;
Import org.apache.hadoop.hive.ql.exe C. UDF;
Import org. Apache. hadoop. Io. intwritable;
Import org. Apache. hadoop. Io. text;

Public final class udfsunwg extends UDF {
Public text evaluate (Final Text S ){
If (S = NULL) {return NULL ;}
Return new text (S. tostring (). Concat ("sunwg"). tolowercase ());
}
}

2. register the function sunwg to the hive function list.
Modify the $ hive_home/src/QL/src/Java/org/Apache/hadoop/hive/QL/exec/functionregistry. Java File
Import org. Apache. hadoop. hive. QL. UDF. udfsunwg;

Registerudf ("sunwg", udfsunwg. Class, false );

3. Test the sunwg function.
Hive> select sunwg ("ABC") from sunwg00 limit 1;
Total mapreduce jobs = 1
Launching job 1 out of 1
Number of reduce tasks is set to 0 since there's no reduce Operator
Starting job = job_201104091858_381437, tracking url = http: // hdpjt: 50030/jobdetails. jsp? Jobid = job_201104091858_381437
Kill command =/home/dwapp/hadoop/bin/../bin/hadoop job-dmapred. Job. Tracker = hdpjt: 9001-kill job_201104091858_381437
16:01:28, 733 stage-1 Map = 0%, reduce = 0%
16:01:34, 123 stage-1 Map = 50%, reduce = 0%
16:01:35, 543 stage-1 Map = 100%, reduce = 0%
Ended job = job_201104091858_381437
OK
Abcsunwg
Time taken: 48.53 seconds

Conclusion: It is so easy to add functions in hive. We recommend that you add common basic functions to hive, while some personalized functions still create temporary functions.

The default hive functions are not completely complete. We must add some additional functions for future use.
The following example is a simple test about user-defined functions.

Function Code
Package com. example. hive. UDF;

Import org.apache.hadoop.hive.ql.exe C. UDF;
Import org. Apache. hadoop. Io. text;

Public final class lower extends UDF {
Public text evaluate (Final Text S ){
If (S = NULL) {return NULL ;}
Return new text (S. tostring (). tolowercase ());
}
}

Package
Javac-D Lower. Java
Jar-CVF lower. jar-C lower /.

Add a package in hive
Hive> Add JAR/home/hjl/sunwg/lower. jar;
Added/home/hjl/sunwg/lower. jar to class path

Create a function in hive
Hive> create temporary function my_lower as 'com. example. hive. UDF. Lower ';
OK
Time taken: 0.407 seconds

Use Functions
Hive> select my_lower (name) from test10;

 

The user-defined functions in hive are described above. Some functions are relatively basic and common. It is too troublesome to create the temporary function every time.
Such basic functions need to be directly integrated into hive to avoid creation every time.

1. Add the function File $ hive_home/src/QL/src/Java/org/Apache/hadoop/hive/QL/UDF/udfsunwg. java.
Package org. Apache. hadoop. hive. QL. UDF;

Import org.apache.hadoop.hive.ql.exe C. description;
Import org.apache.hadoop.hive.ql.exe C. UDF;
Import org. Apache. hadoop. Io. intwritable;
Import org. Apache. hadoop. Io. text;

Public final class udfsunwg extends UDF {
Public text evaluate (Final Text S ){
If (S = NULL) {return NULL ;}
Return new text (S. tostring (). Concat ("sunwg"). tolowercase ());
}
}

2. register the function sunwg to the hive function list.
Modify the $ hive_home/src/QL/src/Java/org/Apache/hadoop/hive/QL/exec/functionregistry. Java File
Import org. Apache. hadoop. hive. QL. UDF. udfsunwg;

Registerudf ("sunwg", udfsunwg. Class, false );

3. Test the sunwg function.
Hive> select sunwg ("ABC") from sunwg00 limit 1;
Total mapreduce jobs = 1
Launching job 1 out of 1
Number of reduce tasks is set to 0 since there's no reduce Operator
Starting job = job_201104091858_381437, tracking url = http: // hdpjt: 50030/jobdetails. jsp? Jobid = job_201104091858_381437
Kill command =/home/dwapp/hadoop/bin/../bin/hadoop job-dmapred. Job. Tracker = hdpjt: 9001-kill job_201104091858_381437
16:01:28, 733 stage-1 Map = 0%, reduce = 0%
16:01:34, 123 stage-1 Map = 50%, reduce = 0%
16:01:35, 543 stage-1 Map = 100%, reduce = 0%
Ended job = job_201104091858_381437
OK
Abcsunwg
Time taken: 48.53 seconds

Conclusion: It is so easy to add functions in hive. We recommend that you add common basic functions to hive, while some personalized functions still create temporary functions.

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.