-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPP.java
369 lines (343 loc) · 14.1 KB
/
APP.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import java.io.*;
import com.google.common.base.Joiner;
import java.util.StringTokenizer;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.Random;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.mapreduce.InputFormat;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Partitioner;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.NLineInputFormat;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileAsTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
import org.apache.hadoop.io.compress.DefaultCodec;
import org.apache.hadoop.io.compress.GzipCodec;
import org.apache.hadoop.io.compress.BZip2Codec;
import org.apache.hadoop.io.compress.SnappyCodec;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.apache.hadoop.conf.Configured;
/**
* Aggregate certain and possible equivalence classes in parallel
*/
public class APP extends Configured implements Tool
{
public static void main(String [] args) throws Exception
{
int exitCode = ToolRunner.run(new APP(), args);
System.exit(exitCode);
}
public int run(String[] args) throws Exception {
long startTime = System.nanoTime();
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, args[2]);
job.setJarByClass(APP.class);
job.setMapperClass(MapAPP.class);
job.setPartitionerClass(PartitionerAPP.class);
job.setReducerClass(ReduceAPP.class);
job.setNumReduceTasks(Integer.parseInt(args[3]));
// job input
job.setInputFormatClass(SequenceFileInputFormat.class);
// map output, compressed using Snappy
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
conf.setBoolean(Job.MAP_OUTPUT_COMPRESS,true);
conf.setClass(Job.MAP_OUTPUT_COMPRESS_CODEC, SnappyCodec.class,CompressionCodec.class);
// job output
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setOutputFormatClass(SequenceFileOutputFormat.class);
FileOutputFormat.setCompressOutput(job, true);
SequenceFileOutputFormat.setOutputCompressionType(job, SequenceFile.CompressionType.BLOCK);// just only effect when job output is SequenceFile
FileOutputFormat.setOutputCompressorClass(job, SnappyCodec.class);
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
int status = job.waitForCompletion(true) ? 0:1;
long stopTime = System.nanoTime();
System.out.println("Time" + TimeUnit.NANOSECONDS.toSeconds(startTime - stopTime));
return status;
}
// partitioner
public static class PartitionerAPP extends Partitioner<Text,Text>{
public int getPartition(Text key, Text value, int numReduceTasks)
{
if (numReduceTasks==1) return 1;
if (numReduceTasks==0) return 0;
int ret=0;
String[] v= value.toString().split("\t");
if (numReduceTasks==2) {
if (value.toString().startsWith("2")){
ret=0;
}
else ret=1;
}
if (numReduceTasks==3) {
Random rand = new Random();
ret = rand.nextInt(3);//return 0,1,2
}
if (numReduceTasks==4){
if (v[0].startsWith("1")){
ret=1;
}
else if (v[0].startsWith("2")){
ret=2;
}
else if (v[0].startsWith("3")){
ret=3;
}
else {
ret=0;
}
}
System.out.println("key: "+ v[0] + "goes to " + ret + " partition"+", numReduceTasks="+numReduceTasks);
return ret;
}
}
public static class MapAPP extends Mapper<Text, Text, Text, Text>{
public static int k=0;
public List<List<Integer>> c_pre;
public List<List<Integer>> c_current;
public List<List<Integer>> c_result;
public List<String> strKey;
public void setup(Context con) throws IOException, InterruptedException
{
c_pre = new ArrayList<>();
c_current = new ArrayList<>();
c_result = new ArrayList<>();
strKey = new ArrayList<>();
}
public void map(Text key, Text value, Context con) throws IOException, InterruptedException
{
System.out.println("key: "+ key);
//aggregate max
if (k!=100){
if (k==0){
c_pre= convertToListList(value.toString());
strKey.add(key.toString());
k=1;
}else{
c_current= convertToListList(value.toString());
for (List<Integer> p: c_pre){
for (List<Integer> c: c_current){
List<Integer> intersect=intersection(c,p);
if (!intersect.isEmpty()){
if(!exists(intersect,c_result))
c_result.add(intersect);
}
}
}
if (c_result.isEmpty()){
k=100;
System.out.println("key: "+key+" not_exist");
} else {
c_pre=new ArrayList<List<Integer>>(c_result);
c_result.clear();
System.out.println("key: "+ key + " OK");
strKey.add(key.toString());
}
}
}
}
protected void cleanup(Context con) throws IOException, InterruptedException {
Joiner pipe = Joiner.on("|").skipNulls();
Joiner comma = Joiner.on(",").skipNulls();
String sKey= comma.join(strKey);
if (k==100){
con.write(new Text("1"), new Text(sKey+"\t"+"not_exist"));
System.out.println(sKey+"\t"+"not_exist");
}else {
List<String> tmp = new ArrayList<String>();
for(List<Integer> st: c_pre){
String s= comma.join(st);
tmp.add(s);
}
String max_str = pipe.join(tmp);
if (!max_str.isEmpty()){
con.write(new Text("1"), new Text(sKey+"\t"+max_str));
System.out.println(sKey+"\t"+"OK");
}
}
c_pre.clear();
c_current.clear();
c_result.clear();
System.out.println("Map finished");
}
// convert a string to a List<List<Integer>>
public static List<List<Integer>> convertToListList(String v) {
String[] vStr= v.trim().split("\\|");
List<List<Integer>> tmp = new ArrayList<List<Integer>>();
for (String s: vStr){
StringTokenizer st = new StringTokenizer(s, ",");
int len = s.trim().split(",").length;
List<Integer> myList = new ArrayList<Integer>();
while(st.hasMoreTokens()){
myList.add(Integer.parseInt(st.nextToken()));
}
tmp.add(myList);
}
return tmp;
}
// find intersection between two List<Integer>
public static List<Integer> intersection(List<Integer> arr1, List<Integer> arr2)
{
List<Integer> tmp = new ArrayList<Integer>();
int i = 0, j = 0;
int m = arr1.size();
int n= arr2.size();
while (i < m && j < n)
{
if (arr1.get(i) < arr2.get(j))
i++;
else if (arr2.get(j) < arr1.get(i))
j++;
else
{
tmp.add(arr2.get(j));
j++;
i++;
}
}
return tmp;
}
// check arr1 to see if it is arr2. Return true if arr1 is in arr2.
static Boolean exists(List<Integer> arr1, List<List<Integer>> arr2)
{
Boolean exist=false;
for (List<Integer> l : arr2){
if (arr1.equals(l)){
exist=true;
break;
}
}
return exist;
}
}
public static class ReduceAPP extends Reducer<Text, Text, Text, Text> {
public void reduce(Text key, Iterable<Text> values, Context con) throws IOException, InterruptedException
{
System.out.println("Start reducer");
List<List<Integer>> c_pre = new ArrayList<>();
List<List<Integer>> c_current = new ArrayList<>();
List<List<Integer>> c_result = new ArrayList<>();
List<String> strKey = new ArrayList<>();
int k=0;
// aggregate
for(Text value: values){
String[] v=value.toString().split("\t");
String sKey=v[0];
String sValue=v[1];
System.out.println("key: "+ sKey);
if (sValue.equals("not_exist")){
k=100;
strKey.add(sKey);
break;
}
if (k==0){
c_pre= convertToListList(sValue);
strKey.add(sKey);
k=1;
}else{
c_current= convertToListList(sValue);
for (List<Integer> p: c_pre){
for (List<Integer> c: c_current){
List<Integer> intersect=intersection(c,p);
if (!intersect.isEmpty()){
if(!exists(intersect,c_result)){
c_result.add(intersect);
}
}
}
}
if (c_result.isEmpty()){
k=100;
System.out.println(sKey+"\t"+"not_exist");
break;
} else {
c_pre=new ArrayList<List<Integer>>(c_result);
c_result.clear();
System.out.println(sKey +"\t"+"OK");
strKey.add(sKey);
}
}
}
Joiner pipe = Joiner.on("|").skipNulls();
Joiner comma = Joiner.on(",").skipNulls();
String sKey=comma.join(strKey);
if (k==100){
con.write(new Text("1"), new Text(sKey +"\t"+"not_exist"));
}else {
List<String> tmp = new ArrayList<String>();
for(List<Integer> st: c_pre){
String s= comma.join(st);
tmp.add(s);
}
String max_str = pipe.join(tmp);
if (!max_str.isEmpty()){
con.write(new Text("1"), new Text(sKey+"\t"+max_str));
System.out.println(sKey+"\t"+"OK");
System.out.println("Reducer: finished");
}
}
}
static Boolean exists(List<Integer> arr1, List<List<Integer>> arr2)
{
Boolean exist=false;
for (List<Integer> l : arr2){
if (arr1.equals(l)){
exist=true;
break;
}
}
return exist;
}
public static List<List<Integer>> convertToListList(String v) {
String[] vStr= v.trim().split("\\|");
List<List<Integer>> tmp = new ArrayList<List<Integer>>();
for (String s: vStr){
StringTokenizer st = new StringTokenizer(s, ",");
int len = s.trim().split(",").length;
List<Integer> myList = new ArrayList<Integer>();
while(st.hasMoreTokens()){
myList.add(Integer.parseInt(st.nextToken()));
}
tmp.add(myList);
}
return tmp;
}
public static List<Integer> intersection(List<Integer> arr1, List<Integer> arr2)
{
List<Integer> tmp = new ArrayList<Integer>();
int i = 0, j = 0;
int m = arr1.size();
int n= arr2.size();
while (i < m && j < n)
{
if (arr1.get(i) < arr2.get(j))
i++;
else if (arr2.get(j) < arr1.get(i))
j++;
else
{
tmp.add(arr2.get(j));
j++;
i++;
}
}
return tmp;
}
}
}