forked from xldrx/cloudapp-mp2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrphanPages.java
77 lines (64 loc) · 2.63 KB
/
OrphanPages.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
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.NullWritable;
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.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import java.io.IOException;
import java.util.StringTokenizer;
// >>> Don't Change
public class OrphanPages extends Configured implements Tool {
public static void main(String[] args) throws Exception {
int res = ToolRunner.run(new Configuration(), new OrphanPages(), args);
System.exit(res);
}
// <<< Don't Change
@Override
public int run(String[] args) throws Exception {
Job job = Job.getInstance(this.getConf(), "Orphan Page");
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(NullWritable.class);
job.setMapOutputKeyClass(IntWritable.class);
job.setMapOutputValueClass(IntWritable.class);
job.setMapperClass(LinkCountMap.class);
job.setReducerClass(OrphanPageReduce.class);
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setJarByClass(OrphanPages.class);
return job.waitForCompletion(true) ? 0 : 1;
}
public static class LinkCountMap extends Mapper<Object, Text, IntWritable, IntWritable> {
@Override
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
final String line = value.toString();
final String pages[] = line.split("[:]");
context.write(new IntWritable(Integer.parseInt(pages[0].trim())), new IntWritable(0));
final String links[] = pages[1].split("[ ]");
for(String l : links) {
if (!l.trim().isEmpty()) {
Integer linkId = Integer.parseInt(l.trim());
context.write(new IntWritable(linkId), new IntWritable(1));
}
}
}
}
public static class OrphanPageReduce extends Reducer<IntWritable, IntWritable, IntWritable, NullWritable> {
@Override
public void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int numLink = 0;
for (IntWritable val : values) {
numLink += val.get();
}
if (0 == numLink) {
context.write(key, NullWritable.get());
}
}
}
}