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
| import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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.util.GenericOptionsParser;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.conf.Configuration;
public class triangle {
public static class sortMapper extends Mapper<Object, Text, Text, Text>{
private Text a = new Text();
private Text b = new Text();
public void map(Object key,Text value,Context context)throws IOException,InterruptedException {
StringTokenizer itr=new StringTokenizer(value.toString());
long first = Integer.parseInt(itr.nextToken());
long second = Integer.parseInt(itr.nextToken());
if (first <= second){
a.set(first+"#"+second);
b.set("#");
}
else{
a.set(second+"#"+first);
b.set("#");
}
context.write(a, b);
}
}
public static class sortReducer extends Reducer<Text, Text, Text, Text>{
private Text b = new Text();
public void reduce(Text key,Iterable<Text> values,Context context)throws IOException,InterruptedException {
//Text b = new Text();
b.set("#");
context.write(key, b);
}
}
public static class findMap extends Mapper<LongWritable, Text, Text, Text>{
public void map(LongWritable key, Text value, Context context)throws IOException,InterruptedException {
StringTokenizer itr=new StringTokenizer(value.toString());
String[] tokens = itr.nextToken().toString().split("#");
//String[] tokens = value.toString().split("#");
String a = tokens[0];
String b = tokens[1];
//System.out.println(a+" "+b);
Text left = new Text();
Text right = new Text();
left.set(a+"");
right.set(b+"");
context.write(left, right);
}
}
public static class findReducer extends Reducer<Text, Text, Text, Text>{
public void reduce(Text key,Iterable<Text> values,Context context)throws IOException,InterruptedException {
ArrayList<String> array = new ArrayList<String>();
Text left = new Text();
Text right = new Text();
right.set("#");
for(Text value : values){
array.add(value.toString());
left.set(key.toString()+"#"+value.toString());
context.write(left, right);
}
for(int i=0; i<array.size(); i++){
for(int j=i+1; j<array.size(); j++){
Text a = new Text();
Text b = new Text();
if(Integer.parseInt(array.get(i)) < Integer.parseInt(array.get(j))){
a.set(array.get(i)+"#"+array.get(j));
b.set("&");
}
else{
a.set(array.get(j)+"#"+array.get(i));
b.set("&");
}
context.write(a, b);
}
}
}
}
public static class combineMap extends Mapper<LongWritable, Text, Text, Text>{
private Text a = new Text();
private Text b = new Text();
public void map(LongWritable key,Text value,Context context)throws IOException,InterruptedException {
StringTokenizer itr=new StringTokenizer(value.toString());
a.set(itr.nextToken().toString());
b.set(itr.nextToken().toString());
context.write(a, b);
}
}
public static class combineReducer extends Reducer<Text, Text, Text, Text>{
private static int result = 0;
//private Text a = new Text();
//private Text b = new Text();
public void cleanup(Context context) throws IOException, InterruptedException{
context.write(new Text("Result: "), new Text(""+result));
}
public void reduce(Text key,Iterable<Text> values,Context context)throws IOException,InterruptedException {
int count = 0;
int is_triangle = 0;
for(Text value : values){
if(value.toString().equalsIgnoreCase("#")){
is_triangle = 1;
}else if(value.toString().equalsIgnoreCase("&")){
count ++;
}else{
//System.out.println("wrong input number");
}
}
if (is_triangle == 1){
result += count;
}
//b.set(result+"");
//context.write(key, new Text(count+""));
}
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Configuration conf=new Configuration();
String[] otherArgs=new GenericOptionsParser(conf,args).getRemainingArgs();
if (otherArgs.length!=4) {
System.err.println("Usage:invertedindex<in> <out1> <out2> <out3>");
System.exit(2);
}
Job job1 = new Job(conf, "job1");
job1.setJarByClass(triangle.class);
job1.setMapperClass(sortMapper.class);
job1.setMapOutputKeyClass(Text.class);
job1.setMapOutputValueClass(Text.class);
job1.setReducerClass(sortReducer.class);
job1.setOutputKeyClass(Text.class);
job1.setOutputValueClass(Text.class);
//job1.setInputFormatClass(TextInputFormat.class);
//job1.setOutputFormatClass(TextOutputFormat.class);
//job1.setMapOutputKeyClass(Text.class);
//job1.setMapOutputValueClass(Text.class);
FileInputFormat.addInputPath(job1, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job1, new Path(otherArgs[1]));
job1.waitForCompletion(true);
Job job2 = new Job(conf, "job2");
job2.setJarByClass(triangle.class);
job2.setMapperClass(findMap.class);
job2.setMapOutputKeyClass(Text.class);
job2.setMapOutputValueClass(Text.class);
job2.setReducerClass(findReducer.class);
job2.setOutputKeyClass(Text.class);
job2.setOutputValueClass(Text.class);
//job2.setInputFormatClass(TextInputFormat.class);
//job2.setOutputFormatClass(TextOutputFormat.class);
//job2.setMapOutputKeyClass(Text.class);
//job2.setMapOutputValueClass(Text.class);
FileInputFormat.addInputPath(job2, new Path(otherArgs[1]));
FileOutputFormat.setOutputPath(job2, new Path(otherArgs[2]));
job2.waitForCompletion(job1.isComplete());
Job job3 = new Job(conf, "job3");
job3.setJarByClass(triangle.class);
job3.setMapperClass(combineMap.class);
job3.setMapOutputKeyClass(Text.class);
job3.setMapOutputValueClass(Text.class);
job3.setReducerClass(combineReducer.class);
job3.setOutputKeyClass(Text.class);
job3.setOutputValueClass(Text.class);
//job3.setInputFormatClass(TextInputFormat.class);
//job3.setOutputFormatClass(TextOutputFormat.class);
//job3.setMapOutputKeyClass(Text.class);
//job3.setMapOutputValueClass(Text.class);
FileInputFormat.addInputPath(job3, new Path(otherArgs[2]));
FileOutputFormat.setOutputPath(job3, new Path(otherArgs[3]));
job3.waitForCompletion(job2.isComplete());
}
}
|