SlideShare a Scribd company logo
11
Most read
13
Most read
14
Most read
Exp-6
Implement Matrix Multiplication with
Hadoop Map Reduce
MapReduce Algorithm for Matrix Multiplication
The reduce( ) step in the
MapReduce Algorithm for matrix
multiplication
1.The final step in the MapReduce algorithm is to produce the matrix A × B
2.The unit of computation of of matrix A × B is one element in the matrix
The input information of the reduce( ) step (function)
of the MapReduce algorithm are
One row vector from matrix A
One column vector from matrix B
•The inner product of the
•One row vector from matrix A
•One column vector from matrix B
The reduce( ) function will compute:
Preprocessing for the map(
) function
The map( ) function (really) only has one input stream:
of the format ( keyi , valuei )
The inputs of the matrix
multiplication are
(2) input matrices
( key1 , value1 ) ( key2 , value2 ) ( key3 , value3 ) ...
convert the input matrices to the form:
Pre-processing used for matrix
multiplication:
Overview of the MapReduce
Algorithm for Matrix Multiplication
( (A, 1, 1) , a11 )
( (A, 1, 2) , a12 )
( (A, 1, 3) , a13 ) ...
( (B, 1, 1) , b11 )
( (B, 1, 2) , b12 )
( (B, 1, 3) , b13 ) ...
•A row vector from matrix A
•A column vector from matrix B
•The input to the Map( ) is as follows:
•The input to one reduce( ) function is as follows:
EX-6-Implement Matrix Multiplication with Hadoop Map Reduce.pptx
Algorithm for Map Function.
for each element mij of M do
produce (key,value) pairs as ((i,k), (M,j,mij), for
k=1,2,3,.. upto the number of columns of N
for each element njk of N do
produce (key,value) pairs as ((i,k),(N,j,Njk), for i =
1,2,3,.. Upto the number of rows of M.
return Set of (key,value) pairs that each key (i,k),
has list with values (M,j,mij) and (N, j,njk) for all
possible values of j
Algorithm for Reduce
Function.
for each key (i,k) do
sort values begin with M by j in listM
sort values begin with N by j in listN
multiply mij and njk for jth value of each list
sum up mij x njk return (i,k), Σj=1 mij x njk
Creating Mapper file for Matrix Multiplication.
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
public class Map
extends org.apache.hadoop.mapreduce.Mapper<LongWritable, Text, Text, Text> {
@Override
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
Configuration conf = context.getConfiguration();
int m = Integer.parseInt(conf.get("m"));
int p = Integer.parseInt(conf.get("p"));
String line = value.toString();
// (M, i, j, Mij);
String[] indicesAndValue = line.split(",");
Text outputKey = new Text();
Text outputValue = new Text();
if (indicesAndValue[0].equals("M")) {
for (int k = 0; k < p; k++) {
outputKey.set(indicesAndValue[1] + "," + k);
// outputKey.set(i,k);
outputValue.set(indicesAndValue[0] + "," + indicesAndValue[2]
+ "," + indicesAndValue[3]);
// outputValue.set(M,j,Mij);
context.write(outputKey, outputValue);
}
} else {
// (N, j, k, Njk);
for (int i = 0; i < m; i++) {
outputKey.set(i + "," + indicesAndValue[2]);
outputValue.set("N," + indicesAndValue[1] + ","
+ indicesAndValue[3]);
context.write(outputKey, outputValue);
}
}
}
}
Creating Reducer.java file for Matrix Multiplication.
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
import java.util.HashMap;
public class Reduce
extends org.apache.hadoop.mapreduce.Reducer<Text, Text, Text, Text> {
@Override
public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
String[] value;
//key=(i,k),
//Values = [(M/N,j,V/W),..]
HashMap<Integer, Float> hashA = new HashMap<Integer, Float>();
HashMap<Integer, Float> hashB = new HashMap<Integer, Float>();
for (Text val : values) {
value = val.toString().split(",");
if (value[0].equals("M")) {
hashA.put(Integer.parseInt(value[1]), Float.parseFloat(value[2]));
} else {
hashB.put(Integer.parseInt(value[1]), Float.parseFloat(value[2]));
}
}
int n = Integer.parseInt(context.getConfiguration().get("n"));
float result = 0.0f;
float m_ij;
float n_jk;
for (int j = 0; j < n; j++) {
m_ij = hashA.containsKey(j) ? hashA.get(j) : 0.0f;
n_jk = hashB.containsKey(j) ? hashB.get(j) : 0.0f;
result += m_ij * n_jk;
}
if (result != 0.0f) {
context.write(null,
new Text(key.toString() + "," + Float.toString(result)));
}
}
}
Creating MatrixMultiply.java file for
import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
public class MatrixMultiply {
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.err.println("Usage: MatrixMultiply <in_dir> <out_dir>");
System.exit(2);
}
Configuration conf = new Configuration();
// M is an m-by-n matrix; N is an n-by-p matrix.
conf.set("m", "1000");
conf.set("n", "100");
conf.set("p", "1000");
@SuppressWarnings("deprecation")
Job job = new Job(conf, "MatrixMultiply");
job.setJarByClass(MatrixMultiply.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.waitForCompletion(true);
}
}
 Creating Jar file for the Matrix Multiplication.
