Hive自定义函数(结合Idea)_idea怎样在本地调试hive的自定义函数-程序员宅基地

技术标签: hive  Hive  hadoop  

一 自定义UDF函数

1)创建一个 Maven 工程 Hive
2)导入依赖

<dependencies>
<!--https://mvnrepository.com/artifact/org.apache.hive/hive-exec-->
	<dependency>
		<groupId>org.apache.hive</groupId>
		<artifactId>hive-exec</artifactId>
		<version>1.2.1</version>
	</dependency>
</dependencies>

3)创建一个类

import org.apache.hadoop.hive.ql.exec.UDF;
public class Lower extends UDF {
    
	public String evaluate (String s) {
    
	if (s == null) {
    
		return null;
	}
	return s.toLowerCase();
	}
}

4)打成 jar 包上传到服务器/opt/module/datas/udf.jar
5)将 jar 包添加到 hive 的 classpath

hive (default)> add jar /opt/module/datas/udf.jar;

6)创建临时函数与开发好的 java class 关联

hive (default)> create temporary function mylower as
"Lower";

7.即可在 hql 中使用自定义的函数

hive (default)> select ename, mylower(ename) lowername from emp;

二 自定义 UDTF 函数

import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDTF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
import java.util.ArrayList;
import java.util.List;

public class MyUDTF extends GenericUDTF {
    
 private ArrayList<String> outList = new ArrayList<>();
 @Override
 public StructObjectInspector initialize(StructObjectInspector
argOIs) throws UDFArgumentException {
    
 //1.定义输出数据的列名和类型
 List<String> fieldNames = new ArrayList<>();
 List<ObjectInspector> fieldOIs = new ArrayList<>();
 //2.添加输出数据的列名和类型
 fieldNames.add("lineToWord");

fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInsp
ector);
 return
ObjectInspectorFactory.getStandardStructObjectInspector(fieldName
s, fieldOIs);
 }
 @Override
 public void process(Object[] args) throws HiveException {
    

 //1.获取原始数据
 String arg = args[0].toString();
 //2.获取数据传入的第二个参数,此处为分隔符
 String splitKey = args[1].toString();
 //3.将原始数据按照传入的分隔符进行切分
 String[] fields = arg.split(splitKey);
 //4.遍历切分后的结果,并写出
 for (String field : fields) {
    
 //集合为复用的,首先清空集合
 outList.clear();
 //将每一个单词添加至集合
 outList.add(field);
 //将集合内容写出
 forward(outList);
 }
 }
 @Override
 public void close() throws HiveException {
    
 }
}

3)打成 jar 包上传到服务器/opt/module/data/udtf.jar
4)将 jar 包添加到 hive 的 classpath 下

hive (default)> add jar /opt/module/data/udtf.jar;

5)创建临时函数与开发好的 java class 关联

hive (default)> create temporary function myudtf as
"MyUDTF";

6)即可在 hql 中使用自定义的函数

hive (default)> select myudtf(line, ",") word from words;
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_43497444/article/details/104789783

智能推荐

SQL Server修改数据-程序员宅基地

文章浏览阅读1.6w次,点赞2次,收藏22次。SQL Server 修改数据的相关语句

前端工程与性能优化-程序员宅基地

文章浏览阅读881次。前端工程与性能优化 · Issue #3 · fouber/blog https://github.com/fouber/blog/issues/3

yolov5测试单张图片-程序员宅基地

