利用aws构建数据仓库(三):编写hive的udf

本文介绍在利用EMR的Hive构建数据仓库时编写用户自定义函数UDF的方法。因从Maven中央仓库拉取jar包编写UDF在执行引擎为TEZ时会报错,故从EMR的Hive安装目录获取hive - exec的jar包,详细阐述了下载、引入、创建UDF、打包、上传及测试等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.

       在利用EMR的hive构建数据仓库时,需要编写用户自定义函数udf。在实际的经验中,根据EMR的hive版本选择从Maven中央仓库中拉取所需要的jar包这种方式,是存在问题的。采用Maven仓库拉取的jar包编写的udf函数放入hive中,在执行引擎为mapreduce时没有问题,但是一旦将执行引擎换成TEZ,就会报错。

所以,我从EMR的hive安装目录中找到hive-exe这个jar包放入java工程中。

2.登录EMR集群的主节点(Master),因为Hive是安装在EMR集群的主节点上的。

ssh -i ~/Downloads/dfwarehouse-test.pem hadoop@ec2-54-169-197-246.ap-southeast-1.compute.amazonaws.com

3.执行命令切换到root用户:

sudo -s

4.搜索编写hive的udf所需要的jar包,执行命令:

 find / -name "hive-exec*"

5.在/usr/lib/hive/lib下面有2个hive-exec的jar包,那我们应该下载哪个呢?执行命令

ls -lh /usr/lib/hive/lib/hive-exec*

发现实际上只有/usr/lib/hive/lib/hive-exec-2.3.3-amzn-1.jar这一个真实的jar包

6.下载/usr/lib/hive/lib/hive-exec-2.3.3-amzn-1.jar。在Mac电脑的命令行执行如下命令,将jar包下载到Documents目录:

scp -i ~/Downloads/dfwarehouse-test.pem  hadoop@ec2-54-169-197-246.ap-southeast-1.compute.amazonaws.com:/usr/lib/hive/lib/hive-exec-2.3.3-amzn-1.jar ~/Documents/

7.创建Maven工程,引入依赖,这个时候Maven肯定会找不到这个jar包

<dependency>

<groupId>org.apache.hive</groupId>

<artifactId>hive-exec</artifactId>

<version>2.3.3-amzn-1</version>

</dependency>

8.在自己的Maven仓库中进入org/apache/hive目录找到2.3.3-amzn-1,进入2.3.3-amzn-1目录后,删除里面的内容,并将下载的hive-exec-2.3.3-amzn-1.jar复制到这个目录

9.在eclipse的这个Maven项目上执行Maven -- update project 。这个时候创建的Maven工程就能找到我们下载的jar包了。

10.创建一个转换字符串为大写的udf

package dfwarehouse.udf;

import org.apache.commons.lang3.StringUtils;

import org.apache.hadoop.hive.ql.exec.UDF;

public class MyUdf extends UDF {

//将输入字符串转为大写的udf

public String evaluate(String str) {

if (StringUtils.isBlank(str)) {

return str;

}

str = str.toUpperCase();

return str;

}

}

11.Maven的pom中引入打jar包工具

<build>

<finalName>dfwarehouse</finalName>

<resources>

<resource>

<directory>src/main/java</directory>

</resource>

</resources>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-shade-plugin</artifactId>

<version>2.3</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>

12.在eclipse中run as --> maven build中输入clean package 进行打包。在target目录下生产2个jar包,dfwarehouse.jar是包含依赖的jar包。

13.将dfwarehouse.jar包上传到EMR集群的主节点。

14.在EMR的主节点执行hive命令,登录到hive数据库

15.创建临时函数

add jar /home/hadoop/dfwarehouse.jar;

create temporary function myudf as 'dfwarehouse.udf.MyUdf';

16.测试编写的udf

SELECT myudf("hello world") ;

成功

 

 


附录:

pom.xml

​
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>df</groupId>
	<artifactId>dfwarehouse</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>dfwarehouse</name>
	<url>http://maven.apache.org</url>

	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>

		<dependency>
			<groupId>org.apache.hive</groupId>
			<artifactId>hive-exec</artifactId>
			<version>2.3.3-amzn-1</version>
		</dependency>



	</dependencies>
	 
	<build>
		<finalName>dfwarehouse</finalName>
		<resources>
			<resource>
				<directory>src/main/java</directory>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-shade-plugin</artifactId>
				<version>2.3</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>
</project>

​

MyUdf.java

package dfwarehouse.udf;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.hive.ql.exec.UDF;

public class MyUdf extends UDF {
	//将输入字符串转为大写的udf
	public String evaluate(String str) {
		if (StringUtils.isBlank(str)) {
			return str;
		}
		str = str.toUpperCase();

		return str;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值