-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixAdd.java
More file actions
70 lines (63 loc) · 2.25 KB
/
MatrixAdd.java
File metadata and controls
70 lines (63 loc) · 2.25 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
import java.io.IOException;
import java.util.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
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.GenericOptionsParser;
public class MatrixAdd
{
public static class MatrixAddMapper extends Mapper<Object, Text, Text, IntWritable>
{
private final static IntWritable i_value = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context) throws IOException, InterruptedException
{
StringTokenizer itr = new StringTokenizer(value.toString());
int row_id = Integer.parseInt(itr.nextToken().trim());
int col_id = Integer.parseInt(itr.nextToken().trim());
int m_value = Integer.parseInt(itr.nextToken().trim());
word.set(row_id + "," + col_id);
i_value.set(m_value);
context.write(word, i_value);
}
}
public static class MatrixAddReducer extends Reducer<Text,IntWritable,Text,IntWritable>
{
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException
{
int sum = 0;
for (IntWritable val : values)
{
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
public static void main(String[] args) throws Exception
{
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length != 2)
{
System.err.println("Usage: MatrixAdd <in> <out>");
System.exit(2);
}
Job job = new Job(conf, "Matrix Add");
job.setJarByClass(MatrixAdd.class);
job.setMapperClass(MatrixAddMapper.class);
job.setCombinerClass(MatrixAddReducer.class);
job.setReducerClass(MatrixAddReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
FileSystem.get(job.getConfiguration()).delete( new Path(otherArgs[1]), true);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}