41. job.waitForCompletion(true);
42. }
43.
44. public static class Map extends Mapper
45. private Text id = new Text();
46. private Text row = new Text();
47.
48. public void map(LongWritable key, Text value, Context context) throws IOException,
InterruptedException {
49. String line = value.toString();
50. String[] items = line.split(\
51.
52. if (items.length == 8) {
53. if (StringUtils.isNumeric(items[6])) {
54. id.set(items[0] + \
55. row.set(line);
56. context.write(id, row);
57. }
58. } else {
59. System.out.println(\
60. }
61. }
62. }
63.
64. public static class Reduce extends Reducer
65. private static final SimpleDateFormat format = new SimpleDateFormat(\
HH:mm:ss\ 66.
67. static {
68. format.setLenient(false);
69. }
70.
71. private Text rest = new Text();
72.
73. public void reduce(Text key, Iterable
74. throws IOException, InterruptedException {
75. // Parse row to Record
76. ArrayList
77. for (Text row : values) {
78. String[] items = row.toString().split(\
79. try {
80. Record record = new Record();
81. record.items = items;
82. record.start_time = format.parse(items[3]).getTime();
83. record.stay_time = Long.parseLong(items[6]) * 1000;
84. list.add(record);
85. } catch (ParseException e) {
86. e.printStackTrace();
87. }
88.
89. }
90.
91. // Sort
92. Collections.sort(list, new Comparator
93. @Override
94. public int compare(Record r1, Record r2) {
95. return (int) (r1.start_time - r2.start_time);
96. }
97. });
98.
99. // Find and merge slice
100. ArrayList
101. for (Record r1 : list) {
102. boolean found = false;
103. long r1_stop_time = r1.start_time + r1.stay_time;
104. for (Record r2 : result) {
105. long r2_stop_time = r2.start_time + r2.stay_time;
106. if (r1.start_time > r2.start_time && r1.start_time <= r2_stop_time &&
r1_stop_time > r2_stop_time) {
107. // merge the new slice
108. r2.stay_time = r1_stop_time - r2.start_time;
109. found = true;
110. }
111. }
112.
113. if (!found) {
114. result.add(r1);
115. }
116. }
117.
118. // Output
119. for (Record r : result) {
120. key.set(r.items[0]);
121.
122. String value = r.items[1] + \
123. + r.items[2] + \
124. + r.items[3] + \
125. + r.items[4] + \