@zhangyy
2016-05-20T11:13:10.000000Z
字数 2175
阅读 185
大数据系列
- 1)依据课程讲解UDF编程案例,完成练习,总结开发UDF步骤,代码贴图,给予注释,重点
- 2)更改emp 表中名字的大写给为小写。
cd .m2/
mv repository repository.bak
上传新的repository.tar.gz 包。
tar -zxvf repository.tar.gz
备份原有setting.xml 文件
cp -p setting.xml setting.xml.bak
cd /home/hadoop/yangyang/maven/conf
cp -p setting.xml setting.xml
更改maven源的配置:
在setting.xml 中<mirrors> ....</mirrors> 之间增加新的源仓库:
<mirror>
<id>nexus-osc</id>
<mirrorOf>central</mirrorOf>
<name>Nexus osc</name>
<url>http://maven.oschina.net/content/groups/public/</url>
</mirror>
拷贝新的setting文件到maven 的配置文件中
cp -p .m2/setting.xml /home/hadoop/yangyang/maven/conf
更改eclipse的pom.xml 文件增加:
在原有的<dependencies>....</dependencies> 之间加上hive 的参数:
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>0.13.1</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>0.13.1</version>
</dependency>
</dependencies>
新建UDF包:
编写lowerudf.java
package org.apache.hadoop.udf;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
/**
* New UDF classes need to inherit from this UDF class.
*/
public class LowerUDF extends UDF{
/**
* 1. Implement one or more methods named "evaluate" which will be called by Hive.
*
* 2. "evaluate" should never be a void method. However it can return "null" if needed.
*/
public Text evaluate(Text str){
// input parameter validate
if(null == str){
return null ;
}
// validate
if(StringUtils.isBlank(str.toString())){
return null ;
}
// lower
return new Text(str.toString().toLowerCase()) ;
}
public static void main(String[] args) {
System.out.println(new LowerUDF().evaluate(new Text()));
}
}
导出jar包 到/home/hadoop/yangyang/hive/jars 下面:
执行lowerudf 包:
增加jar包与hive的关联:
add jar /home/hadoop/yangyang/hive/jars/lowerudf.jar
create temporary function my_lower as 'org.apache.hadoop.udf.LowerUDF' ;
show functions;
销毁临时的udf 函数:
drop temporary function add_example;
<!--这里指my_lower 函数-->
执行my_lower 函数:
select my_lower(ename) from emp;