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

  1.  
  1. create table sixdormancy (accountleg string,year_month string,amount double) row format delimited fields terminated by '\t';
  2. load data local inpath '/mnt/data/sixdormancy.txt' into table sixdormancy;
  3. --get the last row year_month
  4. drop table sixdormancy_lastmonth;
  5. create table sixdormancy_lastmonth as
  6. select
  7. *,
  8. lag(year_month) over(partition by accountleg order by year_month) as lastmonth
  9. from sixdormancy;
  10.  
  11. create table sixdormancy_monthdiff as
  12. select *,
  13. (year(concat(year_month,'-01')) - year(concat(lastmonth,'-01')))*12
  14. +month(concat(year_month,'-01'))-
  15. month(concat(lastmonth,'-01')) as monthdiff
  16. from sixdormancy_lastmonth;
  17.  
  18. select accountleg from sixdormancy_monthdiff where monthdiff>5 group by accountleg;
  19.  
  20. if 0.10 not support lag function, we can write one udf to do this, and then we can combine
  21. the calculation and filter and the udf.
  1. package myudf;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7.  
  8. import org.apache.hadoop.hive.ql.exec.UDF;
  9.  
  10. public class dormancy extends UDF {
  11.  
  12. String accountleg = "";
  13. String predate = "";
  14. boolean isDormancy = false;
  15.  
  16. public boolean evaluate(String _accountleg, String _date) {
        isDormancy=false;
  17. if (accountleg.equalsIgnoreCase(_accountleg)) {
  18. isDormancy = hasSixMonthsGap(predate, _date);
  19. }
  20. accountleg = _accountleg;
  21. predate = _date;
  22. return isDormancy;
  23. }
  24.  
  25. boolean hasSixMonthsGap(String _sd, String _bd) {
  26. // issue yyyy-MM
  27. int year1 = Integer.parseInt(_bd.substring(1, 4));
  28. int year2 = Integer.parseInt(_sd.substring(1, 4));
  29. int month1 = Integer.parseInt(_bd.substring(5, 7));
  30. int month2 = Integer.parseInt(_sd.substring(5, 7));
  31. int cp = (year1 - year2) * 12 + (month1 - month2) + 1;
  32. if (cp > 7) // has dormancy
  33. return true;
  34. else
  35. return false;
  36. }
  37.  
  38. public static void main(String[] args) {
  39.  
  40. dormancy test = new dormancy();
  41. // read data from source
  42. String filepath = "/mnt/data/sixdormancy.txt";
  43. try {
  44. BufferedReader br = new BufferedReader(new FileReader(filepath));
  45. String line;
  46.  
  47. line = br.readLine();
  48. String[] items = null;
  49. while (line != null) {
  50. // handle this line data
  51. items = line.split("\t");
  52. System.out.print(line);
  53. System.out.print("\t");
  54. System.out.println(test.evaluate(items[0], items[1]));
  55. line = br.readLine();
  56. }
  57.  
  58. } catch (FileNotFoundException e) {
  59. e.printStackTrace();
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. } catch (Exception ex) {
  63. ex.printStackTrace();
  64. }
  65.  
  66. }
  67.  
  68. }
  1. add jar /home/hadoop/workspace/myudf/bin/myudf.jar;
  2. create temporary function dormancy as "myudf.dormancy";
  3. select *,dormancy(accountleg,year_month) from
  4. (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. FreeBSD10上编译尝试DeepIn UI

    经历了两百多次命令的输入尝试,终于搞定. 1 git clone https://github.com/linuxdeepin/deepin-ui.git 11 git clone https://g ...

  2. Unity3D脚本语言UnityScript初探

    译者注: Unity3D中支持三种语言:JavaScript.C#.Boo,很多人不知道如何选择,通过这篇译文,我们可以搞清楚这三者语言的来龙去脉,对选择主语言有一定的借鉴意义. 首先,Unity是基 ...

  3. eclipse新建maven项目(1)

    首先看一下eclipse版本,我用的是最新版Mars2. 下载地址自行搜索关键字:“eclipse官网”即可,注意下版本,32bit or 64bit. maven插件以及svn等相关插件安装设置问题 ...

  4. UGUI之Toggle使用

    Toggle对象是一个开关.一般用于单选,可以用Toggle制作背包的选项卡 在场景中创建Toggle按钮.看看他有Toggle组件

  5. POJ 1836 Alignment 最长递增子序列(LIS)的变形

    大致题意:给出一队士兵的身高,一开始不是按身高排序的.要求最少的人出列,使原序列的士兵的身高先递增后递减. 求递增和递减不难想到递增子序列,要求最少的人出列,也就是原队列的人要最多. 1 2 3 4 ...

  6. 【Asphyre引擎】发布了新版本V101

    引擎简称还是PXL,但是这个P是Platform而不是Pascal. 修复了一些bug,增加了轻量级的随机数发生器,进一步完善了XML的解析. 不是很明白,为何把Pascal扩展库改成Platform ...

  7. PPP模式下的融资结构优化

    PPPcode{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && d ...

  8. [Tips] Useful link ... on going

    1. CSS Bootstrap http://getbootstrap.com/ Bootstrap 中文文档 http://getbootstrap.com/2.3.2/ 最全的 Twitter ...

  9. SQLserver2008如何把表格变量传递到存储过程中

    在Microsoft SQL Server 2008中,你可以实现把表格变量传递到存储过程中,如果变量可以被声明,那么它就可以被传递.下面我们来具体介绍如何把表格变量(包括内含的数据)传递到存储过程和 ...

  10. win10应用部署到手机出现问题Exception from HRESULT: 0x80073CFD

    今天把应用部署到手机上时,出现了这样的问题 Exception from HRESULT: 0x80073CFD 具体错误是: Error Error : DEP0001 : Unexpected E ...