-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageRankDriver.java
More file actions
167 lines (97 loc) · 4.13 KB
/
PageRankDriver.java
File metadata and controls
167 lines (97 loc) · 4.13 KB
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
package bigdata;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.ObjectWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class PageRankDriver extends Configured implements Tool {
public static double BETA=0.8;
public static enum Counter {
totalNodes,
sumOfPageRanks;
}
@Override
public int run(String[] args) throws Exception {
if (args.length != 2) {
System.out.printf("Usage: " + this.getClass().getName() + " <input dir> <output dir>\n");
return -1;
}
int iterationCount=0;
while (true)
{
System.out.println("Running Map Reduce Job!!!!!!");
///////////////////////////First Map Reduce Job Which calculates S///////////////////////////////////////
Job job_s = Job.getInstance(getConf());
job_s.setJarByClass(PageRankDriver.class);
job_s.setJobName("S_Calculation");
FileSystem fs = FileSystem.get(new Configuration());
fs.delete(new Path("/Temp"), true);
fs.delete(new Path(args[1]), true);
FileInputFormat.setInputPaths(job_s, new Path(args[0]));
FileOutputFormat.setOutputPath(job_s, new Path("/Temp"));
job_s.setReducerClass(S_Calculation_Reducer.class);
job_s.setMapperClass(S_Calculation_Mapper.class);
job_s.setMapOutputKeyClass(IntWritable.class);
job_s.setMapOutputValueClass(DoubleWritable.class);
/*
* Set the key output class for the job_s
*/
job_s.setOutputKeyClass(DoubleWritable.class);
/*
* Set the value output class for the job_s
*/
job_s.setOutputValueClass(NullWritable.class);
job_s.setNumReduceTasks(1);
job_s.waitForCompletion(true); //the first job calculates S.
double sumOfPageRanks= (double) job_s.getCounters().findCounter(Counter.sumOfPageRanks).getValue()/ (double)10000.0; //getting sum of page ranks S from the previous job
System.out.println("S: "+sumOfPageRanks);
fs.delete(new Path("/Temp"),true);
/////////////////////////Second map reduce job which calculates page rank of each node////////////////////////////////////////////////
Job job = Job.getInstance(getConf());
job.setJarByClass(PageRankDriver.class);
//job.setJobName("Page Rank");
job.getConfiguration().setDouble("sumOfPageRanks", sumOfPageRanks); //passing sum of Page ranks S.
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setReducerClass(PageRankReducer.class);
job.setMapperClass(PageRankMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(ObjectWritable.class);
/*
* Set the key output class for the job
*/
job.setOutputKeyClass(Text.class);
/*
* Set the value output class for the job
*/
job.setOutputValueClass(Text.class);
boolean success = job.waitForCompletion(true);
iterationCount++;
if (iterationCount>2)
{
fs.delete(new Path(args[0]), true); //deleting folder before exiting
System.out.println("Total Number of Times the Job was run= "+iterationCount);
System.out.println("total Nodes Processed: "+job.getCounters().findCounter(Counter.totalNodes).getValue());
return (success==true ? 1: 0);
}
else
{
fs.delete(new Path(args[0]), true);
fs.rename(new Path(args[1]), new Path(args[0]));
}
}
}
public static void main(String[] args) throws Exception {
int exitCode = ToolRunner.run(new Configuration(), new PageRankDriver(), args);
System.exit(exitCode);
}
}