-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSortRank.java
106 lines (86 loc) · 2.54 KB
/
SortRank.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
/*
* 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.*;
/*
* sort the results of the PageRank algorithm from highest to lowest
*
* input file format: (<title>, <rank>||<num-links>||<link1>||...||<linkN>)
*
*/
public class SortRank
{
public static final int SORT_MAX = 1000000000;
public static final float SORT_MULT = 100000.0f;
// map class will output (<SORT_MAX - Rank*SORT_MULT>, <Title>)
// for each page
public static class Map extends MapReduceBase
implements Mapper<Text, Text, Text, Text>
{
private Text rankText = new Text();
private Text title = new Text();
public void map(Text key, Text value,
OutputCollector<Text, Text> output,
Reporter reporter)
throws IOException
{
String val0 = key.toString();
String val1 = value.toString();
title.set(val0);
int split = val1.indexOf("||");
float r = new Float(val1.substring(0, split));
int rInt = SORT_MAX - (int)(SORT_MULT * r);
rankText.set(String.valueOf(rInt));
output.collect(rankText, title);
}
}
public static class MapText extends MapReduceBase
implements Mapper<LongWritable, Text, Text, Text>
{
private Text rankText = new Text();
private Text title = new Text();
public void map(LongWritable offset, Text value,
OutputCollector<Text, Text> output,
Reporter reporter)
throws IOException
{
String[] val = value.toString().split("\t");
title.set(val[0]);
int split = val[1].indexOf("||");
float r = new Float(val[1].substring(0, split));
int rInt = SORT_MAX - (int)(SORT_MULT * r);
rankText.set(String.valueOf(rInt));
output.collect(rankText, title);
}
}
// reducer converts rank back to normal value and formats it
public static class Reduce extends MapReduceBase
implements Reducer<Text, Text, Text, Text>
{
private Text valText = new Text();
public void reduce(Text key, Iterator<Text> values,
OutputCollector<Text, Text> output,
Reporter report)
throws IOException
{
while(values.hasNext()){
float val = (float)(SORT_MAX - new Integer(key.toString()))
/SORT_MULT;
//String valString = String.valueOf(val);
String valString = String.format("%12.4f", val);
valText.set(valString);
output.collect(valText, values.next());
}
}
}
}