forked from eminem54/Hadoop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordCount.java
383 lines (303 loc) · 11.6 KB
/
WordCount.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.*;
import org.apache.hadoop.mapreduce.lib.output.*;
import java.util.*;
import java.io.*;
import org.apache.commons.logging.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.bson.*;
import org.bson.types.ObjectId;
import com.mongodb.hadoop.*;
import com.mongodb.hadoop.util.*;
public class WordCount {
static String[] topId = new String[5];
static String[] topLo = new String[4];
public static class WordCountMapper extends
Mapper<LongWritable, Text, Text, LongWritable> {
private static LongWritable one = new LongWritable(1);
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
String words[] = line.split(" ");
context.write(new Text(words[0]), one);
}
}
public static class WordCountReducer extends
Reducer<Text, LongWritable, Text, LongWritable> {
static Map<String, Integer> m = new HashMap<String, Integer>();
static ArrayList< Map.Entry<String, Integer> > arr = new ArrayList<>();
@Override
protected void reduce(Text key, Iterable<LongWritable> values,
Context context) throws IOException, InterruptedException {
long count = 0;
for (LongWritable val : values) {
count += val.get();
}
m.put(key.toString(), Integer.parseInt(Long.toString(count)));
}
@Override
protected void cleanup(Context context) throws IOException, InterruptedException {
for( Map.Entry<String, Integer> e:m.entrySet() ) {
arr.add(e);
}
arr.sort((a, b) -> b.getValue() - a.getValue());
for(int i=0; i<5; i++) {
context.write(new Text(arr.get(i).getKey()), new LongWritable(arr.get(i).getValue()));
}
}
}
public static class MWordCountReducer extends
Reducer<Text, LongWritable, Text, LongWritable> {
static Map<String, Integer> m = new HashMap<String, Integer>();
static ArrayList< Map.Entry<String, Integer> > arr = new ArrayList<>();
@Override
protected void reduce(Text key, Iterable<LongWritable> values,
Context context) throws IOException, InterruptedException {
long count = 0;
for (LongWritable val : values) {
count += val.get();
}
m.put(key.toString(), Integer.parseInt(Long.toString(count)));
}
@Override
protected void cleanup(Context context) throws IOException, InterruptedException {
for( Map.Entry<String, Integer> e:m.entrySet() ) {
arr.add(e);
}
arr.sort((a, b) -> b.getValue() - a.getValue());
for(int i=0; i<5; i++) {
context.write(new Text(arr.get(i).getKey()), new LongWritable(arr.get(i).getValue()));
}
}
}
////////////////////////////////////////////////////////////////////////////
public static class LocationCountMapper1 extends Mapper<Text,Text,Text,Text>{
@Override
protected void map(Text key, Text value, Context context) throws IOException, InterruptedException {
context.write(key, new Text(value + "\t" + 1));
}
}
public static class LocationCountMapper
extends Mapper<LongWritable,Text,Text,Text>{
private static LongWritable one = new LongWritable(1);
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line=value.toString();
String words[]=line.split(" ");
context.write(new Text(words[0]), new Text(words[3] + "\t" + 2));
}
}
public static class LocationCountReducer extends
Reducer<Text, Text, Text, Text> {
static Map<String, Integer> m2 = new HashMap<String, Integer>();
@Override
protected void reduce(Text key, Iterable<Text> values,
Context context) throws IOException, InterruptedException {
Map<String, Integer> mtemp = new HashMap<String, Integer>();
int count = 0;
String ID = null;
String Bus = null;
for (Text val : values) {
String str = val.toString();
String[] tokens = str.split("\\t");
if(tokens[1].equals("1")) {
ID = tokens[0];
}
else {
Bus = tokens[0];
mtemp.compute(Bus, (k,v) -> (v==null ? 1:v+1));
}
}
if(ID!=null && Bus!=null){
for(Map.Entry<String, Integer> e : mtemp.entrySet()) {
m2.compute(e.getKey(),(k,v) -> (v==null ? e.getValue():v+e.getValue()));
}
}
}
@Override
protected void cleanup(Context context) throws IOException, InterruptedException {
ArrayList< Map.Entry<String, Integer> > arr2 = new ArrayList<>();
for( Map.Entry<String, Integer> e : m2.entrySet() ) {
arr2.add(e);
}
arr2.sort( (a, b) -> b.getValue() - a.getValue() );
for(int i=0; i<4; i++){
context.write(new Text(arr2.get(i).getKey()), new Text(arr2.get(i).getValue().toString()));
}
}
}
public static class MLocationCountReducer extends
Reducer<Text, Text, Text, Text> {
static Map<String, Integer> m2 = new HashMap<String, Integer>();
@Override
protected void reduce(Text key, Iterable<Text> values,
Context context) throws IOException, InterruptedException {
Map<String, Integer> mtemp = new HashMap<String, Integer>();
int count = 0;
String ID = null;
String Bus = null;
for (Text val : values) {
String str = val.toString();
String[] tokens = str.split("\\t");
if(tokens[1].equals("1")) {
ID = tokens[0];
}
else {
Bus = tokens[0];
mtemp.compute(Bus, (k,v) -> (v==null ? 1:v+1));
}
}
if(ID!=null && Bus!=null){
for(Map.Entry<String, Integer> e : mtemp.entrySet()) {
m2.compute(e.getKey(),(k,v) -> (v==null ? e.getValue():v+e.getValue()));
}
}
}
@Override
protected void cleanup(Context context) throws IOException, InterruptedException {
ArrayList< Map.Entry<String, Integer> > arr2 = new ArrayList<>();
for( Map.Entry<String, Integer> e : m2.entrySet() ) {
arr2.add(e);
}
arr2.sort( (a, b) -> b.getValue() - a.getValue() );
for(int i=0; i<4; i++){
context.write(new Text(arr2.get(i).getKey()), new Text(arr2.get(i).getValue().toString()));
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public static class StationInvertingMapper1 extends Mapper<Text, Text, Text, Text> {
@Override
protected void map(Text key, Text value, Context context) throws IOException, InterruptedException {
context.write(key, new Text(value + "\t" + 1));
}
}
public static class StationInvertingMapper extends Mapper<LongWritable, Text, Text, Text> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line=value.toString();
String words[]=line.split(" ");
context.write(new Text(words[3]), new Text(words[0] + "\t" + 2));
}
}
public static class StationInvertingReducer extends Reducer<Text, Text, Text, Text> {
Map<String, String> mtemp = new HashMap<>();
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
int count = 0;
String ID = null;
String Bus = null;
Set<Integer> userSet = new HashSet<>();
for (Text val : values) {
String str = val.toString();
String[] tokens = str.split("\\t");
if(tokens[1].equals("1")) {
Bus = tokens[0];
}
else {
ID = tokens[0];
userSet.add(Integer.parseInt(tokens[0]));
}
}
if(ID!=null && Bus!=null) {
ArrayList<Integer> sortlist = new ArrayList<>();
for(Integer e:userSet){
sortlist.add(e);
}
Collections.sort(sortlist);
String val = "";
for(int i =0; i<sortlist.size(); i++){
val = val + sortlist.get(i) + " ";
}
mtemp.put(key.toString(), val);
}
}
@Override
protected void cleanup(Context context) throws IOException, InterruptedException {
ArrayList<String> arr2 = new ArrayList<>();
for(Map.Entry<String, String> e:mtemp.entrySet()) {
arr2.add(e.getKey());
}
Collections.sort(arr2);
for(int i=0; i<4; i++) {
context.write(new Text(arr2.get(i)), new Text(mtemp.get(arr2.get(i))));
}
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "word count");
String inputPath="test//input";
String outputPath="test//output";
job.setJarByClass(WordCount.class);
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
FileInputFormat.addInputPath(job, new Path(inputPath));
FileOutputFormat.setOutputPath(job, new Path(outputPath + "//First"));
job.waitForCompletion(true);
//mongodb
Configuration mconf = new Configuration();
MongoConfigUtil.setOutputURI( mconf, "mongodb://localhost/test.outFirst4" );
Job mjob = Job.getInstance(mconf, "word count");
mjob.setJarByClass(WordCount.class);
mjob.setMapperClass(WordCountMapper.class);
mjob.setReducerClass(MWordCountReducer.class);
mjob.setOutputKeyClass(Text.class);
mjob.setOutputValueClass(LongWritable.class);
FileInputFormat.addInputPath(mjob, new Path(inputPath));
mjob.setOutputFormatClass( MongoOutputFormat.class );
mjob.waitForCompletion(true);
/////////////
Configuration conf2=new Configuration();
Job job2 = Job.getInstance(conf2, "Location");
job2.setJarByClass(WordCount.class);
MultipleInputs.addInputPath(job2, new Path(inputPath), TextInputFormat.class, LocationCountMapper.class);
MultipleInputs.addInputPath(job2, new Path(outputPath+"//First"), KeyValueTextInputFormat.class, LocationCountMapper1.class);
FileOutputFormat.setOutputPath(job2, new Path(outputPath + "//Second"));
job2.setReducerClass(LocationCountReducer.class);
job2.setNumReduceTasks(1);
job2.setOutputKeyClass(Text.class);
job2.setOutputValueClass(Text.class);
job2.waitForCompletion(true);
//mongodb
Configuration mconf2=new Configuration();
MongoConfigUtil.setOutputURI( mconf2, "mongodb://localhost/test.outSecond4" );
Job mjob2 = Job.getInstance(mconf2, "Location");
mjob2.setJarByClass(WordCount.class);
MultipleInputs.addInputPath(mjob2, new Path(inputPath), TextInputFormat.class, LocationCountMapper.class);
MultipleInputs.addInputPath(mjob2, new Path(outputPath+"//First"), KeyValueTextInputFormat.class, LocationCountMapper1.class);
mjob2.setReducerClass(MLocationCountReducer.class);
mjob2.setNumReduceTasks(1);
mjob2.setOutputKeyClass(Text.class);
mjob2.setOutputValueClass(Text.class);
mjob2.setOutputFormatClass( MongoOutputFormat.class );
mjob2.waitForCompletion(true);
/////////////////
Configuration conf3=new Configuration();
MongoConfigUtil.setOutputURI( conf3, "mongodb://localhost/test.outFinal4" );
Job job3 = Job.getInstance(conf3, "Indexing");
job3.setJarByClass(WordCount.class);
MultipleInputs.addInputPath(job3, new Path(inputPath), TextInputFormat.class, StationInvertingMapper.class);
MultipleInputs.addInputPath(job3, new Path(outputPath+"//Second"), KeyValueTextInputFormat.class, StationInvertingMapper1.class);
//FileOutputFormat.setOutputPath(job3, new Path(outputPath + "//Final"));
job3.setReducerClass(StationInvertingReducer.class);
job3.setNumReduceTasks(1);
job3.setOutputKeyClass(Text.class);
job3.setOutputValueClass(Text.class);
job3.setOutputFormatClass( MongoOutputFormat.class );
System.exit(job3.waitForCompletion(true) ? 0 : 1);
}
}