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 ...
随机推荐
- FreeBSD10上编译尝试DeepIn UI
经历了两百多次命令的输入尝试,终于搞定. 1 git clone https://github.com/linuxdeepin/deepin-ui.git 11 git clone https://g ...
- Unity3D脚本语言UnityScript初探
译者注: Unity3D中支持三种语言:JavaScript.C#.Boo,很多人不知道如何选择,通过这篇译文,我们可以搞清楚这三者语言的来龙去脉,对选择主语言有一定的借鉴意义. 首先,Unity是基 ...
- eclipse新建maven项目(1)
首先看一下eclipse版本,我用的是最新版Mars2. 下载地址自行搜索关键字:“eclipse官网”即可,注意下版本,32bit or 64bit. maven插件以及svn等相关插件安装设置问题 ...
- UGUI之Toggle使用
Toggle对象是一个开关.一般用于单选,可以用Toggle制作背包的选项卡 在场景中创建Toggle按钮.看看他有Toggle组件
- POJ 1836 Alignment 最长递增子序列(LIS)的变形
大致题意:给出一队士兵的身高,一开始不是按身高排序的.要求最少的人出列,使原序列的士兵的身高先递增后递减. 求递增和递减不难想到递增子序列,要求最少的人出列,也就是原队列的人要最多. 1 2 3 4 ...
- 【Asphyre引擎】发布了新版本V101
引擎简称还是PXL,但是这个P是Platform而不是Pascal. 修复了一些bug,增加了轻量级的随机数发生器,进一步完善了XML的解析. 不是很明白,为何把Pascal扩展库改成Platform ...
- PPP模式下的融资结构优化
PPPcode{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && d ...
- [Tips] Useful link ... on going
1. CSS Bootstrap http://getbootstrap.com/ Bootstrap 中文文档 http://getbootstrap.com/2.3.2/ 最全的 Twitter ...
- SQLserver2008如何把表格变量传递到存储过程中
在Microsoft SQL Server 2008中,你可以实现把表格变量传递到存储过程中,如果变量可以被声明,那么它就可以被传递.下面我们来具体介绍如何把表格变量(包括内含的数据)传递到存储过程和 ...
- win10应用部署到手机出现问题Exception from HRESULT: 0x80073CFD
今天把应用部署到手机上时,出现了这样的问题 Exception from HRESULT: 0x80073CFD 具体错误是: Error Error : DEP0001 : Unexpected E ...