HADOOP笔试(2)

2019-08-31 15:53

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 values, Context context)

74. throws IOException, InterruptedException {

75. // Parse row to Record

76. ArrayList list = new 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 result = new 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] + \


HADOOP笔试(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:关于狗的经典对联精选

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: