maven导入jar包
<repositories>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0-cdh5.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>1.1.0-cdh5.14.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*/RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
-------------------------------------------主程序代码------------------------------------
package com.cn;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
/**
* 自定义函数写法比较简单,继承UDF类,重写evaluate函数,入参为hsql的表字段,K2
*/
public class UpperFunctionDemo extends UDF {
/**
* 将字段转换为大写,以Text返回
*/
public Text evaluate(Text text){
String result = null;
if (text != null){
result = text.toString().toUpperCase();
return new Text(result);
}else{
return null;
}
}
}
----------------------------------java代码编写完成的后续操作-------------------------------
1.将项目打包放到hive的lib目录
2.重命名我们的jar包名称(可有可无,保持包的名字规范即可)
cd /export/servers/hive-1.1.0-cdh5.14.0/lib
mv original-day_06_hive_udf-1.0-SNAPSHOT.jar udf.jar
3.hive的客户端添加我们的jar包
add jar /export/servers/hive-1.1.0-cdh5.14.0/lib/udf.jar;
4.在hive客户端内创建自定义函数,指定对应jar包,以及入口类
create temporary function tolowercase as 'cn.itcast.udf.ItcastUDF';
5.测试自定义函数
select tolowercase('abc');
