UDF:User Definition Function

一、function

#查看自带的函数
hive (db_hive)> show functions; #查看一个函数的详细用法
hive (db_hive)> desc function extended split;
OK
tab_name
split(str, regex) - Splits str around occurances that match regex
Example:
> SELECT split('oneAtwoBthreeC', '[ABC]') FROM src LIMIT 1;
["one", "two", "three"]
Time taken: 0.005 seconds, Fetched: 4 row(s)

二、UDF

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF

https://cwiki.apache.org/confluence/display/Hive/HivePlugins        #自定义UDF

Hive自带了一些函数,比如:max/min等,但是数量有限,自己可以通过自定义UDF来方便的扩展。

UDF:用户自定义函数,允许用户扩展HiveQL功能;

##
UDF(User-Defined-Function)
一进一出 UDAF(User-Defined Aggregation Funcation)
聚集函数,多进一出;类似于:count/max/min UDTF(User-Defined Table-Generating Functions)
一进多出;如lateral view explore() 编程步骤:
1、继承org.apache.hadoop.hive.ql.UDF
2、需要实现evaluate函数;evaluate函数支持重载; 注意事项:
1、UDF必须要有返回类型,可以返回null,但是返回类型不能为void;
2、UDF中常用Text/LongWritable等类型,不推荐使用java类型;

创建一个UDF-方式一:

1、Creating Custom UDFs

### LowerUDF.java###
package com.beifeng.senior.hive.udf; import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text; /**
* 1. Implement one or more methods named
* "evaluate" which will be called by Hive.
*
* 2."evaluate" should never be a void method. However it can return "null" if
* needed.
* @author root
*
*/ public class LowerUDF extends UDF{ public Text evaluate(Text str) {
//validate
if(null == str.toString()) {
return null;
}
//lower
return new Text (str.toString().toLowerCase());
} public static void main(String[] args) {
System.out.println(new LowerUDF().evaluate(new Text("HIVE")));
}
} #然后打成jar包
[root@hadoop-senior datas]# pwd
/opt/datas
[root@hadoop-senior datas]# ls hiveudf.jar
hiveudf.jar

2、usage

#添加
hive (db_hive)> add jar /opt/datas/hiveudf.jar;
Added /opt/datas/hiveudf.jar to class path
Added resource: /opt/datas/hiveudf.jar #注册,my_lower是要注册的函数名,com.beifeng.senior.hive.udf.LowerUDF是类名
hive (db_hive)> create temporary function my_lower as "com.beifeng.senior.hive.udf.LowerUDF";
OK
Time taken: 0.012 seconds #查看
hive (db_hive)> show functions;
...
my_lower
... #测试使用
hive (db_hive)> select ename, my_lower(ename) lowername from emp limit 5;
Total jobs = 1
Launching Job 1 out of 1
Number of reduce tasks is set to 0 since there's no reduce operator
Starting Job = job_1554717689707_0031, Tracking URL = http://hadoop-senior.ibeifeng.com:8088/proxy/application_1554717689707_0031/
Kill Command = /opt/modules/hadoop-2.5.0/bin/hadoop job -kill job_1554717689707_0031
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 0
2019-04-24 15:32:42,268 Stage-1 map = 0%, reduce = 0%
2019-04-24 15:32:47,387 Stage-1 map = 100%, reduce = 0%, Cumulative CPU 1.28 sec
MapReduce Total cumulative CPU time: 1 seconds 280 msec
Ended Job = job_1554717689707_0031
MapReduce Jobs Launched:
Job 0: Map: 1 Cumulative CPU: 1.28 sec HDFS Read: 894 HDFS Write: 60 SUCCESS
Total MapReduce CPU Time Spent: 1 seconds 280 msec
OK
ename lowername
SMITH smith
ALLEN allen
WARD ward
JONES jones
MARTIN martin
Time taken: 10.548 seconds, Fetched: 5 row(s)

创建一个UDF-方式二:

此方法jar包要位于hdfs上;

CREATE FUNCTION myfunc AS 'myclass' USING JAR 'hdfs:///path/to/jar';

1、

##上传jar包到hdfs
hive (db_hive)> dfs -mkdir -p /user/root/hive/jars/;
hive (db_hive)> dfs -put /opt/datas/hiveudf.jar /user/root/hive/jars/;
hive (db_hive)> dfs -ls -R /user/root/hive/jars;
-rw-r--r-- 1 root supergroup 910 2019-04-24 15:40 /user/root/hive/jars/hiveudf.jar #创建function
hive (db_hive)> create function self_lower as 'com.beifeng.senior.hive.udf.LowerUDF' using jar 'hdfs://hadoop-senior.ibeifeng.com:8020/user/root/hive/jars/hiveudf.jar';
converting to local hdfs://hadoop-senior.ibeifeng.com:8020/user/root/hive/jars/hiveudf.jar
Added /tmp/5356b66f-bf56-4de6-abf8-30be8029fa8b_resources/hiveudf.jar to class path
Added resource: /tmp/5356b66f-bf56-4de6-abf8-30be8029fa8b_resources/hiveudf.jar
OK
Time taken: 0.025 seconds #使用
hive (db_hive)> select ename, self_lower(ename) lowername from emp limit 5;
Total jobs = 1
Launching Job 1 out of 1
Number of reduce tasks is set to 0 since there's no reduce operator
Starting Job = job_1554717689707_0032, Tracking URL = http://hadoop-senior.ibeifeng.com:8088/proxy/application_1554717689707_0032/
Kill Command = /opt/modules/hadoop-2.5.0/bin/hadoop job -kill job_1554717689707_0032
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 0
2019-04-24 15:53:28,378 Stage-1 map = 0%, reduce = 0%
2019-04-24 15:53:33,504 Stage-1 map = 100%, reduce = 0%, Cumulative CPU 1.35 sec
MapReduce Total cumulative CPU time: 1 seconds 350 msec
Ended Job = job_1554717689707_0032
MapReduce Jobs Launched:
Job 0: Map: 1 Cumulative CPU: 1.35 sec HDFS Read: 894 HDFS Write: 60 SUCCESS
Total MapReduce CPU Time Spent: 1 seconds 350 msec
OK
ename lowername
SMITH smith
ALLEN allen
WARD ward
JONES jones
MARTIN martin
Time taken: 10.549 seconds, Fetched: 5 row(s)

