Home  >  Article  >  Database  >  hive的udf的编写

hive的udf的编写

WBOY
WBOYOriginal
2016-06-07 15:59:121599browse

1.配置环境 1.引入jar包:hive-exec-0.8.0.jar 和 hadoop-core-0.20.2.jar 2.编写udf函数 1.extends UDF 2.重新定义UDF的evaluate函数。 package com.qunar.hiveudf;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;imp

1.配置环境

1.引入jar包:hive-exec-0.8.0.jar 和 hadoop-core-0.20.2.jar

2.编写udf函数

1.extends UDF 2.重新定义UDF的evaluate函数。
package com.qunar.hiveudf;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.hadoop.hive.ql.exec.UDF;

public class IsSundayOrSaturday extends UDF
{
	public Boolean evaluate(String dateString)
	{
		try 
		{
			//将string转化为date
			String str = dateString.split(" ")[0];
			SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
			Date date =sdf.parse(str);
			
			//将date转换为calendar
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			
			//获取date的星期
			int week = calendar.get(Calendar.DAY_OF_WEEK)-1;
			if(week == 0 || week == 6)
			{
				return true;
			}
			else 
			{
				return false;
			}	
		} 
		catch (Exception e) 
		{
			// TODO: handle exception
			return false;
		}
	}
}

3.将jar包引入hadoop环境

1.将jar放入hive的lib的物理目录下;
2.打开hive客户端,加入jar包 add jar /home/hive/lib/HiveUDF.jar
3.创建一个临时文件 create temporary function IsSundayOrSaturday AS 'com.qunar.hiveudf.IsSundayOrSaturday'

4.调用函数 select IsSundayOrSaturday(intime) from user_tag

注意:

1.如果函数的参数中含有文件,则先要将文件放入hive的运行环境中 add file /home/filename
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn