- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixAdd.java
More file actions
Latest commit
70 lines (63 loc) · 2.25 KB
/
Copy pathMatrixAdd.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
importjava.io.IOException;
importjava.util.*;
importorg.apache.hadoop.conf.*;
importorg.apache.hadoop.fs.FileSystem;
importorg.apache.hadoop.fs.Path;
importorg.apache.hadoop.io.*;
importorg.apache.hadoop.mapreduce.*;
importorg.apache.hadoop.mapreduce.lib.input.*;
importorg.apache.hadoop.mapreduce.lib.output.*;
importorg.apache.hadoop.util.GenericOptionsParser;
publicclassMatrixAdd
{
publicstaticclassMatrixAddMapperextendsMapper<Object, Text, Text, IntWritable>
{
privatefinalstaticIntWritablei_value = newIntWritable(1);
privateTextword = newText();
publicvoidmap(Objectkey, Textvalue, Contextcontext) throwsIOException, InterruptedException
{
StringTokenizeritr = newStringTokenizer(value.toString());
introw_id = Integer.parseInt(itr.nextToken().trim());
intcol_id = Integer.parseInt(itr.nextToken().trim());
intm_value = Integer.parseInt(itr.nextToken().trim());
word.set(row_id + "," + col_id);
i_value.set(m_value);
context.write(word, i_value);
}
}
publicstaticclassMatrixAddReducerextendsReducer<Text,IntWritable,Text,IntWritable>
{
privateIntWritableresult = newIntWritable();
publicvoidreduce(Textkey, Iterable<IntWritable> values, Contextcontext) throwsIOException, InterruptedException
{
intsum = 0;
for (IntWritableval : values)
{
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
publicstaticvoidmain(String[] args) throwsException
{
Configurationconf = newConfiguration();
String[] otherArgs = newGenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length != 2)
{
System.err.println("Usage: MatrixAdd <in> <out>");
System.exit(2);
}
Jobjob = newJob(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, newPath(otherArgs[0]));
FileOutputFormat.setOutputPath(job, newPath(otherArgs[1]));
FileSystem.get(job.getConfiguration()).delete( newPath(otherArgs[1]), true);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}