hive创建udf函数流程

article/2025/9/27 0:05:08

1.编写udf函数

引入pom文件

<dependencies>
    <dependency>

<!-- 这个属于额外的jar包 自己按需引用 比如你想搞得函数 里面要连接mysql 这里肯定需要引入mysql的驱动包 我这个包是为了计算字符串的表达式的。 -->
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-jexl3</artifactId>
        <version>3.1</version>
    </dependency>
    <dependency> <!-- 这个只需provided即可,因为服务器有hive环境-->
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-exec</artifactId>
        <version>3.1.2</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

先确定好你要写什么函数比如我要写一个 计算字符串表达式。

177f240bb5f449948f8e2966b6fab692.png

 开始继承hive的udf接口,有很多小伙伴这个时候就喜欢看别人是怎么写的,这个时候就是体现个人差距的时候了,如何不看别人文档自己写呢?比如没网的条件下?

抄别人的 为啥不直接抄hive的呢? 想想hive什么udf函数最简单,lower/upper。照着抄就行。

public class StringCal extends GenericUDF 实现三个方法

initialize 初始化 校验参数的

evaluate 真正执行的方法

getDisplayString: desc function时 打印的话


import org.apache.commons.jexl3.JexlBuilder;
import org.apache.commons.jexl3.JexlEngine;
import org.apache.commons.jexl3.JexlExpression;
import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDFUtils;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.typeinfo.BaseCharTypeInfo;
import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory;
//有时间的这里写下,免得后面自己都不知道是干嘛的了。
@Description(name = "StringCal",value = "_FUNC_(str) - Returns str with calculate result",extended = "Example:\n"+ "  > SELECT _FUNC_('1+(-1+2.0-3.0+(4.0-5.0))+3.1-4.1+2*3+1.1*4') FROM src LIMIT 1;\n" + "  '-7.2'")
public class StringCal extends GenericUDF {private transient PrimitiveObjectInspector argumentOI;private transient PrimitiveObjectInspectorConverter.StringConverter stringConverter;private transient PrimitiveObjectInspector.PrimitiveCategory returnType = PrimitiveObjectInspector.PrimitiveCategory.STRING;private transient GenericUDFUtils.StringHelper returnHelper;//这里一大串校验,校验是不是普通类型啥的,校验是字符串还是啥,哪那么多事,反正照着抄就行,不写也没啥你自己定义的函数,别人也不会用。@Overridepublic ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {if (arguments.length != 1) {throw new UDFArgumentLengthException("StringCal requires 1 argument, got " + arguments.length);}if (arguments[0].getCategory() != ObjectInspector.Category.PRIMITIVE) {throw new UDFArgumentException("StringCal only takes primitive types, got " + argumentOI.getTypeName());}argumentOI = (PrimitiveObjectInspector) arguments[0];stringConverter = new PrimitiveObjectInspectorConverter.StringConverter(argumentOI);PrimitiveObjectInspector.PrimitiveCategory inputType = argumentOI.getPrimitiveCategory();ObjectInspector outputOI = null;BaseCharTypeInfo typeInfo;switch (inputType) {case CHAR:// return type should have same length as the input.returnType = inputType;typeInfo = TypeInfoFactory.getCharTypeInfo(GenericUDFUtils.StringHelper.getFixedStringSizeForType(argumentOI));outputOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(typeInfo);break;case VARCHAR:// return type should have same length as the input.returnType = inputType;typeInfo = TypeInfoFactory.getVarcharTypeInfo(GenericUDFUtils.StringHelper.getFixedStringSizeForType(argumentOI));outputOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(typeInfo);break;default:returnType = PrimitiveObjectInspector.PrimitiveCategory.STRING;outputOI = PrimitiveObjectInspectorFactory.writableStringObjectInspector;break;}returnHelper = new GenericUDFUtils.StringHelper(returnType);return outputOI;}@Overridepublic Object evaluate(DeferredObject[] arguments) throws HiveException {String val = null;if (arguments[0] != null) {val = (String) stringConverter.convert(arguments[0].get());}if (val == null) {return null;}
//就这里是我自己写的 其他的都是抄的lowerUdf的。
//        String expressionString = "1+(-1+2.0-3.0+(4.0-5.0))+3.1-4.1+2*3+1.1*4";JexlEngine jexlEngine = new JexlBuilder().create();JexlExpression jexlExpression = jexlEngine.createExpression(val);Object evaluate = jexlExpression.evaluate(null);return returnHelper.setReturnValue(evaluate.toString());}@Overridepublic String getDisplayString(String[] children) {return null;}
}

