Package Org.bigdata.util;import Java.io.ioexception;import Java.util.arraylist;import java.util.Collections;import Java.util.comparator;import Java.util.list;import Java.util.scanner;import org.apache.hadoop.conf.Configuration; Import Org.apache.hadoop.fs.path;import Org.apache.hadoop.io.longwritable;import Org.apache.hadoop.io.text;import Org.apache.hadoop.mapreduce.job;import Org.apache.hadoop.mapreduce.mapper;import Org.apache.hadoop.mapreduce.reducer;import Org.apache.hadoop.mapreduce.lib.input.fileinputformat;import Org.apache.hadoop.mapreduce.lib.input.filesplit;import Org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;/** * Large Matrix multiplication * * @author WWHHF * */ Public classMatrixmapreduce { Public Static classNode {PrivateInteger i =NULL;PrivateInteger j =NULL;PrivateLong val =NULL; Public Node(Integer i, Integer j, Long val) {super (); This. i = i; This. J = J; This. val = val; } PublicIntegerGeti() {returnI } PublicIntegerGETJ() {returnJ } PublicLongGetval() {returnVal } @Override PublicStringtoString() {return "Node [i=]+ i +", j="+ j +", val="+ val +"]"; } } Public Static classMatrixcomparator implements comparator<node> {@Override Public int Compare(Node O1, node O2) {if(O1.geti () = = O2.geti ()) {return(int) (O1.GETJ ()-O2.GETJ ()); }Else{return(int) (O1.geti ()-O2.geti ()); } } } Public Static classMatrixmapper extends Mapper<longwritable, text, text, text> {Private intM =0;Private intN =0; @Overrideprotected void Map(longwritable key, Textvalue, context context) throws IOException, interruptedexception {filesplit filesplit = (filesplit) Context.getinputsplit (); String fileName = Filesplit.getpath (). GetName (); String terms[] =value. toString (). Split (" "); String xy[] = terms[0].split (",");intx = integer.valueof (xy[0]);inty = integer.valueof (xy[1]);//Matrix M*n if(Filename.startswith ("M")) {//Matrix M for(inti =1; I <= N; i++) {Context.write (NewText (x +","+ i),NewText ("M"+value. toString ())); } }Else{//Matrix n for(inti =1; I <= M; i++) {Context.write (NewText (i +","+ y),NewText ("N"+value. toString ())); }}} @Overrideprotected void Setup(context context) throws IOException, interruptedexception {Configuration config = context.getc Onfiguration (); M = Config.getint ("M",0); N = Config.getint ("N",0); } } Public Static classMatrixreducer extends Reducer<text, text, text, longwritable> {Private intM =0;Private intN =0; @Overrideprotected void Reduce(Text key, iterable<text> values, context context) throws IOException, Interruptedexception { List<node> Mmatrix =NewArraylist<> (); List<node> Nmatrix =NewArraylist<> (); for(Textvalue: values) {String record =value. toString (); String terms[] = record.substring (1). Split (" "); String xy[] = terms[0].split (",");intx = integer.valueof (xy[0]);inty = integer.valueof (xy[1]);Longval = integer.valueof (terms[1]);if(Record.startswith ("M")) {//Matrix MMmatrix.add (NewNode (x, Y, Val)); }Else{Nmatrix.add (NewNode (x, Y, Val)); }} comparator<node> cmp =NewMatrixcomparator (); Collections.sort (Mmatrix, CMP); Collections.sort (Nmatrix, CMP); System. out. println (Mmatrix); System. out. println (Nmatrix);if(nmatrix.size () = = Mmatrix.size ()) {Longsum =0L for(Node A:mmatrix) { for(Node B:nmatrix) {sum = sum + (a.getval () * B.getval ()); }} context.write (Key,NewLongwritable (sum)); }} @Overrideprotected void Setup(context context) throws IOException, interruptedexception {Configuration config = context.getc Onfiguration (); M = Config.getint ("M",0); N = Config.getint ("N",0); } } Public Static void Main(string[] args) {Scanner cin =NewScanner (System.inch);Try{Configuration cfg = hadoopcfg.getconfiguration (); Cfg.setint ("M", Cin.nextint ()); Cfg.setint ("K", Cin.nextint ()); Cfg.setint ("N", Cin.nextint ()); Job Job = job.getinstance (CFG); Job.setjobname ("Matrix"); Job.setjarbyclass (Matrixmapreduce.class);//MapperJob.setmapperclass (Matrixmapper.class); Job.setmapoutputkeyclass (Text.class); Job.setmapoutputvalueclass (Text.class);//ReducerJob.setreducerclass (Matrixreducer.class); Job.setoutputkeyclass (Text.class); Job.setoutputvalueclass (Longwritable.class); Fileinputformat.addinputpath (Job,NewPath ("/matrix")); Fileoutputformat.setoutputpath (Job,NewPath ("/matrix_out/")); System.exit (Job.waitforcompletion (true) ?0:1); }Catch(IllegalStateException | IllegalArgumentException | ClassNotFoundException | IOException | Interruptedexception e) {e.printstacktrace (); } }}
Hadoop Large matrix multiplication