Hive写一个时间转换器的自定义函数(UDF)和创建hive自定义函数的两种方式

2024-05-11 12:32

本文主要是介绍Hive写一个时间转换器的自定义函数(UDF)和创建hive自定义函数的两种方式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在前面一篇文章的日志表中,时间的格式的是这样的"31/Aug/2015:00:04:37 +0800";这样并不友好,为了好看点,我们自定义一个时间格式化的udf函数,hive应该也提供时间转换的函数。

自定义函数

代码
自定义函数还是继承UDF类

package com.madman.hive.function;import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;/*** * UDF函数还是老样子....* * /*** A User-defined function (UDF) for the use with Hive.** New UDF classes need to inherit from this UDF class.** Required for all UDF classes: 1. Implement one or more methods named* "evaluate" which will be called by Hive. The following are some examples:* public int evaluate(); public int evaluate(int a); public double evaluate(int* a, double b); public String evaluate(String a, int b, String c);** "evaluate" should never be a void method. However it can return "null" if* needed.*/
public class HiveDateFunction extends UDF {public Text evaluate(Text time) {if (time == null) {return null;}if (StringUtils.isBlank(time.toString())) {return null;}String parser = time.toString().replaceAll("\"", "");SimpleDateFormat inputSimple = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss", Locale.ENGLISH);SimpleDateFormat outputSimple = new SimpleDateFormat("yyyyMMddHHmmss");String format = "";try {Date parse = inputSimple.parse(parser);format = outputSimple.format(parse);System.out.println(format);} catch (Exception e) {e.printStackTrace();return null;}return new Text(format);}public static void main(String[] args) {String text = "31/Aug/2015:00:04:37 +0800";System.out.println(new HiveDateFunction().evaluate(new Text(text)));System.exit(0);SimpleDateFormat inputSimple = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss", Locale.ENGLISH);SimpleDateFormat outputSimple = new SimpleDateFormat("yyyyMMddHHmmss");try {Date parse = inputSimple.parse(text);String format = outputSimple.format(parse);System.out.println(format);} catch (Exception e) {e.printStackTrace();}}
}

代码写好之后本地先测试下,是否可行,可行之后打成jar包上传到hive环境中去,然后将jar加入到hive中。
参考命令:

hive (default)> add  jar /opt/cdhmoduels/data/hiveDateFunction.jar;  

然后创建一个函数,参考命令:

create  temporary function hiveDateFunction as  'com.madman.hive.function.HiveDateFunction';
//这里需要制定类的路劲。

调用函数命令:

hive (default)> select hiveDateFunction(time_local) from bf_log limit 10;
结果:
Total MapReduce CPU Time Spent: 1 seconds 750 msec
OK
_c0
20150831000437
20150831000437
20150831000453
20150831000453
20150831000453
20150831000453
20150831000453
20150831000453
20150831000453
20150831000453
Time taken: 20.954 seconds, Fetched: 10 row(s)
hive自定义函数的两种方法
方式1

先上传jar包到hive的环境中,然后再定义函数指明类的具体路劲。

hive (default)> add  jar /opt/cdhmoduels/data/hiveDateFunction.jar;  
create  temporary function hiveDateFunction as  'com.madman.hive.function.HiveDateFunction';
测试SQL
hive (default)> select hiveDateFunction(time_local) from bf_log limit 10;
方式2

创建函数的时候直接指定类路劲和类所在jar的路劲,这里我是放在hdfs上面了,直接指定了hdfs的路劲。

create temporary function parseDate as 'com.madman.hive.function.HiveDateFunction' using jar 'hdfs://hadoop.madman.com:8020/jar/hiveDateFunction.jar';
测试SQL
hive (default)> select parseDate(time_local) from bf_log limit 10;

这篇关于Hive写一个时间转换器的自定义函数(UDF)和创建hive自定义函数的两种方式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/979536

相关文章

SpringBoot中@Value注入静态变量方式

《SpringBoot中@Value注入静态变量方式》SpringBoot中静态变量无法直接用@Value注入,需通过setter方法,@Value(${})从属性文件获取值,@Value(#{})用... 目录项目场景解决方案注解说明1、@Value("${}")使用示例2、@Value("#{}"php

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

Python的Darts库实现时间序列预测

《Python的Darts库实现时间序列预测》Darts一个集统计、机器学习与深度学习模型于一体的Python时间序列预测库,本文主要介绍了Python的Darts库实现时间序列预测,感兴趣的可以了解... 目录目录一、什么是 Darts?二、安装与基本配置安装 Darts导入基础模块三、时间序列数据结构与

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

HTTP 与 SpringBoot 参数提交与接收协议方式

《HTTP与SpringBoot参数提交与接收协议方式》HTTP参数提交方式包括URL查询、表单、JSON/XML、路径变量、头部、Cookie、GraphQL、WebSocket和SSE,依据... 目录HTTP 协议支持多种参数提交方式,主要取决于请求方法(Method)和内容类型(Content-Ty

C# LiteDB处理时间序列数据的高性能解决方案

《C#LiteDB处理时间序列数据的高性能解决方案》LiteDB作为.NET生态下的轻量级嵌入式NoSQL数据库,一直是时间序列处理的优选方案,本文将为大家大家简单介绍一下LiteDB处理时间序列数... 目录为什么选择LiteDB处理时间序列数据第一章:LiteDB时间序列数据模型设计1.1 核心设计原则

GO语言中函数命名返回值的使用

《GO语言中函数命名返回值的使用》在Go语言中,函数可以为其返回值指定名称,这被称为命名返回值或命名返回参数,这种特性可以使代码更清晰,特别是在返回多个值时,感兴趣的可以了解一下... 目录基本语法函数命名返回特点代码示例命名特点基本语法func functionName(parameters) (nam