然后打包成一个jar,上传的hdfs

7097596595bb407a87c884aa15a4ad4d.png

 我嫌弃打的包不好听就直接改了个名字。

然后创建函数

create function default.stringCal as 'com.chenchi.s2.function.StringCal' using jar 'hdfs:///user/hive/function/stringcalculate.jar';

建议大家用这种方式,这种方式你可以随时替换jar,使得函数都是最新的。

这个是创建函数指定用哪个jar

或者

add jar hdfs:///user/hive/function/dw_ce_analysis.jar;

create function defalut.stringcal as 'com.chenchi.s2.function.StringCal';

这个是先加载jar然后根据jar创建函数。 add jar 后list jar可以看到是在/tmp目录

注意啊 这里加上数据库的名字,否则退出会话就没了。

c0c3b953740844c7ba7353e280ef203b.png 这里我创建了两次。就是因为没加数据库的名字,反正建议各位加下。

也可以 

 drop function dwdmdata.stringcal

 最后享受下劳动成果。有精度误差,无伤大雅。 

283dd306601e4f0688a87456e0d06fe3.png


http://chatgpt.dhexx.cn/article/bRDYNQEU.shtml

相关文章

自定义UDF、UDTF函数

自定义步骤 自定义UDF&#xff1a;继承UDF&#xff0c;重写evaluate方法自定义UDTF&#xff1a;继承GenericUDTF&#xff0c;重写3个方法&#xff1a;initialize&#xff08;自定义输出数据的列名和类型&#xff09;&#xff0c;process&#xff08;将结果返回forward(result)…

Hive-编写UDF函数(详细教程~~~)

编写UDF函数 (1)创建项目:(2)导入依赖:(3)继承UDF类(4)业务代码(5)打包编译(5)添加到hive类路径(6)创建临时函数(7)使用测试 Hive 自带了一些函数&#xff0c;比如&#xff1a;max/min 等&#xff0c;但是数量有限&#xff0c;自己可以通过自定义 UDF 来 方便的扩展。 2&#x…

关于Mysql中UDF函数的思考(一)

一点背景 从大学二年纪接触编程&#xff0c;几乎我阅读过的所有的编程语言教材都会有那么一章专门讲述数据库编程&#xff0c;而讲述的内容都无非是介绍某个数据库历史&#xff0c;对应的安装过程&#xff0c;最后才会讲解一点SQL语句&#xff0c;像这样的教材我个人认为是完全…

UDF、UDAF、UDTF之间的区别

1、UDF&#xff1a;用户定义&#xff08;普通&#xff09;函数&#xff0c;只对单行数值产生作用&#xff1b; 继承UDF类&#xff0c;添加方法 evaluate() /*** function 自定义UDF统计最小值**/public class Min extends UDF {public Double evaluate(Double a, Double b) {i…

HIVE udf、udaf、udtf函数定义与用法(最全!!!!!)

一、定义 1、hive udf、udaf、udtf函数定义与用法 &#xff08;1&#xff09;UDF&#xff08;user-defined function&#xff09;作用于单个数据行&#xff0c;产生一个数据行作为输出。&#xff08;数学函数&#xff0c;字符串函数&#xff09; &#xff08;2&#xff09;U…

udf,udaf,udtf之间的区别

1、UDF&#xff1a;用户定义&#xff08;普通&#xff09;函数&#xff0c;只对单行数值产生作用&#xff1b; 继承UDF类&#xff0c;添加方法 evaluate() /*** function 自定义UDF统计最小值* author John**/public class Min extends UDF {public Double evaluate(Double a…

如何编写udf函数(收藏篇)

hive自带了一些函数&#xff0c;比如&#xff1a;max、min 等&#xff0c;但是自带的函数数量有限&#xff0c;所以hive提供给用户自定义函数的功能。 udf 函数可以直接应用于select 语句&#xff0c;对查询结构做格式化处理之后&#xff0c;然后再输出内容。 下面将详细介绍下…

自定义UDF函数

