-
Notifications
You must be signed in to change notification settings - Fork 0
/
WikipediaPopular.java
99 lines (82 loc) · 3.03 KB
/
WikipediaPopular.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
import java.io.*;
import java.util.*;
import java.lang.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.DoubleWritable;
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.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class WikipediaPopular extends Configured implements Tool {
public static class WikipediaPopularMapper
extends Mapper<LongWritable, Text, Text, IntWritable>{
//private final static IntWritable one = new IntWritable(1);
//private static IntWritable count = new IntWritable();
private Text word = new Text();
@Override
public void map(LongWritable key, Text value, Context context
) throws IOException, InterruptedException {
List<String> ls = new ArrayList<String>();
String s = " ";
String[ ] temp = (value.toString()).split("\n");
for(int i=0;i<temp.length;i++){
String[ ] words = temp[i].split(" ");
if(words[1].equalsIgnoreCase("en")) {
if(!words[2].startsWith("Special:")) {
if(!words[2].equalsIgnoreCase("Main_Page")) {
word.set(words[0]);
IntWritable count = new IntWritable(Integer.parseInt(words[3]));
context.write(word, count);
}
}
}
}
}
}
public static class IntSumReducer
extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable();
@Override
public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int max=0;
for (IntWritable val : values) {
if(val.get() > max){
max=val.get();
}
}
result.set(max);
context.write(key, result);
}
}
public static void main(String[] args) throws Exception {
int res = ToolRunner.run(new Configuration(), new WikipediaPopular(), args);
System.exit(res);
}
@Override
public int run(String[] args) throws Exception {
Configuration conf = this.getConf();
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(WikipediaPopular.class);
job.setInputFormatClass(TextInputFormat.class);
job.setMapperClass(WikipediaPopularMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
TextInputFormat.addInputPath(job, new Path(args[0]));
TextOutputFormat.setOutputPath(job, new Path(args[1]));
return job.waitForCompletion(true) ? 0 : 1;
}
}