MapReduce Implementation Matrix multiplication: implementation code

Source: Internet
Author: User
Tags integer map class

Programming Environment:

Java Version "1.7.0_40"

Eclipse Kepler

Windows7 x64

Ubuntu 12.04 LTS

Hadoop2.2.0

Vmware 9.0.0 build-812388

Enter data:

A matrix store address: Hdfs://singlehadoop:8020/workspace/dataguru/hadoopdev/week09/matrixmultiply/matrixa/matrixa

A matrix content:
3 4 6
4 0 8

The Matrixa file has been processed as a (x,y,value) format:

0 0 3

0 1 4

0 2 6

1 0 4

1 1 0

1 2 8

B-Matrix storage address: HDFS://SINGLEHADOOP:8020/WORKSPACE/DATAGURU/HADOOPDEV/WEEK09/MATRIXMULTIPLY/MATRIXB/MATRIXB

B Matrix Content:
2 3
3 0
4 1

The MATRIXB file has been processed as a (x,y,value) format:

0 0 2

0 1 3

1 0 3

1 1 0

2 0 4

2 1 1

Implementation code:

Altogether three classes:

Driver Class Mmdriver

Map Class Mmmapper

Reduce class Mmreducer

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

You can combine them into one class based on your personal habits.

Package dataguru.matrixmultiply;  
Import org.apache.hadoop.conf.Configuration;  
Import Org.apache.hadoop.fs.FileSystem;  
Import Org.apache.hadoop.fs.Path;  
Import Org.apache.hadoop.io.Text;  
Import Org.apache.hadoop.mapreduce.Job;  
Import Org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
      
Import Org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  public class Mmdriver {public static void main (string[] args) throws Exception {//Set configuration configuration  
      
conf = new Configuration ();  
Create job Job Job = new Job (conf, "matrixmultiply");  
Job.setjarbyclass (Dataguru.matrixmultiply.MMDriver.class);  
Specify Mapper & Reducer Job.setmapperclass (Dataguru.matrixmultiply.MMMapper.class);  
Job.setreducerclass (Dataguru.matrixmultiply.MMReducer.class);  
Specify output types of mapper and reducer Job.setoutputkeyclass (Text.class);  
Job.setoutputvalueclass (Text.class);  
Job.setmapoutputkeyclass (Text.class); Job.setmapouTputvalueclass (Text.class); Specify input and output directories path Inpatha = new Path ("hdfs://singlehadoop:8020/workspace/dataguru/hadoopdev/w  
Eek09/matrixmultiply/matrixa ");  
Path INPATHB = new Path ("Hdfs://singlehadoop:8020/workspace/dataguru/hadoopdev/week09/matrixmultiply/matrixb");  
Path Outpath = new Path ("Hdfs://singlehadoop:8020/workspace/dataguru/hadoopdev/week09/matrixmultiply/matrixc");  
Fileinputformat.addinputpath (Job, Inpatha);  
Fileinputformat.addinputpath (Job, INPATHB);  
      
Fileoutputformat.setoutputpath (Job,outpath);  
Delete Output Directory try{filesystem HDFs = Outpath.getfilesystem (conf);  
if (hdfs.exists (Outpath)) Hdfs.delete (Outpath);  
Hdfs.close ();  
catch (Exception e) {e.printstacktrace ();  
return;  
}//Run the Job System.exit (Job.waitforcompletion (true)? 0:1); }  
}
Package dataguru.matrixmultiply;  
      
Import java.io.IOException;  
      
      
      
Import Java.util.StringTokenizer;  
      
Import Org.apache.hadoop.io.Text;  
      
Import Org.apache.hadoop.mapreduce.Mapper;  
      
      
      
Import Org.apache.hadoop.mapreduce.lib.input.FileSplit;  
      
      
public class Mmmapper extends Mapper<object, text, text, text> {private String tag;//current Matrix private int crow = number of rows of 2;//a matrix private int ccol = 2;//b matrix @Override Prote CTED void Setup throws IOException, interruptedexception {//TODO get InputPath of INP  
      
UT data, set to tag Filesplit fs = (Filesplit) context.getinputsplit ();  
      
Tag = Fs.getpath (). GetParent (). GetName ();  }/** * input data include two matrix files/public void map (Object key, Text value, Context context) throws IOexception, interruptedexception {stringtokenizer str = new StringTokenizer (value.tostring ()); if ("Matrixa". Equals (Tag)) {//left matrix,output key:x,y while (Str.hasmoretokens ()) {String CurrentX = Str.nexttoken ();  
      
X,y,value of current item String currenty = Str.nexttoken ();  
      
String CurrentValue = Str.nexttoken ();  
      
for (int i = 0; i < Ccol i++) {Text Outkey = new Text (currentx+ "," +i);  
      
Text Outvalue = new text ("A," +currenty+ "," +currentvalue);  
      
Context.write (Outkey, Outvalue); System.out.println (outkey+) |  
      
"+outvalue); }}else if ("Matrixb". Equals (Tag)) {while (Str.hasmoretokens ()) {String cur Rentx = Str.nexttoken ();  
      
X,y,value of current item String currenty = Str.nexttoken ();  
      
String CurrentValue = Str.nexttoken (); for (int i = 0; i < Crow i++) {Text Outkey = new Text (i+ "," +currEnty);  
      
Text Outvalue = new text ("B," +currentx+ "," +currentvalue);  
      
Context.write (Outkey, Outvalue); System.out.println (outkey+) |  
      
"+outvalue); }  
      
}  
      
      
}  
      
}  
      
}
Package dataguru.matrixmultiply;  
Import java.io.IOException;  
Import Java.util.HashMap;  
Import Java.util.Iterator;  
Import Java.util.Map;  
      
Import Java.util.StringTokenizer;  
Import org.apache.hadoop.io.IntWritable;  
Import Org.apache.hadoop.io.Text;  
Import Org.apache.hadoop.mapreduce.Reducer;  
      
Import Org.apache.hadoop.mapreduce.Reducer.Context; public class Mmreducer extends Reducer<text, text, text, text> {public void reduce (text key, iterable<t Ext> values, Context context) throws IOException, interruptedexception {map<string,string> Matrixa =  
New Hashmap<string,string> ();  
map<string,string> MATRIXB = new hashmap<string,string> (); for (Text val:values) {//values example:b,0,2 or a,0,4 stringtokenizer str = new StringTokenizer (val.tostring (), ","  
);  
String Sourcematrix = Str.nexttoken ();  if ("a". Equals (Sourcematrix)) {Matrixa.put (Str.nexttoken (), Str.nexttoken ());//(0,4)}
if ("B". Equals (Sourcematrix)) {Matrixb.put (Str.nexttoken (), Str.nexttoken ());//(0,2)}} int result = 0;  
Iterator<string> iter = Matrixa.keyset (). iterator ();  
while (Iter.hasnext ()) {String Mapkey = Iter.next ();  
result = Integer.parseint (Matrixa.get (Mapkey)) * Integer.parseint (Matrixb.get (Mapkey));  
} context.write (Key, New Text (string.valueof)); }  
}

Final output results:

0,042
0,115
1,040
1,120

Author: csdn Blog u014512124

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.