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 ...
随机推荐
- Netty学习之客户端创建
一.客户端开发时序图 图片来源:Netty权威指南(第2版) 二.Netty客户端开发步骤 使用Netty进行客户端开发主要有以下几个步骤: 1.用户线程创建Bootstrap Bootstrap b ...
- C#的timer类
在C#里关于定时器类就有3个 1.定义在System.Windows.Forms里 2.定义在System.Threading.Timer类里 3.定义在System.Timers.Timer类里 S ...
- csharp:Chart
http://www.dotnetperls.com/chart using System; using System.Windows.Forms; using System.Windows.Form ...
- SpreadJS 中应用 KnockoutJS 技术
SpreadJS 支持 Knockout (KO)技术, KnockoutJS 是一个使用 MVVM 模式的 JavaScript 库,允许双向数据绑定,使数据和UI界面进行实时的交互更新.关于KO的 ...
- MyEclipse+Mysql (二)
上一节介绍了如何在Myeclipse中连接mysql 这一节介绍如何在java程序中访问mysql数据库中的数据b并进行简单的操作 创建一个javaProject,并输入如下java代码: packa ...
- php实现快速排序
下午练习时候,把经典排序快速排序做了,以下是我的代码 <?php /** * Created by PhpStorm. * User: Administrator * Date: 16-8-29 ...
- JavaScripts 基础详细笔记整理
一.JS简介 JavaScript 是 Web 的编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理 ...
- SQL数据库基础(四)
聚合函数:sum,avg,max,min,count 使用方法示例: group by 分组的使用方法 分组的练习: 数学函数:ABS.ceiling.floor.power.round.sqrt. ...
- 个人收集整理的5Ucms标签
{field:cid} 当前栏目id {field:id} 当前页面id {field:content} 当前页面内容 [List:Modifytime $format=yy-mm-dd] 文章发布 ...
- JavaScript If...Else、Switch、For、While、Break、Continue语句
一,JavaScript If...Else 语句 条件语句 通常在写代码时,您总是需要为不同的决定来执行不同的动作.您可以在代码中使用条件语句来完成该任务. 在 JavaScript 中,我们可使用 ...