3.
Develop a MapReduce program to implement Matrix Multiplication
Java :
Open a notepad or editor and save the below program as [Link]
import [Link];
import [Link].*;
import [Link];
import [Link];
import [Link].*;
import [Link].*;
import [Link];
import [Link];
public class MatrixMultiply {
public static class MatrixMapper extends Mapper<LongWritable, Text, Text, Text>
{
private int m = 3; // rows in A
private int p = 3; // cols in B
public void map(LongWritable key, Text value, Context context) throws
IOException, InterruptedException {
String[] tokens = [Link]().split(",");
String matrix = tokens[0];
int i = [Link](tokens[1]);
int j = [Link](tokens[2]);
String val = tokens[3];
if ([Link]("A")) {
for (int k = 0; k < p; k++) {
[Link](new Text(i + "," + k), new Text("A," + j + "," + val));
}
} else if ([Link]("B")) {
for (int k = 0; k < m; k++) {
[Link](new Text(k + "," + j), new Text("B," + i + "," + val));
}
}
}
}
public static class MatrixReducer extends Reducer<Text, Text, Text,
DoubleWritable> {
public void reduce(Text key, Iterable<Text> values, Context context) throws
IOException, InterruptedException {
Map<Integer, Double> A = new HashMap<>();
Map<Integer, Double> B = new HashMap<>();
for (Text val : values) {
String[] tokens = [Link]().split(",");
String matrixName = tokens[0];
int index = [Link](tokens[1]);
double value = [Link](tokens[2]);
if ([Link]("A")) {
[Link](index, value);
} else if ([Link]("B")) {
[Link](index, value);
}
}
double sum = 0;
for (int k : [Link]()) {
if ([Link](k)) {
sum += [Link](k) * [Link](k);
}
}
[Link](key, new DoubleWritable(sum));
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = [Link](conf, "Matrix Multiplication");
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link](job, new Path(args[0])); // input path
[Link](job, new Path(args[1])); // output path
[Link]([Link](true) ? 0 : 1);
}
}
Create a [Link] file in a folder ‘input’
Sample data:
A,0,0,1
A,0,1,2
A,1,0,3
A,1,1,4
B,0,0,5
B,1,0,6
In Command Prompt:(Always run CMD as administrator only)
c:\Users\KUMARESH\Documents>javac -classpath
"C:\hadoop\share\hadoop\common\*;C:\hadoop\share\hadoop\common\lib\*;C:\hadoop\share\ha
doop\mapreduce\*;C:\hadoop\share\hadoop\mapreduce\lib\*;C:\hadoop\share\hadoop\hdfs\*;C:\
hadoop\share\hadoop\hdfs\lib\*" -d classes [Link]
c:\Users\KUMARESH\Documents>jar -cvf [Link] -C classes/ .
c:\Users\KUMARESH\Documents>[Link]
c:\hadoop>hdfs dfs -mkdir /inputmatrix
c:\hadoop>hdfs dfs -put input/[Link] /inputmatrix
c:\Users\KUMARESH\Documents>hadoop jar [Link] MatrixMultiply /inputmatrix
/output3
Output: