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. CAD鼠标移动到对象时显示对象内容

    //定义事件         Editor ed = doc.Editor;         ed.PointMonitor += new PointMonitorEventHandler(ed_Po ...

  2. C#正则表达式获取网址的域名(IP)

    代码如下: string p = @"(http|https)://(?<domain>[^(:|/]*)"; Regex reg = new Regex(p, Reg ...

  3. windows server 安装之后需要做的操作

    一.运行windows update安装更新 提示: 若一直停留在“正在检查更新”,请参考https://answers.microsoft.com/zh-hans/windows/forum/win ...

  4. 缓存的set、getAndTouch一定要谨慎使用

    缓存的set.getAndTouch一定要谨慎使用. 很多人认为缓存在内存中性能良好,频繁更新,却不想机器的IO无法支撑,结果就是缓存成了系统的瓶颈.

  5. ERP和C4C中的function location

    SAP ERP里的Functional Locations,下载到SAP Cloud for Customer后成为类型为'Functional Location'的Installation Poin ...

  6. 搭建大众点评CAT监控平台

    CAT(Central Application Tracking)是基于Java开发的实时应用监控平台,包括实时应用监控,业务监控.关于CAT的具体介绍可移步到CAT官网进行查阅. 1. 环境清单 C ...

  7. Linux赋予普通用户root权限

    本文以新建用户admin来举例,请自行替换自己需要的用户 方法一: vi编辑 /etc/sudoers 文件,找到 root    ALL=(ALL)     ALL,在下面添加一行,如下所示: ## ...

  8. PowerDesigner生成sql脚本

    1.打开PowerDesigner->New Project; 2.填写项目名称,选择文件的存放路径: 3.新建一个模型,New Model: 4.选择概念模型,填写模型名称: 5.选择enti ...

  9. Web as a App(Web既APP)的概念可以提出吗?

    Web as a App (WaaA),Web既APP.灵感出于SaaS.PaaS.IaaS等~ 最近在做一个PC项目,即便我的项目是to B的,用户量没那么大,但是我仍然很注重性能及用户体验,我把我 ...

  10. 洛谷P2759 奇怪的函数(log 二分)

    题目描述 使得 x^xxx 达到或超过 n 位数字的最小正整数 x 是多少? 输入输出格式 输入格式: 一个正整数 n 输出格式: 使得 x^xxx 达到 n 位数字的最小正整数 x 输入输出样例 输 ...