JAVA maven 编写UDF适用于hive和impala

2023-11-04

hive 内置函数很少,我们可以通过自定义的方式添加新的UDF上去,来增强hive的处理能力。

比如hive没有字符串包含的UDF.

我们通过Java+maven的方式来编写一个字符串包含的UDF

1、新建maven工程


2、修改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>com.lr.udf</groupId>
	<artifactId>common.udf</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<properties>
		<java.source.version>1.8</java.source.version>
		<java.target.version>1.8</java.target.version>
		<maven.compiler.plugin.version>3.3</maven.compiler.plugin.version>
		<file.encoding>UTF-8</file.encoding>
		<hadoop.version>2.6.0</hadoop.version>
	</properties>


	<dependencies>
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-client</artifactId>
			<version>2.7.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.hive</groupId>
			<artifactId>hive-exec</artifactId>
			<version>1.2.1</version>
			<exclusions>
				<exclusion>
					<groupId>org.pentaho</groupId>
					<artifactId>pentaho-aggdesigner-algorithm</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>
	
	<build>
		<finalName>${project.artifactId}</finalName>
	</build>
</project>

3、编写UDF类

package common.udf;


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


/**
 * 需要继承UDF
 * @author liurui
 *
 */
public class StringUDF extends UDF{


	/**
	 * 重写evaluate方法,参数可以任意
	 * @param orgStr
	 * @param dest
	 * @return
	 */
	public Integer evaluate(String orgStr,String dest){
		if(orgStr.contains(dest)){
			return 1;
		}
		return 0;
	}
	
	public static void main(String[] args) {
		StringUDF udf = new StringUDF();
		System.out.println(udf.evaluate("aaabc", "b"));
	}
}

4、打包

mvn clean package

5、上传到hdfs

hdfs dfs -put common.udf.jar /tmp/test/common.udf.jar

[root@bigdata-cdh001 home]# ll
total 4
drwx------. 2 centos-user centos-user   62 Aug 25  2017 centos-user
-rw-r--r--  1 root        root        2518 May 22 18:56 common.udf.jar

[root@bigdata-cdh001 home]# hadoop dfs -ls /tmp/test
DEPRECATED: Use of this script to execute hdfs command is deprecated.
Instead use the hdfs command for it.
Found 1 items
drwxr-xr-x   - hdfs supergroup          0 2018-04-10 15:11 /tmp/test/8a18f7a6b63a4b8180b242e448cea705

[root@bigdata-cdh001 home]# hdfs dfs -put common.udf.jar /tmp/test/common.udf.jar
[root@bigdata-cdh001 home]# hadoop dfs -ls /tmp/test
DEPRECATED: Use of this script to execute hdfs command is deprecated.
Instead use the hdfs command for it.
Found 2 items
drwxr-xr-x   - hdfs supergroup          0 2018-04-10 15:11 /tmp/test/8a18f7a6b63a4b8180b242e448cea705
-rw-r--r--   3 root supergroup       2819 2018-05-21 22:34 /tmp/test/csot-hive-udf.jar

6、将UDF添加到hive

#将jar添加到hive

add jar hdfs:///tmp/test/common.udf.jar

#创建临时的UDF

create temporary function my_contains as 'common.udf.StringUDF';

#创建永久的只需要去掉temporary

[root@bigdata-cdh001 home]# hive
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=512M; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=512M; support was removed in 8.0

