-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransform.java
More file actions
90 lines (77 loc) · 3.18 KB
/
Transform.java
File metadata and controls
90 lines (77 loc) · 3.18 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
package org.apache.hadoop.examples;
import java.io.IOException;
import java.io.OutputStream;
import java.util.StringTokenizer;
import java.util.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FileSystem;
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.io.IntWritable;
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.mapreduce.lib.input.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.mapreduce.lib.input.*;
import org.apache.hadoop.mapreduce.lib.output.*;
public class Transform{
public static class TransformMap extends Mapper<LongWritable, Text, Text, Text> {
//@Override
public void map(LongWritable key, Text value, Context context
) throws IOException, InterruptedException {
//input: <row,col,1>
//<K,V> = <colId, (rowId, 1)> -> only throw if the value is 1
String[] data = value.toString().split(",");
context.write(new Text(data[1]), new Text(data[0]));
}
}
public static class TransformReduce extends Reducer<Text, Text, Text, Text> {
@Override
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
Configuration conf = context.getConfiguration();
int numOfRows = Integer.parseInt(conf.get("numOfShingles"));//numOfShingles
int[] row = new int[numOfRows];
Arrays.fill(row, 0);
for(Text val : values){
int rowId = Integer.parseInt(val.toString());
row[rowId] = 1;
}
StringBuilder result = new StringBuilder();
result.append("M,");
result.append(key.toString());
result.append(",");
for(int i=0; i<numOfRows; i++){
result.append(Integer.toString(row[i]));
}
context.write(null, new Text(result.toString()));
}
}
public int run(int numOfShingles) throws Exception {
Configuration conf = new Configuration();
//Save params
conf.set("numOfShingles",Integer.toString(numOfShingles));
Job job = new Job(conf,"Transform");
job.setJarByClass(Transform.class);
job.setMapperClass(TransformMap.class);
//job.setCombinerClass(Reduce.class);
job.setReducerClass(TransformReduce.class);
//mapOutput,reduceOutput
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.addInputPaths(job, "/user/root/data/tempMatrix.txt");
FileOutputFormat.setOutputPath(job, new Path("/user/root/data/transMatrix"));
return (job.waitForCompletion(true) ? 0 : -1);
}
}