0% found this document useful (0 votes)
8 views2 pages

Hadoop Matrix Multiplication Example

The document contains a Java implementation of a MapReduce program for matrix multiplication using Hadoop. It defines a Mapper class that processes input matrices A and B, and a Reducer class that computes the product of the matrices. The main method sets up the job configuration and specifies input and output paths.

Uploaded by

veebika1803
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Hadoop Matrix Multiplication Example

The document contains a Java implementation of a MapReduce program for matrix multiplication using Hadoop. It defines a Mapper class that processes input matrices A and B, and a Reducer class that computes the product of the matrices. The main method sets up the job configuration and specifies input and output paths.

Uploaded by

veebika1803
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import [Link].

IOException;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MatrixMultiply {

public static class Map extends Mapper<Object, Text, Text, Text> {


public void map(Object key, Text value, Context context) throws
IOException, InterruptedException {
// value format: i j val
String[] parts = [Link]().split(" ");
String i = parts[0];
String j = parts[1];
String v = parts[2];

// Matrix A: i j aij → key=j, value="A,i,aij"


if ([Link]().startsWith("A"))
[Link](new Text(j), new Text("A," + i + "," + v));

// Matrix B: j k bjk → key=j, value="B,k,bjk"


else if ([Link]().startsWith("B"))
[Link](new Text(j), new Text("B," + j + "," + v));
}
}

public static class Reduce extends Reducer<Text, Text, Text, IntWritable> {


public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {

int[] A = new int[10];


int[] B = new int[10];

for (Text val : values) {


String[] parts = [Link]().split(",");
if (parts[0].equals("A")) {
int i = [Link](parts[1]);
int a = [Link](parts[2]);
A[i] = a;
} else {
int k = [Link](parts[1]);
int b = [Link](parts[2]);
B[k] = b;
}
}

for (int i = 0; i < 10; i++) {


for (int k = 0; k < 10; k++) {
int result = A[i] * B[k];
if (result != 0)
[Link](new Text(i + "," + k), new
IntWritable(result));
}
}
}
}

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](job, new Path(args[0]));


[Link](job, new Path(args[1]));

[Link]([Link](true) ? 0 : 1);
}
}

You might also like