six month dormancy test
source data:
accountleg year_month amount
acc1A 2010-01 100acc1A 2010-02 100
acc1A 2010-03 100
acc1A 2010-04 100
acc1A 2010-06 100
acc1A 2010-07 100
acc1A 2010-08 100
acc1A 2010-09 100
acc1A 2010-10 100
acc1A 2010-11 100
acc1A 2011-06 100
acc1A 2011-07 100
acc1A 2011-08 100
acc1A 2011-09 100
acc1A 2011-10 100
acc1A 2011-11 100
acc1A 2011-12 100
acc1A 2012-01 100
acc1A 2012-07 100
create table sixdormancy (accountleg string,year_month string,amount double) row format delimited fields terminated by '\t';
load data local inpath '/mnt/data/sixdormancy.txt' into table sixdormancy;
--get the last row year_month
drop table sixdormancy_lastmonth;
create table sixdormancy_lastmonth as
select
*,
lag(year_month) over(partition by accountleg order by year_month) as lastmonth
from sixdormancy; create table sixdormancy_monthdiff as
select *,
(year(concat(year_month,'-01')) - year(concat(lastmonth,'-01')))*12
+month(concat(year_month,'-01'))-
month(concat(lastmonth,'-01')) as monthdiff
from sixdormancy_lastmonth; select accountleg from sixdormancy_monthdiff where monthdiff>5 group by accountleg; if 0.10 not support lag function, we can write one udf to do this, and then we can combine
the calculation and filter and the udf.
package myudf; import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException; import org.apache.hadoop.hive.ql.exec.UDF; public class dormancy extends UDF { String accountleg = "";
String predate = "";
boolean isDormancy = false; public boolean evaluate(String _accountleg, String _date) {
isDormancy=false;
if (accountleg.equalsIgnoreCase(_accountleg)) {
isDormancy = hasSixMonthsGap(predate, _date);
}
accountleg = _accountleg;
predate = _date;
return isDormancy;
} boolean hasSixMonthsGap(String _sd, String _bd) {
// issue yyyy-MM
int year1 = Integer.parseInt(_bd.substring(1, 4));
int year2 = Integer.parseInt(_sd.substring(1, 4));
int month1 = Integer.parseInt(_bd.substring(5, 7));
int month2 = Integer.parseInt(_sd.substring(5, 7));
int cp = (year1 - year2) * 12 + (month1 - month2) + 1;
if (cp > 7) // has dormancy
return true;
else
return false;
} public static void main(String[] args) { dormancy test = new dormancy();
// read data from source
String filepath = "/mnt/data/sixdormancy.txt";
try {
BufferedReader br = new BufferedReader(new FileReader(filepath));
String line; line = br.readLine();
String[] items = null;
while (line != null) {
// handle this line data
items = line.split("\t");
System.out.print(line);
System.out.print("\t");
System.out.println(test.evaluate(items[0], items[1]));
line = br.readLine();
} } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
} } }
add jar /home/hadoop/workspace/myudf/bin/myudf.jar;
create temporary function dormancy as "myudf.dormancy";
select *,dormancy(accountleg,year_month) from
(select * from sixdormancy distribute by accountleg sort by accountleg, year_month) a;

