-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPageRank.java
178 lines (145 loc) · 4.66 KB
/
PageRank.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
/*
* cs292 homework6
*
* Gaurav Gupta
*/
package cs292.hw6;
import cs292.hw6.RunPageRank;
import java.io.IOException;
import java.util.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;
/*
* page rank algorithm
*
* input file format: (<title>, <rank-and-links>)
* where <rank-and-links> = "<page-rank>||<num-links>||<Link1>||...||<LinkN>"
*
* output has two kinds of records,
* one link record for each page:
* (<title>, <"##links##||<num-links>||<Link1>||...||<LinkN>">
* and a rank-contribution record for each link on the page:
* (<Link>, <rank-contribution>)
*/
public class PageRank
{
private static final String linkIndicator = "##links##";
// mapper will output two kinds of records,
// for each link, the rank contribution from page being processed:
// (<Link>, <rank-contrib>)
// for the page being processed, the coded links:
// (<Title>, "##links##||<coded-links>")
public static class Map extends MapReduceBase
implements Mapper<Text, Text, Text, Text>
{
private String code;
private Text link = new Text();
private Text rankContribText = new Text();
private Text codeText = new Text();
private float rank, count;
public void map(Text key, Text value,
OutputCollector<Text, Text> output,
Reporter reporter)
throws IOException
{
code = value.toString();
// code = <rank>||<num-links>||<link1>||...||<linkN>
int split = code.indexOf("||");
rank = new Float(code.substring(0, split));
code = code.substring(split+2);
// code now = <num-links>||<link1>||...||<linkN>
// output the ##links## record
codeText.set(linkIndicator + "||" + code);
output.collect(key, codeText);
split = code.indexOf("||");
// if split < 0 then there are no links
if(split >= 0){
count = new Float(code.substring(0, split));
code = code.substring(split+2);
// code now = <link1>||...||<linkN>
// output the rank-contribution for each link
String[] links = code.split("\\|\\|");
for(String l : links){
link.set(l);
rankContribText.set(String.valueOf(rank/count));
output.collect(link, rankContribText);
}
}
}
}
public static class MapText extends MapReduceBase
implements Mapper<LongWritable, Text, Text, Text>
{
private String code;
private Text link = new Text();
private Text rankContribText = new Text();
private Text codeText = new Text();
private float rank, count;
public void map(LongWritable offset, Text value,
OutputCollector<Text, Text> output,
Reporter reporter)
throws IOException
{
Text key = new Text();
String[] val = value.toString().split("\t");
key.set(val[0]);
value.set(val[1]);
code = value.toString();
// code = <rank>||<num-links>||<link1>||...||<linkN>
int split = code.indexOf("||");
rank = new Float(code.substring(0, split));
code = code.substring(split+2);
// code now = <num-links>||<link1>||...||<linkN>
// output the ##links## record
codeText.set(linkIndicator + "||" + code);
output.collect(key, codeText);
split = code.indexOf("||");
// if split < 0 then there are no links
if(split >= 0){
count = new Float(code.substring(0, split));
code = code.substring(split+2);
// code now = <link1>||...||<linkN>
// output the rank-contribution for each link
String[] links = code.split("\\|\\|");
for(String l : links){
link.set(l);
rankContribText.set(String.valueOf(rank/count));
output.collect(link, rankContribText);
}
}
}
}
// reducer outputs (<title>, <rank-and-links>)
public static class Reduce extends MapReduceBase
implements Reducer<Text, Text, Text, Text>
{
private Text outCodeText = new Text();
public void reduce(Text key, Iterator<Text> values,
OutputCollector<Text, Text> output,
Reporter reporter)
throws IOException
{
String codedLinks = "0";
float rank = 1 - RunPageRank.discount;
while(values.hasNext()){
String inCode = values.next().toString();
int split = inCode.indexOf("||");
//String part1 = (split >= 0) ? inCode.substring(0, split)
// : inCode;
if(split < 0){
// this record just has the rank contribution
rank += RunPageRank.discount * new Float(inCode);
}else{
// this record has the coded links
codedLinks = inCode.substring(split + 2);
}
}
// output the rank and coded links
outCodeText.set(String.valueOf(rank) + "||" + codedLinks);
output.collect(key, outCodeText);
}
}
}