Logging initialized using configuration in jar:file:/opt/cloudera/parcels/CDH-5.10.1-1.cdh5.10.1.p0.10/jars/hive-common-1.1.0-cdh5.10.1.jar!/hive-log4j.properties
WARNING: Hive CLI is deprecated and migration to Beeline is recommended.
hive> add jar hdfs:///tmp/test/common.udf.jar;
converting to local hdfs:///tmp/test/common.udf.jar
Added [/tmp/6eaaaad7-1446-4bed-9543-0a02d901e8bc_resources/common.udf.jar] to class path
Added resources: [hdfs:///tmp/test/common.udf.jar]
hive> create temporary function my_contains as 'common.udf.StringUDF';
OK
Time taken: 1.692 seconds
hive> 
    > SELECT * FROM default.i_f_task_status where my_contains(task_id,'c')=0 LIMIT 5;
OK
1	100
1	100
1	100
1	100
1	100
Time taken: 0.109 seconds, Fetched: 5 row(s)
hive> 

7、查询task_id不包含字符c的 

my_contains(task_id,'c')=0 不包含  my_contains(task_id,'c')=1 包含
SELECT * FROM default.i_f_task_status where my_contains(task_id,'c')=0 LIMIT 5;



在hive添加的udf 在impala是不支持的,所以需要另外再加

如果是impala,创建函数的时候需要指定参数和返回值类型,如下:

则在impala-shell或者hue的impala上执行

create function csot_contains(string,string) returns int location 'hdfs:///tmp/test/common.udf.jar' symbol='cn.com.cloudstar.udf.StringUDF';
[bigdata-cdh001:21000] > create function my_contains(string,string) returns int location 'hdfs:///tmp/test/common.udf.jar' symbol='common.udf.StringUDF';
Query: create function my_contains(string,string) returns int location 'hdfs:///tmp/test/common.udf.jar' symbol='common.udf.StringUDF'

Fetched 0 row(s) in 0.07s
[bigdata-cdh001:21000] > 
[bigdata-cdh001:21000] > SELECT * FROM default.i_f_task_status where my_contains(task_id,'c')=0 LIMIT 5;
Query: select * FROM default.i_f_task_status where my_contains(task_id,'c')=0 LIMIT 5
Query submitted at: 2018-05-22 19:19:09 (Coordinator: http://bigdata-cdh001:25000)
Query progress can be monitored at: http://bigdata-cdh001:25000/query_plan?query_id=34f236d41e4812c:9f1e208500000000
+---------+---------+
| process | task_id |
+---------+---------+
| 1       | 100     |
| 1       | 100     |
| 1       | 100     |
| 1       | 100     |
| 1       | 100     |
+---------+---------+
Fetched 5 row(s) in 4.55s
[bigdata-cdh001:21000] > 

源码:https://gitee.com/qianqianjie/common.udf.git










本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JAVA maven 编写UDF适用于hive和impala 的相关文章

随机推荐

  • CNN提取图片特征,之后用SVM分类

    https blog csdn net qq 27756361 article details 80479278 先用CNN提取特征 之后用SVM分类 平台是TensorFlow 1 3 0 rc0 python3 6 这个是我的一个小小的
  • SpringBoot线程池使用详解

    前提摘要 基于Springboot 2 1 4 RELEASE 配置TaskExecutor import java util concurrent ThreadPoolExecutor import org springframework
  • c++实现串口通讯踩坑(argument of type “char *“ is incompatible with parameter of type “LPCWSTR“)

    在C下 可以使用outportb和inportb进行串口通讯 C 没有这两个函数 那就使用createfile吧 通过网上搜到读取打开串口的例子 如下 include
  • 初探Spring Data Elasticsearch

    个人博客 dogbin vip Spring Data Elasticsearch 介绍 Spring Data Elasticsearch 基于 spring data API 简化 Elasticsearch 操作 将原始操作 Elas
  • Steam游戏《TownScaper》技术分析!

    TownScaper是今年6月份上线Steam的建造类游戏 不过它的作者已经在twitter做了一年多的游戏开发日志 结合2019年欧洲独立游戏大会的一场演讲 EPC2018一场演讲 输出一下这个游戏的技术要点 首先作者从业经验非常令人羡慕
  • 计算阶乘的两种实现方式(Java)

    本文以计算5的阶乘为例 5 5 4 3 2 1 120 一 循环阶乘 1 While循环 public class TestWhileFactorial public static void main String args int res
  • 数字图像处理:局部直方图处理(Local Histogram Processing)

    前往老猿Python博文目录 https blog csdn net LaoYuanPython 一 引言 在前面章节 数字图像处理 直方图均衡学习总结 感悟 数字图像直方图匹配或规定化Histogram Matching Specific
  • 【C++】struct VS class

    文章目录 面向过程 面向对象 C 的struct和class 1 和C的struct的小差异 2 访问权限符 3 类的大小 类的存储 4 成员函数如何区分对象 5 struct和class的区别 6 this指针的两个面试题 一 this存
  • CMake 学习笔记(target_compile_features())

    CMake 学习笔记 target compile features 这一篇博客讲一讲target compile features 这条命令时 CMake 3 1 引入的 在这个之前 如果我们要设置C 编译开启 C 11 的支持 需要用如
  • SourceInsight修改暗灰色护眼主题(模仿vscode和sublime text的风格)

    SourceInsight护眼主题 仿vscode和sublime text 一 仿vscode 二 仿sublime text 三 自己动手 丰衣足食 1 载入主题 2 自制或修改主题 一 仿vscode 效果图 主题文件下载链接 htt
  • 配置MQTT

    MQTT 文章目录 MQTT 快速上手 源码安装mosquitto 配置Broker SDK调用接口 Python NodeJs WebJs Java C 快速上手 安装Broker服务 apt install mosquitto mosq
  • 泊松分布与泊松回归模型

    泊松分布 Poisson分布 法语 loi de Poisson 英语 Poisson distribution 译名有泊松分布 普阿松分布 卜瓦松分布 布瓦松分布 布阿松分布 波以松分布 卜氏分配等 是一种统计与概率学里常见到的离散概率分
  • Go 语言面试题(二):实现原理

    文章目录 Q1 init 函数是什么时候执行的 Q2 Go 语言的局部变量分配在栈上还是堆上 Q3 2 个 interface 可以比较吗 Q4 两个 nil 可能不相等吗 Q5 简述 Go 语言GC 垃圾回收 的工作原理 Q6 函数返回局
  • Android RxJava:功能性操作符 全面讲解

    前言 Rxjava 由于其基于事件流的链式调用 逻辑简洁 使用简单的特点 深受各大 Android开发者的欢迎 Github截图 如果还不了解 RxJava 请看文章 Android 这是一篇 清晰 易懂的Rxjava 入门教程 RxJav
  • Python特点及优势

    Python介绍 Python由荷兰数学和计算机科学研究学会的吉多 范罗苏姆 于1990 年代初设计 作为一门叫做ABC语言的替代品 Python提供了高效的高级数据结构 还能简单有效地面向对象编程 Python语法和动态类型 以及解释型语
  • [远程办公] 通过阿里云反向代理实现内网穿透

    准备工作 购买一台阿里云服务器 新用户第一个月免费 由于仅作数据转发用 配置要求不高 除了传输带宽外 其余配置选最低的就行 内网主机 生成ssh密匙 ssh keygen 与远程主机建立免密登录ssh copy id root ip 安装依
  • docker安装tomcat,以及常用操作

    1 安装tomcat镜像 访问docker hub仓库找出你想安装的版本 docker hub 网址 https registry hub docker com 直接在搜索框里搜tomcat就可以 如果想把自己镜像放到上边就自己注册一个账号
  • Docker 搭建sonarqube,并集成阿里P3C规则

    简介 本文安装的sonarqube是7 6 community版本 未安装最新版是因为7 9之后不再支持mysql 如果你安装的是其他版本的sonarqube 那么不要使用插件包中的插件 会有版本兼容性问题 插件 插件包 插件包中包含jav
  • Android:Action 与 Data 属性

    前言 Intent 的中文翻译就是 意图 的意思 它是 Android 程序中传输数据的核心对象 在 Android 官方文档中 对 Intent 的定义是执行某操作的一个抽象描述 一个 Intent 对象实质上是一组被捆绑的信息 它可以是
  • JAVA maven 编写UDF适用于hive和impala

    hive 内置函数很少 我们可以通过自定义的方式添加新的UDF上去 来增强hive的处理能力 比如hive没有字符串包含的UDF 我们通过Java maven的方式来编写一个字符串包含的UDF 1 新建maven工程 2 修改pom xml