six month dormancy test的更多相关文章
- This month Calendar
package fourth;import java.text.DateFormatSymbols;import java.util.*;public class CalendarTest { pub ...
- Time.MONTH及Calendar.MONTH 默认的月份为 0-11
Time.MONTH及Calendar.MONTH 默认的月份为 0-11 所以使用的时候要自己加1.
- Calendar.get()方法--- WEEK_OF_YEAR 、MONTH、
1. WEEK_OF_YEAR 一年中的第几周 由于西方的一周指的是:星期日-星期六,星期日是一周的第一天,星期六是一周的最后一天, 所以,使用 calendar.get(Calendar.WEE ...
- JavaScript中的setMonth()方法的小问题 解决:setMonth(month, 1)
今天测试人员发现一个问题,从英文日期转化中文日期,月份总会有“6月”变为“7月”.于是我在本地反复测试,发现如下规律:只要setMonth()的参数为小于31天的月份时就会变为下一个月. 原因是:因为 ...
- StackOverflow Update: 560M Pageviews A Month, 25 Servers, And It's All About Performance
http://highscalability.com/blog/2014/7/21/stackoverflow-update-560m-pageviews-a-month-25-servers-and ...
- Week,Month, Year 日期区间辅助类
我们在做一些业务系统的时候,经常会用到一些获取时间段的情况.比如要统计某一周.某月.某年 这样一些时间区间内的一些业务数据.这时候我们就需要获取当前时间段内的一些起止日期.这里分享一个通用的日期辅助类 ...
- SQL 标量函数-----日期函数datediff()、 day() 、month()、year()
select day(createtime) from life_unite_product --取时间字段的天值 select month(createtime) from life_uni ...
- [ActionScript 3.0] AS3 获取某年某月的天数(Get number of days in a month)
function getNumberOfDays($year:int, $month:int):int { var month:Date = new Date($year, $month + 1, 0 ...
- Month Calendar
http://www.codeproject.com/Articles/10840/Another-Month-Calendar#xx4614180xx Another Month Calendar ...
随机推荐
- 【jQuery基础学习】12 jQuery学习感想
学习完<锋利的jQuery>,用时13天. 这期间,私底下又用了一点时间去W3C上把HTML和CSS重新过了一遍. 总的来说,收获还是蛮多的. 其实在本书里面真正重要的也就前几章,后面的都 ...
- IIS理解
WEB开发基础 1IIS原理 IIS的本质其实就是一个sorket的服务器,浏览器就是一个sorket的客户端,浏览器发送请求信息给IIS,IIS返回信息给浏览器显示,就这么简单. 1http.sys ...
- 用Perl编写Apache模块续 - SVNAuth
目标:以整合各类项目管理系统中的用户体系及权限体系为目标,实现SVN的集成式动态鉴权 支持平台:usvn.禅道等 - 开发中,本文仅对前期的探索工作做点整理 开发环境: Windows.XAMPP 1 ...
- jquery 全选 全不选 反选
1.概述 在项目中经常遇到列表中对复选框进行勾选操作,全选...反选.. 2. example <html> <body> <form id="test-for ...
- python3学习笔记目录
目录: Python基础(一),Day1 python基础(二),Day2 python函数和常用模块(一),Day3 python函数和常用模块(二),Day4 python函数和常用模块(三),D ...
- Verilog学习笔记简单功能实现(三)...............同步有限状态机
在Verilog中可以采用多种方法来描述有限状态机最常见的方法就是用always和case语句.如下图所示的状态转移图就表示了一个简单的有限状态机: 图中:图表示了一个四状态的状态机,输入为A和Res ...
- ahjesus sql手动重新更新ID
declare cus_cursor cursor scroll for SELECT Id from [dbo].[TLotterySpiderRule] open cus_cursor decla ...
- Sharepoint 2013 关于"SPChange"简介
在SharePoint中,我们经常会需要获取那些改变的项目,其实api为我们提供了SPChange对象,下面,我们通过列表简单介绍下这一对象. 1.创建一个测试列表,名字叫做“SPChangeItem ...
- WPF 自定义控件,在ViewModel里面获取自定义控件的值
上图: 用户自定义CS里面代码如下: 自定义控件XAML里面的代码如下: 调用用户自定义控件的页面代码如下: CItySelected的属性值就是我们点击确定按钮以后得到的值,通过双向绑定在VIewM ...
- iOS之UI--CAShapeLayer
关于CAShapeLayer 内容大纲: CAShapeLayer简介 贝塞尔曲线与CAShapeLayer的关系 strokeStart和strokeEnd 动画 用CAShapeLayer实现进度 ...