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





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章