LanguageManual UDF

一、分类

UDF:User defined function 用户定义函数
一进一出
UDAF:User defined aggregation function
聚类函数:多进一出
如:max min count
UDTF:User definesd table-Generating Function
一进多出
如:lateral view explore

二、实战

1.创建Maven工程,修改pom.xml

hive-pom.xml

2.First, you need to create a new class that extends UDF, with one or more methods named evaluate.

创建一个类继承UDF类,实现 evaluate 方法

package com.cenzhongman.hive.udf;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text; public class LowerUDF extends UDF{ //•Implement one or more methods named evaluate which will be called by Hive (the exact way in which Hive resolves the method to call can be configured by setting a custom UDFMethodResolver). 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, Text c);
// ◦public Text evaluate(String a);
// ◦public String evaluate(List<Integer> a); (Note that Hive Arrays are represented as Lists in Hive. So an ARRAY<int> column would be passed in as a List<Integer>.)
// •evaluate should never be a void method. However it can return null if needed. 不允许返回类型为 void 可以返回 null
// •Return types as well as method arguments can be either Java primitives or the corresponding Writable class.
// !!推荐参数使用mapReduce 的类型 public Text evaluate(Text str) {
//void data
if(str.toString() == null) {
return null;
}
//lower
return new Text(str.toString().toLowerCase());
} //用于测试,Hive 的入口函数是 evaluate 所以没有影响
public static void main(String[] args) {
System.out.println(new LowerUDF().evaluate(new Text("Hive")));
}
}

3.在 Hive 中使用自定义函数

# 添加 jar 到资源库中
add jar /opt/datas/filename.jar # 创建临时函数
create temporary function my_lower as "com.cenzhongman.hive.udf.LowerUDF"; # 查看函数,确认添加成功
show functions; # 使用函数
select my_lower(job) Upper_job from emp;

As of Hive 0.13, UDFs also have the option of being able to specify required jars in the CREATE FUNCTION statement:

对于新版本,有一种新的打开方式(文件需在HDFS文件系统上)

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

Hive 中的 UDF的更多相关文章

  1. Hive中的UDF详解

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

  2. Hive扩展功能(三)--使用UDF函数将Hive中的数据插入MySQL中

    软件环境: linux系统: CentOS6.7 Hadoop版本: 2.6.5 zookeeper版本: 3.4.8 主机配置: 一共m1, m2, m3这五部机, 每部主机的用户名都为centos ...

  3. Hive 教程(十)-UDF

    hive 虽然自带了很多函数,但是毕竟有限,无法满足所有业务场景,用户可以自定义函数来实现特定功能 UDF user define function,用户自定义函数 可以分为 3 类 UDF:一进一出 ...

  4. hive中UDF、UDAF和UDTF使用

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

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

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

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

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

  7. Hive中如何添加自定义UDF函数以及oozie中使用hive的自定义函数

    操作步骤: 1. 修改.hiverc文件 在hive的conf文件夹下面,如果没有.hiverc文件,手工自己创建一个. 参照如下格式添加: add jar /usr/local/hive/exter ...

  8. Hive中的用户自定义函数UDF

    Hive中的自定义函数允许用户扩展HiveQL,是一个非常强大的功能.Hive中具有多种类型的用户自定义函数.show functions命令可以列举出当前Hive会话中的所加载进来的函数,包括内置的 ...

  9. hive中 udf,udaf,udtf

    1.hive中基本操作: DDL,DML 2.hive中函数 User-Defined Functions : UDF(用户自定义函数,简称JDF函数)UDF: 一进一出  upper  lower ...

随机推荐

  1. php读取mysql中文乱码

    连接mysql的文件: <?php /***************************** *数据库连接 *****************************/ $conn = @m ...

  2. System Center Configuration Manager 2016 配置安装篇(Part2)

    步骤4.安装SCCM当前分支(版本1802) 注意:以管理员身份在ConfigMgr服务器(CM01)上执行以下操作. 为此,在Configuration Manager服务器(CM16)上,打开W ...

  3. ASP.NET设置母版页

    母版页允许开发人员创建具有指定的可编辑区域的站点级模板.随后,此模板可应用到网站中的 ASP.NET 页面上.这些 ASP.NET 页面只需为母版页中指定的可编辑区域提供相应内容 – 在使用母版页的所 ...

  4. Sublime Text3 + Markdown + 实时预览

    Sublime Text3是一款给力的文本编辑器,通过安装插件可以编辑Markdown文本,在编辑Markdown文本的同时可以实时预览编辑效果. 安装准备: 找到菜单栏:Preferences → ...

  5. leetcode:查找

    1.  word ladder 题目: Given two words (start and end), and a dictionary, find the length of shortest t ...

  6. phpStudy-FTP_Server插件安装使用教程

    FileZilla Server使用教程 ftp server安装教程 除了phpStudy for IIS外其他版本phpStudy不再集成ftp server外. phpStudy for IIS ...

  7. 【洛谷P1314】[NOIP2011]聪明的质监员

    聪明的质监员 题目链接:https://www.luogu.org/problemnew/show/P1314 Y(W)随W的值增大而减小 二分W的值,找到最小的W使得Y(W)>S: 比较Y(W ...

  8. vscode-tfs插件报错:TF30063

    解决方案:删除tfs凭证,然后用vs重新登陆tfs服务器,此时会在电脑上创建要一个新的tfs凭证,然后再用vscode-tfs操作tfs就没有问题了.

  9. centos install rtl8188ce driver

    1.导入公钥,注意大小写. rpm --import http://elrepo.org/RPM-GPG-KEY-elrepo.org 2.安装ELRepo库. rpm -Uvh http://elr ...

  10. 第44章 MPU6050传感器—姿态检测—零死角玩转STM32-F429系列

    第44章     MPU6050传感器—姿态检测 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.co ...