$ jar -cvf MatrixMultiply.jar
 Uploading the M, N file which contains the
matrix multiplication data to HDFS.
$ hadoop fs -mkdir Matrix/
$ hadoop fs -copyFromLocal M Matrix/
$ hadoop fs -copyFromLocal N Matrix/
Executing the jar file using hadoop command and
thus how fetching record from HDFS and storing
output in HDFS.
$ hadoop jar MatrixMultiply.jar www.ehadoopinfo.com.MatrixMultiply
Matrix/* result/

More Related Content

PDF
Hadoop eco system with mapreduce hive and pig
PPTX
Introduction To Hadoop | What Is Hadoop And Big Data | Hadoop Tutorial For Be...
PPTX
Map Reduce
PPTX
Introduction to Big Data & Hadoop Architecture - Module 1
PPT
Hadoop HDFS.ppt
PPT
Hadoop Map Reduce
PPTX
Developing a Map Reduce Application
PPTX
Map reduce presentation
Hadoop eco system with mapreduce hive and pig
Introduction To Hadoop | What Is Hadoop And Big Data | Hadoop Tutorial For Be...
Map Reduce
Introduction to Big Data & Hadoop Architecture - Module 1
Hadoop HDFS.ppt
Hadoop Map Reduce
Developing a Map Reduce Application
Map reduce presentation

What's hot (20)

PDF
Inference in Bayesian Networks
PDF
Informed search
PPT
2.5 backpropagation
PDF
Hadoop YARN
PDF
Support Vector Machines ( SVM )
PPTX
Two pass Assembler
PPTX
Unsupervised learning
PPSX
Perceptron (neural network)
PPT
Heuristic Search Techniques {Artificial Intelligence}
PPTX
Open Cloud Consortium Overview (01-10-10 V6)
PPTX
Association rule mining.pptx
PPTX
Data decomposition techniques
PPTX
Depth Buffer Method
PPTX
Recurrent Neural Network (RNN) | RNN LSTM Tutorial | Deep Learning Course | S...
PPTX
Decision tree induction \ Decision Tree Algorithm with Example| Data science
PPTX
lazy learners and other classication methods
PPT
4 informed-search
PDF
PAC Learning
PPTX
Naive bayesian classification
Inference in Bayesian Networks
Informed search
2.5 backpropagation
Hadoop YARN
Support Vector Machines ( SVM )
Two pass Assembler
Unsupervised learning
Perceptron (neural network)
Heuristic Search Techniques {Artificial Intelligence}
Open Cloud Consortium Overview (01-10-10 V6)
Association rule mining.pptx
Data decomposition techniques
Depth Buffer Method
Recurrent Neural Network (RNN) | RNN LSTM Tutorial | Deep Learning Course | S...
Decision tree induction \ Decision Tree Algorithm with Example| Data science
lazy learners and other classication methods
4 informed-search
PAC Learning
Naive bayesian classification
Ad

Similar to EX-6-Implement Matrix Multiplication with Hadoop Map Reduce.pptx (15)

PPTX
Lecture 04 big data analytics | map reduce
PDF
Sparse matrix computations in MapReduce
PDF
Writing MapReduce Programs using Java | Big Data Hadoop Spark Tutorial | Clou...
DOCX
Hadoop Installation_13_09_2022(1).docx
PPTX
Map Reduce
PPTX
Optimal Chain Matrix Multiplication Big Data Perspective
PDF
Mapreduce by examples
PDF
Automatic Synthesis of Combiners in the MapReduce Framework
PPTX
Introduction to Map Reduce
PPTX
Big Data & Analytics MapReduce/Hadoop – A programmer’s perspective
 
PDF
Introduction to map reduce
PPTX
SN-BDA-MR-Analysis-6.pptx.................
PDF
04 Algorithms
PDF
Tall-and-skinny Matrix Computations in MapReduce (ICME MR 2013)
PDF
MapReduce
Lecture 04 big data analytics | map reduce
Sparse matrix computations in MapReduce
Writing MapReduce Programs using Java | Big Data Hadoop Spark Tutorial | Clou...
Hadoop Installation_13_09_2022(1).docx
Map Reduce
Optimal Chain Matrix Multiplication Big Data Perspective
Mapreduce by examples
Automatic Synthesis of Combiners in the MapReduce Framework
Introduction to Map Reduce
Big Data & Analytics MapReduce/Hadoop – A programmer’s perspective
 
Introduction to map reduce
SN-BDA-MR-Analysis-6.pptx.................
04 Algorithms
Tall-and-skinny Matrix Computations in MapReduce (ICME MR 2013)
MapReduce
Ad

More from vishal choudhary (20)

PPTX
mobile application using automatin using node ja java on
PPTX
mobile development using node js and java
PPTX
Pixel to Percentage conversion Convert left and right padding of a div to per...
PPTX
esponsive web design means that your website (
PPTX
function in php using like three type of function
PPTX
data base connectivity in php using msql database
PPTX
software evelopment life cycle model and example of water fall model
PPTX
software Engineering lecture on development life cycle
PPTX
strings in php how to use different data types in string
PPTX
OPEN SOURCE WEB APPLICATION DEVELOPMENT question
PPTX
web performnace optimization using css minification
PPTX
web performance optimization using style
PPTX
Data types and variables in php for writing and databse
PPTX
Data types and variables in php for writing
PPTX
Data types and variables in php for writing
PPTX
sofwtare standard for test plan it execution
PPTX
Software test policy and test plan in development
PPTX
function in php like control loop and its uses
PPTX
introduction to php and its uses in daily
PPTX
data type in php and its introduction to use
mobile application using automatin using node ja java on
mobile development using node js and java
Pixel to Percentage conversion Convert left and right padding of a div to per...
esponsive web design means that your website (
function in php using like three type of function
data base connectivity in php using msql database
software evelopment life cycle model and example of water fall model
software Engineering lecture on development life cycle
strings in php how to use different data types in string
OPEN SOURCE WEB APPLICATION DEVELOPMENT question
web performnace optimization using css minification
web performance optimization using style
Data types and variables in php for writing and databse
Data types and variables in php for writing
Data types and variables in php for writing
sofwtare standard for test plan it execution
Software test policy and test plan in development
function in php like control loop and its uses
introduction to php and its uses in daily
data type in php and its introduction to use

Recently uploaded (20)

PPTX
Lesson notes of climatology university.
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Anesthesia in Laparoscopic Surgery in India
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
PDF
Trump Administration's workforce development strategy
PPTX
master seminar digital applications in india
PDF
RMMM.pdf make it easy to upload and study
PPTX
History, Philosophy and sociology of education (1).pptx
PDF
Complications of Minimal Access Surgery at WLH
PDF
Classroom Observation Tools for Teachers
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
Lesson notes of climatology university.
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Anesthesia in Laparoscopic Surgery in India
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
LDMMIA Reiki Yoga Finals Review Spring Summer
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Microbial disease of the cardiovascular and lymphatic systems
Module 4: Burden of Disease Tutorial Slides S2 2025
Final Presentation General Medicine 03-08-2024.pptx
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
Trump Administration's workforce development strategy
master seminar digital applications in india
RMMM.pdf make it easy to upload and study
History, Philosophy and sociology of education (1).pptx
Complications of Minimal Access Surgery at WLH
Classroom Observation Tools for Teachers
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx

EX-6-Implement Matrix Multiplication with Hadoop Map Reduce.pptx

  • 1. Exp-6 Implement Matrix Multiplication with Hadoop Map Reduce
  • 2. MapReduce Algorithm for Matrix Multiplication
  • 3. The reduce( ) step in the MapReduce Algorithm for matrix multiplication 1.The final step in the MapReduce algorithm is to produce the matrix A × B 2.The unit of computation of of matrix A × B is one element in the matrix
  • 4. The input information of the reduce( ) step (function) of the MapReduce algorithm are One row vector from matrix A One column vector from matrix B •The inner product of the •One row vector from matrix A •One column vector from matrix B The reduce( ) function will compute:
  • 5. Preprocessing for the map( ) function The map( ) function (really) only has one input stream: of the format ( keyi , valuei )
  • 6. The inputs of the matrix multiplication are (2) input matrices
  • 7. ( key1 , value1 ) ( key2 , value2 ) ( key3 , value3 ) ... convert the input matrices to the form:
  • 8. Pre-processing used for matrix multiplication:
  • 9. Overview of the MapReduce Algorithm for Matrix Multiplication ( (A, 1, 1) , a11 ) ( (A, 1, 2) , a12 ) ( (A, 1, 3) , a13 ) ... ( (B, 1, 1) , b11 ) ( (B, 1, 2) , b12 ) ( (B, 1, 3) , b13 ) ... •A row vector from matrix A •A column vector from matrix B •The input to the Map( ) is as follows: •The input to one reduce( ) function is as follows:
  • 11. Algorithm for Map Function. for each element mij of M do produce (key,value) pairs as ((i,k), (M,j,mij), for k=1,2,3,.. upto the number of columns of N for each element njk of N do produce (key,value) pairs as ((i,k),(N,j,Njk), for i = 1,2,3,.. Upto the number of rows of M. return Set of (key,value) pairs that each key (i,k), has list with values (M,j,mij) and (N, j,njk) for all possible values of j
  • 12. Algorithm for Reduce Function. for each key (i,k) do sort values begin with M by j in listM sort values begin with N by j in listN multiply mij and njk for jth value of each list sum up mij x njk return (i,k), Σj=1 mij x njk
  • 13. Creating Mapper file for Matrix Multiplication. import org.apache.hadoop.conf.*; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException; public class Map extends org.apache.hadoop.mapreduce.Mapper<LongWritable, Text, Text, Text> { @Override public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { Configuration conf = context.getConfiguration(); int m = Integer.parseInt(conf.get("m")); int p = Integer.parseInt(conf.get("p")); String line = value.toString(); // (M, i, j, Mij); String[] indicesAndValue = line.split(","); Text outputKey = new Text(); Text outputValue = new Text(); if (indicesAndValue[0].equals("M")) { for (int k = 0; k < p; k++) { outputKey.set(indicesAndValue[1] + "," + k); // outputKey.set(i,k); outputValue.set(indicesAndValue[0] + "," + indicesAndValue[2] + "," + indicesAndValue[3]); // outputValue.set(M,j,Mij); context.write(outputKey, outputValue); } } else { // (N, j, k, Njk); for (int i = 0; i < m; i++) { outputKey.set(i + "," + indicesAndValue[2]); outputValue.set("N," + indicesAndValue[1] + "," + indicesAndValue[3]); context.write(outputKey, outputValue); } } } }
  • 14. Creating Reducer.java file for Matrix Multiplication. import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; import java.util.HashMap; public class Reduce extends org.apache.hadoop.mapreduce.Reducer<Text, Text, Text, Text> { @Override public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { String[] value; //key=(i,k), //Values = [(M/N,j,V/W),..] HashMap<Integer, Float> hashA = new HashMap<Integer, Float>(); HashMap<Integer, Float> hashB = new HashMap<Integer, Float>(); for (Text val : values) { value = val.toString().split(","); if (value[0].equals("M")) { hashA.put(Integer.parseInt(value[1]), Float.parseFloat(value[2])); } else { hashB.put(Integer.parseInt(value[1]), Float.parseFloat(value[2])); } } int n = Integer.parseInt(context.getConfiguration().get("n")); float result = 0.0f; float m_ij; float n_jk; for (int j = 0; j < n; j++) { m_ij = hashA.containsKey(j) ? hashA.get(j) : 0.0f; n_jk = hashB.containsKey(j) ? hashB.get(j) : 0.0f; result += m_ij * n_jk; } if (result != 0.0f) { context.write(null, new Text(key.toString() + "," + Float.toString(result))); } } }
  • 15. Creating MatrixMultiply.java file for import org.apache.hadoop.conf.*; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.*; import org.apache.hadoop.mapreduce.*; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.input.TextInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; public class MatrixMultiply { public static void main(String[] args) throws Exception { if (args.length != 2) { System.err.println("Usage: MatrixMultiply <in_dir> <out_dir>"); System.exit(2); } Configuration conf = new Configuration(); // M is an m-by-n matrix; N is an n-by-p matrix. conf.set("m", "1000"); conf.set("n", "100"); conf.set("p", "1000"); @SuppressWarnings("deprecation") Job job = new Job(conf, "MatrixMultiply"); job.setJarByClass(MatrixMultiply.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); job.setMapperClass(Map.class); job.setReducerClass(Reduce.class); job.setInputFormatClass(TextInputFormat.class); job.setOutputFormatClass(TextOutputFormat.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true); } }
  • 16.  Creating Jar file for the Matrix Multiplication. $ jar -cvf MatrixMultiply.jar  Uploading the M, N file which contains the matrix multiplication data to HDFS. $ hadoop fs -mkdir Matrix/ $ hadoop fs -copyFromLocal M Matrix/ $ hadoop fs -copyFromLocal N Matrix/
  • 17. Executing the jar file using hadoop command and thus how fetching record from HDFS and storing output in HDFS. $ hadoop jar MatrixMultiply.jar www.ehadoopinfo.com.MatrixMultiply Matrix/* result/