Hive的UDF
来源:互联网 发布:淘宝发票抬头设置 编辑:程序博客网 时间:2024/06/06 00:39
1、UDF函数可以直接应用于select语句,对查询结构做格式化处理后,再输出内容。
2、编写UDF函数的时候需要注意一下几点:
a)自定义UDF需要继承org.apache.hadoop.hive.ql.UDF。
b)需要实现evaluate函数,evaluate函数支持重载。
3、步骤
a)把程序打包放到目标机器上去;
b)进入hive客户端,添加jar包:hive>add jar /run/jar/udf_test.jar;
c)创建临时函数:hive>CREATE TEMPORARY FUNCTION add_example AS 'hive.udf.Add';
d)查询HQL语句:
SELECT add_example(8, 9) FROM scores;
SELECT add_example(scores.math, scores.art) FROM scores;
SELECT add_example(6, 7, 8, 6.8) FROM scores;
1.添加jar包(在hive命令行里面执行)
hive> add jar /root/NUDF.jar;
2.创建临时函数
hive> create temporary function getNation as 'edu.jiawei.hive.udf.NationUDF';
3.调用
hive> select id, name, getNation(nation) from people;
4.将查询结果保存到HDFS中
hive> create table result row format delimited fields terminated by '\t' as select * from people order by id desc;
2、编写UDF函数的时候需要注意一下几点:
a)自定义UDF需要继承org.apache.hadoop.hive.ql.UDF。
b)需要实现evaluate函数,evaluate函数支持重载。
3、步骤
a)把程序打包放到目标机器上去;
b)进入hive客户端,添加jar包:hive>add jar /run/jar/udf_test.jar;
c)创建临时函数:hive>CREATE TEMPORARY FUNCTION add_example AS 'hive.udf.Add';
d)查询HQL语句:
SELECT add_example(8, 9) FROM scores;
SELECT add_example(scores.math, scores.art) FROM scores;
SELECT add_example(6, 7, 8, 6.8) FROM scores;
e)销毁临时函数:hive> DROP TEMPORARY FUNCTION add_example;
4、案列
类NationUDF
package edu.jianwei.hive.udf;import java.util.HashMap;import java.util.Map;import org.apache.hadoop.hive.ql.exec.UDF;import org.apache.hadoop.io.Text;public class NationUDF extends UDF{private static Map<String, String> areaMap = new HashMap<String, String>();static {areaMap.put("China", "中国");areaMap.put("Japan", "日本");areaMap.put("USA", "美国");}public Text evaluate(Text in){String result = areaMap.get(in.toString());if(result == null){result = "其他";}return new Text(result);}}自定义函数调用过程:
1.添加jar包(在hive命令行里面执行)
hive> add jar /root/NUDF.jar;
2.创建临时函数
hive> create temporary function getNation as 'edu.jiawei.hive.udf.NationUDF';
3.调用
hive> select id, name, getNation(nation) from people;
4.将查询结果保存到HDFS中
hive> create table result row format delimited fields terminated by '\t' as select * from people order by id desc;
0 0
- hive的UDF 函数
- hive的UDF 函数
- hive的udf开发
- Hive的UDF
- Hive的UDF编程
- hive的UDF
- hive的udf
- Hive的UDF是什么?
- hive的UDF 函数
- Hive学习心得&Hive的UDF
- hive的udf的编写
- hive的UDF (2)
- hive 的UDF和UDAF
- hive中UDF的编写
- Hive中UDF的使用
- 1006-Hive的UDF操作
- Python实现Hive的UDF
- Hive的UDF、UDAF、UDTF
- Leetcode Problem.58—Length of Last Word
- DNS leak (DNS查询记录泄漏)
- java-零散知识点
- Windows GetAsyncKeyState(vk_code)
- 初识聚类算法:K均值、凝聚层次聚类和DBSCAN
- Hive的UDF
- shell脚本中,查找某个文件并执行,排除某个目录
- Leetcode Problem.66—Plus One
- ios UIImage修改大小后图片模糊解决办法
- Python使用lambda排序
- Matlab符号微积分练习
- 关于Intent的Flag属性的讲解
- Git Server搭建
- 软件设计之自顶向下