自定义函数案例&#xff1a; 文章目录 自定义UDF函数1.需求2.前期maven工程准备3.编程实现4.导包5.导入hive中 自定义UDTF函数1.需求2.编程实现3.导入hive中 自定义UDF函数 1.需求 自定义一个UDF实现计算给定字符串的长度例如 2.前期maven工程准备 创建一个maven工程&#x…

Hive自定义UDF函数详解

Hive自定义UDF函数详解 一、UDF概述二、UDF种类三、如何自定义UDF四、自定义实现UDF和UDTF4.1 需求4.2 项目pom文件4.3 Hive建表测试及数据4.4 UDF函数编写4.5 UDTF函数编写4.6 UDTF使用 一、UDF概述 UDF全称&#xff1a;User-Defined Functions&#xff0c;即用户自定义函数&…

Hive UDF简单函数

概念 在Hive中&#xff0c;用户可以自定义一些函数&#xff0c;用于扩展HiveQL的功能&#xff0c;而这类函数叫做UDF&#xff08;用户自定义函数)。UDF分为两大类&#xff1a;UDAF&#xff08;用户自定义聚合函数&#xff09;和UDTF&#xff08;用户自定义表生成函数)。  UDF…

大数据Hive篇--UDF函数

什么是UDF: 它是User defined Function的简写&#xff0c;意思是用户自定义方法 为什么要用UDF&#xff1f; hive自带了一些函数&#xff0c;比如&#xff1a;max、min 等&#xff0c;但是自带的函数数量有限&#xff0c;所以hive提供给用户自定义函数的功能。 udf 函数可以…

《C#入门详解》刘铁猛——Lesson20-21事件

其实事件一般就是在界面程序中应用&#xff0c;所以这里讲的不如WPF这种的实用。

《C#入门详解》刘铁猛——Lesson1-2 IDE、各种应用程序

编程学习路径&#xff1a;语言——类库——框架 参考资料&#xff1a;离线MSDN文档&#xff0c;C#语言定义文档&#xff0c;推荐书籍—C# in a nutshell&#xff1b;其中&#xff0c;C#语言定义文档知识点会串的比较多&#xff0c;不建议详读。 使用MSDN&#xff1a;光标选中…

《C#入门详解》刘铁猛——Lesson8-9 方法的定义、调用与调试

方法命名&#xff1a;使用动词或者动词短语&#xff1b;使用PASCAL规则&#xff0c;即所有单词首字母大写。 静态类型的方法不是实例的方法&#xff0c;是类的方法&#xff0c;因此&#xff0c;实例不能调用静态方法&#xff0c;只能用类调用静态方法&#xff0c;示例程序如下&…

《C#入门详解》刘铁猛——Lesson17字段、属性、索引器、常量

属性代码示例&#xff1a; 以上代码演示了字段由get-set方法对演化成为属性的过程。 prop连敲两下Tab键&#xff0c;是属性声明的快捷键。 强调以下&#xff1a; 很少使用索引器。 声明和使用常量&#xff1a;

《C#入门详解》刘铁猛——Lesson27-28类的重写、多态、抽象类、开闭原则

重写、多态——子类对父类的纵向扩展&#xff0c;就是方法的版本升级。 override——重写&#xff0c;子类对父类成员的版本更新。 virtual——override 下面的例子就是多态——多态就是使用一个父类的变量引用一个子类的实例&#xff0c;当调用方法时&#xff0c;会顺着继承链…

《C#入门详解》刘铁猛——Lesson19委托

自定义委托类型&#xff1a; 模板方法实例&#xff1a; 回调方法示例&#xff1a; 多播委托示例&#xff1a; 同步调用&#xff08;串行&#xff0c;单线程&#xff09;示例&#xff1a; 多播委托也是同步调用&#xff1a; 隐式的异步调用示例&#xff1a; 执行结果发生了资源…

《C#入门详解》刘铁猛——Lesson18传值\输出\引用\数组\具名\可选参数、扩展方法

x是101&#xff0c;y是100 在声明函数的时候带有默认值。 静态函数&#xff0c;第一个参数加this修饰符&#xff0c;就是一个扩展方法。调用的时候可以看见向下的小箭头。

刘铁猛C#语言入门详解——学习笔记014、15、16(2)

using System; using System.Collections.Generic; namespace ConsoleApp2 {class Program{static void Main(string[] args){//c#语言对表达式的定义&#xff1a;a sequence of one or more operands and zero or more operators can be evaluated to a single value object m…