UDF函数
来源:互联网 发布:白俄罗斯交友软件 编辑:程序博客网 时间:2024/06/07 16:29
UDF函数
UDF函数可以直接应用于select语句,对查询结构做格式化处理输出内容。自定义UDF需要继承org.apache.hadoop.hive.ql.UDF,实现evaluate函数。自定义udf函数步骤:
1.继承UDF类
2.重写evaluate方法
3.把项目打成jar包
4.hive中执行命令add jar /home/jrjt/dwetl/PUB/UDF/udf/GetProperty.jar;
5.创建函数create temporary function get_pro as 'jd.Get_Property'//jd.jd.Get_Property为类路径;
永久udf函数创建:
1、hdfs dfs -put udftimestamp.jar /udf/
2、add jar hdfs://nameservice1:8020/udf/udftimestamp.jar;
3、CREATE FUNCTION dm_lots.udftimestamp AS 'mytimestamp.MyUDFTimestamp' using jar 'hdfs://nameservice1:8020/udf/udftimestamp.jar';
删除udf函数
drop function dm_lots.udftimestamp;
查看udf函数
show functions
例1:日志切割
import org.apache.hadoop.hive.ql.exec.UDF;import org.apache.hadoop.io.Text;public class SalaryUDF extends UDF{public Text evaluate(Text salaryText){//1.if salary is nullif (salaryText == null) {return null;}String salaryStr = salaryText.toString();//2.if salary is not double typedouble salary = 0.0;try {salary = Double.valueOf(salaryStr);} catch (NumberFormatException e) {e.printStackTrace();return null;}Text text = new Text();//3.panduan salary return string if (salary > 10000) {text.set("you are rich");return text;}else if (salary <= 10000 && salary > 5000) {text.set("income is normal");return text;}else {text.set("income is pool");return text;}}}例2:日期转化
package com.rainbow.udf;import java.text.SimpleDateFormat;import java.util.Date;import org.apache.hadoop.hive.ql.exec.UDF;import org.apache.hadoop.io.Text;public class TestDate extends UDF{ private SimpleDateFormat inputdateFormat = new SimpleDateFormat("dd/MM/yyyy:HH:mm:ss",locale.ENLISH); private SimpleDateFormat outputdateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); public Text exvaluate(Text input){ Text output = new Text(); if(null==input){ return null;} if(null==input.toString()){ return null;} try { String inputDate=input.toString().trim(); Date perseDate = inputdateFormat.parse(inputDate); String outputDate = outputdateFormat.format(perseDate); output.set(outputDate);}catch(Exception e){ e.printStackTrace(); return output;} return output;}}打jar包
上传到hdfs
CREATE FUNCTION [db_name.]function_name AS class_name [USING JAR|FILE|ARCHIVE'file_uri' [, JAR|FILE|ARCHIVE'file_uri'] ];
阅读全文
0 0
- UDF函数
- UDF函数测试
- 开发hive UDF函数
- HIVE 自定义函数 UDF
- HIVE UDF函数(一)
- hive的UDF 函数
- hive的UDF 函数
- HIVE UDF函数使用
- 开发hive UDF函数
- Hive UDF函数
- hive 永久udf函数
- Hive自定义函数-UDF
- hive UDF函数开发
- 时间转换UDF函数
- hive 永久udf函数
- hive自定义函数UDF
- hive UDF函数
- hvie UDF函数
- C++中的随机数知识总结
- qt下载资源
- Spring整合Mybatis(一)------MapperScannerConfigurer
- 如何理解虚拟DOM
- 系统内置ContentProvider
- UDF函数
- ARKit从入门到精通(1)-ARKit初体验
- 【第三届蓝桥杯】古堡算式
- 安防智能化结合实战才有意义
- java使用FTPClient实现文件的上传和下载
- 对不可预计参数的form表单进行提交时的参数获取
- 消除button或inline-block元素之间的空白间距
- iPhone屏幕相关
- [view release]: message sent to deallocated instance 0xe250df0