【Hive六】Hive用户自定义函数(UDF)
编程技术  /  houtizong 发布于 3年前   81
Hive是基于Hadoop中的MapReduce,提供HQL查询的数据仓库。Hive是一个很开放的系统,很多内容都支持用户定制,包括:
package org.apache.hadoop.hive.ql.udf;import org.apache.hadoop.hive.ql.exec.UDF;import org.apache.hadoop.hive.serde2.io.DoubleWritable;import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable;public abstract class UDFMath extends UDF { private final DoubleWritable doubleWritable = new DoubleWritable(); public UDFMath() { } /** * For subclass to implement. */ public abstract DoubleWritable evaluate(DoubleWritable a); /** * Convert HiveDecimal to a double and call evaluate() on it. */ public final DoubleWritable evaluate(HiveDecimalWritable writable) { if (writable == null) { return null; } double d = writable.getHiveDecimal().bigDecimalValue().doubleValue(); doubleWritable.set(d); return evaluate(doubleWritable); }}
package org.apache.hadoop.hive.ql.udf;import org.apache.hadoop.hive.ql.exec.Description;import org.apache.hadoop.hive.ql.exec.UDF;import org.apache.hadoop.hive.ql.exec.vector.VectorizedExpressions;import org.apache.hadoop.hive.ql.exec.vector.expressions.gen.FuncSinDoubleToDouble;import org.apache.hadoop.hive.ql.exec.vector.expressions.gen.FuncSinLongToDouble;import org.apache.hadoop.hive.serde2.io.DoubleWritable;import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable;/** * UDFSin. * */@Description(name = "sin", value = "_FUNC_(x) - returns the sine of x (x is in radians)", extended = "Example:\n " + " > SELECT _FUNC_(0) FROM src LIMIT 1;\n" + " 0")@VectorizedExpressions({FuncSinLongToDouble.class, FuncSinDoubleToDouble.class})public class UDFSin extends UDFMath { private final DoubleWritable result = new DoubleWritable(); public UDFSin() { } /** * Take Sine of a. */ public DoubleWritable evaluate(DoubleWritable a) { if (a == null) { return null; } else { result.set(Math.sin(a.get())); return result; } }}
上面是自定义了一个求正玄的UDF函数
hive>add jar /user/hadoop/udf.math.sin.jar;
hive>CREATE TEMPORARY FUNCTION math.sin AS 'org.apache.hadoop.hive.ql.udf.UDFSin';
SELECT math.sin(3.14) FROM word;
hive> DROP TEMPORARY FUNCTION math.sin;
org.apache.hadoop.hive.ql.udf.UDFMathorg.apache.hadoop.hive.ql.udf.UDFSin
添加注册函数的语句
registerFunction("udf_math_sina", UDFSin.class)
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接