---
title: "编写hive udf函数"
author: "thomas0988"
date: "2012-05-04T19:13:32Z"
tags: []
categories: ["数据库"]
slug: "hive-udf"
draft: false
lastmod: "2012-05-04T19:13:32Z"
id: 1511913
url: "https://thomas0988.iteye.com/blog/1511913"
---

udf编写入门  
大写转小写  
package com.afan;  
import org.apache.hadoop.hive.ql.exec.UDF;  
import org.apache.hadoop.io.Text;

public class UDFLower extends UDF{  
    public Text evaluate(final Text s){  
        if (null == s){  
            return null;  
        }  
        return new Text(s.toString().toLowerCase());  
    }  
}

1加载udf jar包  
afan@ubuntu:/usr/local/hadoop/hive$ bin/hive  
Hive history file=/tmp/afan/hive\_job\_log\_afan\_201105150623\_175667077.txt  
hive&gt; add jar udf\_hive.jar;  
Added udf\_hive.jar to class path  
Added resource: udf\_hive.jar  
2 创建udf函数  
hive&gt; create temporary function my\_lower as 'com.afan.UDFLower';  
OK  
Time taken: 0.253 seconds  
3 创建测试数据  
hive&gt; create table dual (info string);  
OK  
Time taken: 0.178 seconds  
hive&gt; load data local inpath 'data.txt' into table dual;  
Copying data from file:/usr/local/hadoop/hive/data.txt  
Copying file: file:/usr/local/hadoop/hive/data.txt  
Loading data to table default.dual  
OK  
Time taken: 0.377 seconds  
hive&gt; select info from dual;  
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\_201105150525\_0003, Tracking URL = http://localhost:50030/jobdetails.jsp?jobid=job\_201105150525\_0003  
Kill Command = /usr/local/hadoop/bin/../bin/hadoop job  -Dmapred.job.tracker=localhost:9001 -kill job\_201105150525\_0003  
2011-05-15 06:46:05,459 Stage-1 map = 0%,  reduce = 0%  
2011-05-15 06:46:10,905 Stage-1 map = 100%,  reduce = 0%  
2011-05-15 06:46:13,963 Stage-1 map = 100%,  reduce = 100%  
Ended Job = job\_201105150525\_0003  
OK  
WHO  
AM  
I  
HELLO  
worLd

Time taken: 14.874 seconds  
4使用udf函数  
hive&gt; select my\_lower(info) from dual;  
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\_201105150525\_0002, Tracking URL = http://localhost:50030/jobdetails.jsp?jobid=job\_201105150525\_0002  
Kill Command = /usr/local/hadoop/bin/../bin/hadoop job  -Dmapred.job.tracker=localhost:9001 -kill job\_201105150525\_0002  
2011-05-15 06:43:26,100 Stage-1 map = 0%,  reduce = 0%  
2011-05-15 06:43:34,364 Stage-1 map = 100%,  reduce = 0%  
2011-05-15 06:43:37,484 Stage-1 map = 100%,  reduce = 100%  
Ended Job = job\_201105150525\_0002  
OK  
who  
am  
i  
hello  
world

Time taken: 20.834 seconds

[http://blog.sina.com.cn/s/blog\_61c463090100rh4j.html](http://blog.sina.com.cn/s/blog_61c463090100rh4j.html)