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'] ];





你可能感兴趣的:(hive)