UDF函数

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 null
		if (salaryText == null) {
			return null;
		}
		
		String salaryStr = salaryText.toString();
		
		//2.if salary is not double type
		double 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'] ];





  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值