Hive用户自定义函数

UDF:用户自定义函数,User Defined Function

编程步骤:

1、继承org.apache.hadoop.hive.ql.UDF

2、需要实现evaluate函数;evaluate函数支持重载;

注意事项:

1、UDF必须要有返回值,可以返回null,但是返回类型不能为void;

2、UDF中常用Text/LongWritable等类型,不推荐使用java类型;

例子:

1、实现大小写转换

2、pox.xml如下:

该如何配置参考: http://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client/2.6.0-cdh5.14.2

<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>zzw</groupId>
	<artifactId>bigdata-hadoop</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>bigdata-hadoop</name>

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

	<repositories>
		<repository>
			<id>cloudera</id>
			<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
		</repository>
	</repositories>

	<dependencies>
		<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-client</artifactId>
			<version>2.6.0-cdh5.14.2</version>
		</dependency>

		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-common</artifactId>
			<version>2.6.0-cdh5.14.2</version>
		</dependency>

		<dependency>
			<groupId>org.apache.hive</groupId>
			<artifactId>hive-exec</artifactId>
			<version>1.1.0-cdh5.14.2</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc -->
		<dependency>
			<groupId>org.apache.hive</groupId>
			<artifactId>hive-jdbc</artifactId>
			<version>1.1.0-cdh5.14.2</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

3、将hive-site.xml文件放到Eclipse项目中,便于读取配置:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->

<configuration>
	
	  <!--指定连接mysql数据库的主机名称以及端口号,还有新建数据库的名称-->
	  <property>
		<name>javax.jdo.option.ConnectionURL</name>
		<value>jdbc:mysql://master.cdh.com:3306/cdhmetastore?createDatabaseIfNotExist=true</value>
		<description>JDBC connect string for a JDBC metastore</description>
	  </property>
	  
	  <!--指定连接mysql的驱动-->
	  <property>
		<name>javax.jdo.option.ConnectionDriverName</name>
		<value>com.mysql.jdbc.Driver</value>
		<description>Driver class name for a JDBC metastore</description>
	  </property>
	  
	  <!--指定连接mysql的用户名和密码-->
	  <property>
		<name>javax.jdo.option.ConnectionUserName</name>
		<value>root</value>
		<description>Username to use against metastore database</description>
	  </property>
	  
	  <property>
		<name>javax.jdo.option.ConnectionPassword</name>
		<value>123456</value>
		<description>password to use against metastore database</description>
	  </property>
	  
	  <property>
		<name>hive.cli.print.header</name>
		<value>true</value>
		<description>Whether to print the names of the columns in query output.</description>
	  </property>

	  <property>
		<name>hive.cli.print.current.db</name>
		<value>true</value>
		<description>Whether to include the current database in the Hive prompt.</description>
	  </property>
    
	  <!--设置每个reduce处理的数据量-->
	  <property>
		<name>hive.exec.reducers.bytes.per.reducer</name>
		<value>1000000000</value>
		<description>size per reducer.The default is 256Mb, i.e if the input size is 1G, it will use 4 reducers.</description>
	  </property>
	  
	  <!--设置最大能够运行的reduce个数-->
      <property>
		<name>hive.exec.reducers.max</name>
		<value>999</value>
		<description>
		max number of reducers will be used. If the one specified in the configuration parameter mapred.reduce.tasks is
		negative, Hive will use this one as the max number of reducers when automatically determine number of reducers.
		</description>
      </property>
	  
</configuration>

4、创建类:本来想继承UDF类,却提示该类已经过时。

根据UDF的源码中的提示,应该用GenericUDF

具体代码如下:

 

待续...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值