source data:

accountleg    year_month    amount
acc1A    2010-01    100

acc1A    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的更多相关文章

  1. This month Calendar

    package fourth;import java.text.DateFormatSymbols;import java.util.*;public class CalendarTest { pub ...

  2. Time.MONTH及Calendar.MONTH 默认的月份为 0-11

    Time.MONTH及Calendar.MONTH 默认的月份为  0-11 所以使用的时候要自己加1.

  3. Calendar.get()方法--- WEEK_OF_YEAR 、MONTH、

    1. WEEK_OF_YEAR   一年中的第几周 由于西方的一周指的是:星期日-星期六,星期日是一周的第一天,星期六是一周的最后一天, 所以,使用 calendar.get(Calendar.WEE ...

  4. JavaScript中的setMonth()方法的小问题 解决:setMonth(month, 1)

    今天测试人员发现一个问题,从英文日期转化中文日期,月份总会有“6月”变为“7月”.于是我在本地反复测试,发现如下规律:只要setMonth()的参数为小于31天的月份时就会变为下一个月. 原因是:因为 ...

  5. 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 ...

  6. Week,Month, Year 日期区间辅助类

    我们在做一些业务系统的时候,经常会用到一些获取时间段的情况.比如要统计某一周.某月.某年 这样一些时间区间内的一些业务数据.这时候我们就需要获取当前时间段内的一些起止日期.这里分享一个通用的日期辅助类 ...

  7. SQL 标量函数-----日期函数datediff()、 day() 、month()、year()

    select day(createtime) from life_unite_product     --取时间字段的天值 select month(createtime) from life_uni ...

  8. [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 ...

  9. Month Calendar

    http://www.codeproject.com/Articles/10840/Another-Month-Calendar#xx4614180xx Another Month Calendar ...

随机推荐

  1. 引入js和css文件的总结

    1.用script标签引入javascript时,浏览器对于javascript的加载某些是并行的,某些是串行的,如IE8,Chorme2和firefox3都是串行加载的. 2.charset编码也就 ...

  2. 合并多个dll为一个dll

    有时候自己写个小工具或者其它啥的物件,引用了好多第三方控件,如log4net,aspnetpager啥的,发布出去让别人看到自己竟然用了这么多的第三方DLL, 会对自己的能力产生怀疑,那有什么办法可以 ...

  3. FreeBSD 10 发布

    发行注记:http://www.freebsd.org/releases/10.0R/relnotes.html 下文翻译中... 主要有安全问题修复.新的驱动与硬件支持.新的命名/选项.主要bug修 ...

  4. Verilog学习笔记设计和验证篇(二)...............同步有限状态机

    上图表示的就是数字电路设计中常用的时钟同步状态机的结构.其中共有四个部分产生下一状态的组合逻辑F.状态寄存器组.输出组合逻辑G.流水线输出寄存器组.如果状态寄存器组由n个寄存器组成,就可以记忆2^n个 ...

  5. phpcms--使用添加php原生支持

    1,phpcms模板中有时候要添加一些php相关变量这个时候要使用原始php的东西,可以如下加入 {php $no_wq_id=$r[id] ;}其中$r[id]是通过{pc:get sql=&quo ...

  6. Ogre2.1 灯光与阴影

    Ogre2.1大量光源渲染 Ogre2.1不是采用现在大部分引擎所用的延迟渲染,而是采用一种前向渲染的改进技术,理论基本来自于Forward+,见如下. http://www.klayge.org/? ...

  7. UIMenuController的使用

    1, 基本使用 以对一个UILabel长按弹出菜单为例 子类化UILabel 因为需要覆盖这几个方法:- (BOOL)canBecomeFirstResponder; 返回YES 同时需要在每次UI元 ...

  8. Java学习笔记之_JDBC

    JDBC简介 1.SUN公司为了简化,统一数据库的操作,定义了一套Java操作数据库的规范,称之为JDBC 2.数据库驱动 3.JDBC全称为:JAVA DataBase Commectivity(j ...

  9. rails provide与content_for的区别

    页面渲染时:provide先执行,但找到一个provide之后就不再查找 content_for 顺序执行,在哪个位置,就等之前的渲染完后才执行.但是要等到所有的content被查找完后一块返回,也就 ...

  10. 转:Eclipse 一直不停 building workspace... 完美解决总结

    原文地址: Eclipse 一直不停 building workspace... android开发论坛 juapk 完美解决总结 一.产生这个问题的原因多种 1.自动升级 2.未正确关闭  3.ma ...