2.13 Hive中自带Function使用及自定义UDF编程的更多相关文章

  1. Hive中实现group concat功能(不用udf)

    在 Hive 中实现将一个字段的多条记录拼接成一个记录: hive> desc t; OK id string str string Time taken: 0.249 seconds hive ...

  2. Hive中的UDF详解

    hive作为一个sql查询引擎,自带了一些基本的函数,比如count(计数),sum(求和),有时候这些基本函数满足不了我们的需求,这时候就要写hive hdf(user defined funati ...

  3. 切记ajax中要带上AntiForgeryToken防止CSRF攻击

    在程序项目中经常看到ajax post数据到服务器没有加上防伪标记,导致CSRF被攻击,下面小编通过本篇文章给大家介绍ajax中要带上AntiForgeryToken防止CSRF攻击,感兴趣的朋友一起 ...

  4. hive中UDF、UDAF和UDTF使用

    Hive进行UDF开发十分简单,此处所说UDF为Temporary的function,所以需要hive版本在0.4.0以上才可以. 一.背景:Hive是基于Hadoop中的MapReduce,提供HQ ...

  5. Hive中知识点

    hive的最新学习资料:http://www.cnblogs.com/qingyunzong/p/8707885.html hive的参数设置大全:https://cwiki.apache.org/c ...

  6. 在hive中UDF和UDAF使用说明

    Hive进行UDF开发十分简单,此处所说UDF为Temporary的function,所以需要hive版本在0.4.0以上才可以. 一.背景:Hive是基于Hadoop中的MapReduce,提供HQ ...

  7. 【转】hive中UDF、UDAF和UDTF使用

    原博文出自于: http://blog.csdn.net/liuj2511981/article/details/8523084 感谢! Hive进行UDF开发十分简单,此处所说UDF为Tempora ...

  8. Hive 中的 UDF

    LanguageManual UDF 一.分类 UDF:User defined function 用户定义函数 一进一出 UDAF:User defined aggregation function ...

  9. 如何在 Apache Hive 中解析 Json 数组

    我们都知道,Hive 内部提供了大量的内置函数用于处理各种类型的需求,参见官方文档:Hive Operators and User-Defined Functions (UDFs).我们从这些内置的 ...

随机推荐

  1. 《好好说话》zz

    最近,<奇葩说>闹出来了一些不愉快. 在半决赛中,姜思达惜败,愤怒的粉丝把矛头指向那场比赛的其他人.最终,马薇薇.黄执中和网友们吵起来了. 这件事本不算大事,毕竟娱乐业就是这个样子.刚刚好 ...

  2. UVa11234 表达式

    题意:题目意思是给出后缀表达式.能够通过栈来计算表达式的值,即转化为中缀表达式. 然后如果如今不用栈.而是用队列来操作.即每遇到一操作符时.进行两次pop和一次push.(这里注意,先pop出来的作为 ...

  3. PythonCookBook笔记——字符串和文本

    字符串和文本 使用多个分隔符分割字串 使用正则re.split()方法. >>> line = 'asdf fjdk; afed, fjek,asdf, foo' >>& ...

  4. Hadoop集群_HDFS初探之旅

    1.HDFS简介 HDFS(Hadoop Distributed File System)是Hadoop项目的核心子项目,是分布式计算中数据存储管理的基础,是基于流数据模式访问和处理超大文件的需求而开 ...

  5. 网络协议之rtp---h264的rtp网络协议实现

    完整的C/S架构的基于RTP/RTCP的H.264视频传输方案.此方案中,在服务器端和客户端分别进行了功能模块设计.服务器端:RTP封装模块主要是对H.264码流进行打包封装:RTCP分析模块负责产牛 ...

  6. [ios]objective-c中Category类别(扩展类)专题总结

    本文转载至 http://yul100887.blog.163.com/blog/static/20033613520126333344127/   objective-c类别的作用?通过类别的方式, ...

  7. spring applicationContext.xml详解及模板

    applicationContext.xml 文件   1.<context:component-scan base-package="com.eduoinfo.finances.ba ...

  8. 从前端看JavaWeb软件工程中的解耦合

    以下为作者备忘,详情请看 http://www.cnblogs.com/feichengwulai/articles/3412946.html http://blog.csdn.net/piantou ...

  9. 程序员必知的8大排序(java实现)

    先来看看8种排序之间的关系:

  10. Dubbo Spring Cloud Motan

    跨语言统一治理.Golang,谈谈另辟蹊径的开源RPC框架Motan_搜狐科技_搜狐网 https://www.sohu.com/a/207389967_467759