0% found this document useful (0 votes)
3 views4 pages

Matrix

The document provides Java code for a Hadoop MapReduce program that performs matrix multiplication. It includes the Mapper and Reducer classes to process input matrices and output their product. Additionally, it outlines the compilation and execution steps, including creating a JAR file and running the job on HDFS with sample input data.

Uploaded by

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

Matrix

The document provides Java code for a Hadoop MapReduce program that performs matrix multiplication. It includes the Mapper and Reducer classes to process input matrices and output their product. Additionally, it outlines the compilation and execution steps, including creating a JAR file and running the job on HDFS with sample input data.

Uploaded by

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

Code:

import [Link].*;

import [Link].*;

import [Link].*;

import [Link].*;

import [Link].*;

import [Link].*;

import [Link].*;

public class MatrixMultiply {

public static class M extends Mapper<LongWritable, Text, Text, Text> {

public void map(LongWritable k, Text v, Context c) throws


IOException, InterruptedException {

String[] e = [Link]().split(",");

String type = e[0], row = e[1], col = e[2], val = e[3];

if([Link]("A")) [Link](new Text(row+","+col), new


Text("A,"+val));

else [Link](new Text(row+","+col), new Text("B,"+val));

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

public void reduce(Text k, Iterable<Text> vals, Context c) throws


IOException, InterruptedException {

int a=0, b=0;

for(Text t: vals){

String[] s=[Link]().split(",");

if(s[0].equals("A")) a=[Link](s[1]);

else b=[Link](s[1]);
}

[Link](k, new IntWritable(a*b));

public static void main(String[] args) throws Exception {

Job job = [Link](new Configuration(), "matrixmul");

[Link]("[Link]");

[Link]([Link]);

[Link]([Link]);

[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);

Compile:

javac -classpath "C:\hadoop-3.3.6\share\hadoop\common\*;C:\hadoop-


3.3.6\share\hadoop\common\lib\*;C:\hadoop-3.3.6\share\hadoop\
mapreduce\*;C:\hadoop-3.3.6\share\hadoop\mapreduce\lib\*"
[Link]

create jar:

jar cvf [Link] MatrixMultiply*.class

create a input file called [Link]:

A,0,0,2

B,0,0,3

A,1,1,4

B,1,1,5

upload the file into the hdfs:

hdfs dfs -mkdir /in

hdfs dfs -put [Link] /in

now run:

hadoop jar [Link] MatrixMultiply /in /out

Output:

hdfs dfs -cat /out/part-r-00000

You might also like