文章浏览阅读4.1k次,点赞2次,收藏20次。yolov5测试单张图片,返回一个列表[类别,置信度,x,y,w,h]from numpy import randomimport torchfrom models.experimental import attempt_loadfrom utils.datasets import LoadStreams, LoadImagesfrom utils.general import ( check_img_size, non_max_suppression, apply_classifier_yolov5测试单张图片

SQL做的能改成Oracle吗,从SQL改写到SQL重写,什么样的SQL才是好SQL?(黄浩)-程序员宅基地

文章浏览阅读88次。从SQL改写到SQL重写,什么样的SQL才是好SQL?黄浩 2016-12-14 10:02:26作者介绍黄浩,现任职于中国惠普,从业十年,始终专注于SQL。十年一剑,十年磨砺。3年通信行业,写就近3万条SQL;5年制造行业,遨游在ETL的浪潮;2年性能优化,厚积薄发自成一家。在生活中,很多时候我们会有这样的体悟:问题要么不出,一旦出现,会像多诺米骨牌一样,会连锁引发诸多相关问题,让我们疲于应付。..._黄浩 sql

.NET 学习教程下载地址_.net课程下载-程序员宅基地

文章浏览阅读578次,点赞2次,收藏2次。==============================================================================================================教程&电子书==============================================================================================================C#入门经典_.net课程下载

STM32F7 + FREERTOS + LWIP 接收数据从网卡到应用层完整流程_stm32f7 lwip-程序员宅基地

文章浏览阅读3.8k次,点赞2次,收藏8次。来来来,这里解释下从网卡PHY到IP层的数据接收流程:这里是以函数调用方式来体现:netif_add——》ethernetif_init——》low_level_init——》ethernetif_input——》low_level_input和tcpip_input——》ethernet_input——》ip4_input(etharp_input、pppoe_disc_input)——》udp..._stm32f7 lwip

随便推点

UNITY中判断两个点之间距离的方法_unity 判断两个距离-程序员宅基地

文章浏览阅读3.1k次,点赞3次,收藏4次。Vector3.SqrMagnitude与Vector3.Distance_unity 判断两个距离

Idea设置未使用的方法,变量的提示颜色_idea 更改未使用变量的颜色-程序员宅基地

文章浏览阅读2.8w次,点赞6次,收藏14次。修改未被调用的变量,方法的提示,方便观赏代码_idea 更改未使用变量的颜色

在Linux系统下C语言编译过程的四个步骤_linux下编写一个c程序的基本过程分为几部分?-程序员宅基地

文章浏览阅读2.7k次,点赞7次,收藏23次。1. 简介C语言程序从源代码到可执行文件(二进制文件)都经历了那些过程?本文以Linux下C语言的编译过程为例,讲解C语言程序的编译过程。以hello.c文件为例:#include <stdio.h>int main(){ printf("hello world!\n");}在linux下编译C程序:$ gcc hello.c -o hello # 编译$ ./hello # 执行hello world! # 输出文本2. 编译的步骤gcc命令编译C语言的过程中_linux下编写一个c程序的基本过程分为几部分?

pyqt5界面开发-制作程序集合桌面-基本的框架_用pyqt做程序集合的界面-程序员宅基地

文章浏览阅读340次。pyqt5界面开发-制作多个小程序-基本的框架和思路最近现在无事,看到了电脑桌面,又想到了最近入门的pyqt5,再看看以往的程序,想到了可不可以做一个集成的UI桌面_用pyqt做程序集合的界面

对网站商城源码的研究分析 分享大量源码下载_chengren 电影-程序员宅基地

文章浏览阅读2k次。第一部分(1-6):前端纯静态网页模板无后台+大量网站设计素材 1:PC模板: 9900套响应式html5+css3网页模板【页面齐,二级,三级页均有,含中文模板】 2:PSD模板:3000套PSD模板+600套Flash酷站源文件+千套矢量ICO图标 3:手机模板:2000套各行业中文手机..._chengren 电影

ORA-39143: 转储文件 "F:\ora10G_expdp\ic_price_fromlufang.dmp" 可能是原始的导出转 储文件...-程序员宅基地

文章浏览阅读441次。连接到: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - ProductionWith the Partitioning, OLAP and Data Mining optionsORA-39001: 参数值无效ORA-39000: 转储文件说明错误ORA-39143: 转储文件 "F:\ora10G_expdp\ic_pri..._ora39143

推荐文章

热门文章

相关标签