-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangleCounter.java
More file actions
230 lines (188 loc) · 8.32 KB
/
Copy pathTriangleCounter.java
File metadata and controls
230 lines (188 loc) · 8.32 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package org.apache.hadoop.examples;
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.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.*;
import org.apache.hadoop.mapreduce.lib.output.*;
import org.apache.hadoop.util.*;
public class TriangleCounter extends Configured implements Tool
{
// Maps values to Long,Long pairs.
public static class ParseLongLongPairsMapper extends Mapper<LongWritable, Text, LongWritable, LongWritable>
{
LongWritable mKey = new LongWritable();
LongWritable mValue = new LongWritable();
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException
{
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
long e1,e2;
if (tokenizer.hasMoreTokens()) {
e1 = Long.parseLong(tokenizer.nextToken());
if (!tokenizer.hasMoreTokens())
throw new RuntimeException("invalid edge line " + line);
e2 = Long.parseLong(tokenizer.nextToken());
// Input contains reciprocal edges, only need one.
if (e1 < e2) {
mKey.set(e1);
mValue.set(e2);
context.write(mKey,mValue);
}
}
}
}
// Produces original edges and triads.
public static class TriadsReducer extends Reducer<LongWritable, LongWritable, Text, LongWritable>
{
Text rKey = new Text();
final static LongWritable zero = new LongWritable((byte)0);
final static LongWritable one = new LongWritable((byte)1);
long []vArray = new long[4096];
int size = 0;
public void reduce(LongWritable key, Iterable<LongWritable> values, Context context)
throws IOException, InterruptedException
{
// Produce triads - all permutations of pairs where e1 < e2 (value=1).
// And all original edges (value=0).
// Sorted by value.
Iterator<LongWritable> vs = values.iterator();
for (size = 0; vs.hasNext(); ) {
if (vArray.length==size) {
vArray = Arrays.copyOf(vArray, vArray.length*2);
}
long e = vs.next().get();
vArray[size++] = e;
// Original edge.
rKey.set(key.toString() + "," + Long.toString(e));
context.write(rKey, zero);
}
Arrays.sort(vArray, 0, size);
// Generate triads.
for (int i=0; i<size; ++i) {
for (int j=i+1; j<size; ++j) {
rKey.set(Long.toString(vArray[i]) + "," + Long.toString(vArray[j]));
context.write(rKey, one);
}
}
}
}
// Parses values into {Text,Long} pairs.
public static class ParseTextLongPairsMapper extends Mapper<LongWritable, Text, Text, LongWritable>
{
Text mKey = new Text();
LongWritable mValue = new LongWritable();
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException
{
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
if (tokenizer.hasMoreTokens()) {
mKey.set(tokenizer.nextToken());
if (!tokenizer.hasMoreTokens())
throw new RuntimeException("invalid intermediate line " + line);
mValue.set(Long.parseLong(tokenizer.nextToken()));
context.write(mKey, mValue);
}
}
}
// Counts the number of triangles.
public static class CountTrianglesReducer extends Reducer<Text, LongWritable, LongWritable, LongWritable>
{
long count = 0;
final static LongWritable zero = new LongWritable(0);
public void cleanup(Context context)
throws IOException, InterruptedException
{
LongWritable v = new LongWritable(count);
if (count > 0) context.write(zero, v);
}
public void reduce(Text key, Iterable<LongWritable> values, Context context)
throws IOException, InterruptedException
{
long c = 0, n = 0;
Iterator<LongWritable> vs = values.iterator();
// Triad edge value=1, original edge value=0.
while (vs.hasNext()) {
c += vs.next().get();
++n;
}
if (c!=n) count += c;
}
}
// Aggregates the counts.
public static class AggregateCountsReducer extends Reducer<Text, LongWritable, LongWritable, LongWritable>
{
public void reduce(Text key, Iterable<LongWritable> values, Context context)
throws IOException, InterruptedException
{
long sum = 0;
Iterator<LongWritable> vs = values.iterator();
while (vs.hasNext()) {
sum += vs.next().get();
}
context.write(new LongWritable(sum), null);
}
}
// Takes two arguments, the edges file and output file. Edges must be reciprocal, that is every
// {source, dest} edge must have a corresponding {dest, source}.
// File must be of the form:
// long <whitespace> long <newline>
// <repeat>
//setNumReduceTasks(2);
public int run(String[] args) throws Exception
{
Job job1 = new Job(getConf());
job1.setJobName("triads");
job1.setMapOutputKeyClass(LongWritable.class);
job1.setMapOutputValueClass(LongWritable.class);
job1.setOutputKeyClass(Text.class);
job1.setOutputValueClass(LongWritable.class);
job1.setJarByClass(TriangleCounter.class);
job1.setMapperClass(ParseLongLongPairsMapper.class);
job1.setReducerClass(TriadsReducer.class);
job1.setInputFormatClass(TextInputFormat.class);
job1.setOutputFormatClass(TextOutputFormat.class);
job1.setNumReduceTasks( 8 );
FileInputFormat.addInputPath(job1, new Path(args[0]));
FileOutputFormat.setOutputPath(job1, new Path("temp1"));
Job job2 = new Job(getConf());
job2.setJobName("triangles");
job2.setMapOutputKeyClass(Text.class);
job2.setMapOutputValueClass(LongWritable.class);
job2.setOutputKeyClass(LongWritable.class);
job2.setOutputValueClass(LongWritable.class);
job2.setJarByClass(TriangleCounter.class);
job2.setMapperClass(ParseTextLongPairsMapper.class);
job2.setReducerClass(CountTrianglesReducer.class);
job2.setInputFormatClass(TextInputFormat.class);
job2.setOutputFormatClass(TextOutputFormat.class);
job2.setNumReduceTasks( 8 );
FileInputFormat.setInputPaths(job2, new Path("temp1"));
FileOutputFormat.setOutputPath(job2, new Path("temp2"));
Job job3 = new Job(getConf());
job3.setJobName("count");
job3.setMapOutputKeyClass(Text.class);
job3.setMapOutputValueClass(LongWritable.class);
job3.setOutputKeyClass(LongWritable.class);
job3.setOutputValueClass(LongWritable.class);
job3.setJarByClass(TriangleCounter.class);
job3.setMapperClass(ParseTextLongPairsMapper.class);
job3.setReducerClass(AggregateCountsReducer.class);
job3.setInputFormatClass(TextInputFormat.class);
job3.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.setInputPaths(job3, new Path("temp2"));
FileOutputFormat.setOutputPath(job3, new Path(args[1]));
int ret = job1.waitForCompletion(true) ? 0 : 1;
if (ret==0) ret = job2.waitForCompletion(true) ? 0 : 1;
if (ret==0) ret = job3.waitForCompletion(true) ? 0 : 1;
return ret;
}
public static void main(String[] args) throws Exception {
int res = ToolRunner.run(new Configuration(), new TriangleCounter(), args);
System.exit(res);
}
}