our project use hive 0.10 , and in the hiveql , we need use addMonths function builtin in hive-0.11.

so I write this udf and test.

java code:

package myudf;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; import org.apache.hadoop.hive.ql.exec.UDF; public class addMonths extends UDF {
final Calendar calendar = Calendar.getInstance(); // String Year_month int numMonths
// return date add
public String evaluate(String _date, int numMonths) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
addMonth(sdf.parse(_date), numMonths);
int month = calendar.get(Calendar.MONTH) + 1;
String strmonth = String.valueOf(month > 9 ? month : "0" + month);
String strYearMon = calendar.get(Calendar.YEAR) + strmonth;
return strYearMon;
} Calendar addMonth(Date d, int numMonths) {
calendar.setTime(d);
boolean lastDatOfMonth = isLastDayOfMonth(calendar);
calendar.add(Calendar.MONTH, numMonths);
if (lastDatOfMonth) {
int maxDd = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, maxDd);
}
return calendar;
} boolean isLastDayOfMonth(Calendar cal) {
int maxDd = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
int dd = cal.get(Calendar.DAY_OF_MONTH);
return dd == maxDd;
} }
cd workspace and project folder. then 
jar cvf myudf.jar ./
move to proper folder: mv myudf.jar /home/hadoop/jar/myudf.jar

add jar /home/hadoop/jar/myudf.jar;
create temporary function addMonths as "myudf.addMonths";
select addMonths('2012-03-03',2) as newdate, addMonths('2012-03-03',-2) as newdate2  from nums where num<2;

 
 

Hive UDF’S addMonths的更多相关文章

  1. Hive UDF初探

    1. 引言 在前一篇中,解决了Hive表中复杂数据结构平铺化以导入Kylin的问题,但是平铺之后计算广告日志的曝光PV是翻倍的,因为一个用户对应于多个标签.所以,为了计算曝光PV,我们得另外创建视图. ...

  2. Hive UDF 实验1

    项目中使用的hive版本低于0.11,无法使用hive在0.11中新加的开窗分析函数. 在项目中需要使用到row_number()函数的地方,有人写了udf来实现这个功能. new java proj ...

  3. hive UDF添加方式

    hive UDF添加的方式 1.添加临时函数,只能在此会话中生效,退出hive自动失效 hive> add jar /home/jtdata/hiveUDF/out0.jar; Added [/ ...

  4. DeveloperGuide Hive UDF

    Creating Custom UDFs First, you need to create a new class that extends UDF, with one or more method ...

  5. [转]HIVE UDF/UDAF/UDTF的Map Reduce代码框架模板

    FROM : http://hugh-wangp.iteye.com/blog/1472371 自己写代码时候的利用到的模板   UDF步骤: 1.必须继承org.apache.hadoop.hive ...

  6. 2、Hive UDF编程实例

    Hive的UDF包括3种:UDF(User-Defined Function).UDAF(User-Defined Aggregate Function)和UDTF(User-Defined Tabl ...

  7. Hive UDF 用户自定义函数 编程及使用

    首先创建工程编写UDF 代码,示例如下: 1. 新建Maven项目 udf 本机Hadoop版本为2.7.7, Hive版本为1.2.2,所以选择对应版本的jar ,其它版本也不影响编译. 2. po ...

  8. Hive UDF开发-简介

    Hive进行UDF开发十分简单,此处所说UDF为Temporary的function,所以需要hive版本在0.4.0以上才可以. Hive的UDF开发只需要重构UDF类的evaluate函数即可.例 ...

  9. 【转】HIVE UDF UDAF UDTF 区别 使用

    原博文出自于:http://blog.csdn.net/longzilong216/article/details/23921235(暂时) 感谢! 自己写代码时候的利用到的模板   UDF步骤: 1 ...

随机推荐

  1. iOS开发之蓝牙通讯

    iOS开发之蓝牙通讯 一.引言 蓝牙是设备近距离通信的一种方便手段,在iPhone引入蓝牙4.0后,设备之间的通讯变得更加简单.相关的蓝牙操作由专门的CoreBluetooth.framework进行 ...

  2. KMA & ex_KMP---Revolving Digits

    HDU  4333 Description One day Silence is interested in revolving the digits of a positive integer. I ...

  3. Represent code in math equations

    Introduce The article shows a way to use math equations to represent code's logical. Key ideas logic ...

  4. eclipse:File->New没有Android Application Project的解决办法

    我的Eclipse版本是:Kepler Service Release 1,截图: 解决步骤: 1.单击Window,选择Customize Perspective,如图: 2.勾选Android A ...

  5. webform(内置对象)

    一.内置对象 (一)Response - 响应请求对象1.定义:Response对象用于动态响应客户端请示,控制发送给用户的信息,并将动态生成响应.Response对象只提供了一个数据集合cookie ...

  6. C# 枚举、字符串、值的相互转换

    using  System; class  Program{    public   enum  Color   {      Red  =   0xff0000 ,      Orange  =   ...

  7. gulp小记(无刷新重载样式)

    之前在使用sass的时候,使用了一个不错的工具koala,其实它的原理就是监视sass文件的变化,去编译css而gulp也能为我们做这样的事并且更多 使用gulp之前我们要做一些准备工作 1)安装no ...

  8. AngularJS的指令用法

    scope的绑定策略: @ :把当前属性作为字符串传递,你还可以绑定来自外层scope的值,在属性值中插入 {{}}即可 示例代码: scopeAt.html <!doctype html> ...

  9. QT5中全屏显示子窗口和取消全屏的方法

    问题描述:用QT5做了个MDI多窗体应用程序,想把子窗体全屏显示,用网上的方法,但总是遇到问题. 网上的解决方法原文在这:http://www.cnblogs.com/Rick-w/archive/2 ...

  10. SharePoint 2013 使用PowerShell创建State Service

    今天,搞SPD配置的sp2010wf迁移到sp2013环境上去,发布解决方案都很正常,给列表添加wf的时候报错“该表单无法显示,可能是由于 Microsoft SharePoint Server St ...