欢迎来到天天文库
浏览记录
ID:41625691
大小:53.43 KB
页数:4页
时间:2019-08-29
《基于MapReduce的矩阵相乘算法代码及其使用》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、MMMapper类代码:packagecom;importjava.ioJOException;importjava.util.StringTokenizer;importorg.apache.hadoop.io.Text;importorg.apache.hadoop.mapreduce.Mapper;importorg.apache.hadoop.mapreduce.lib.input.FileSplit;publicclassMMMapperextendsMapper2、xt,Text>{privateStringtag;//currentmatrixprivateintcrow=4;//炬阵A的彳亍数privateintccol=4;//矩阵B的列数privatestaticintarow=0;//currentarowprivatestaticintbrow=0;//currentbrow@Overrideprotectedvoidsetup(Contextcontext)throwsIOException,InterruptedException{//TODOge3、tinputpathofinputdata,settotagFileSplitfs=(FileSplit)context.getInputSplit();tag=fs•getPath().getParent().getName();*inputdataincludetwomatrixfiles*/publicvoidmap(Objectkey,Textvalue,Contextcontext)throwsIOException,InterruptedException{StringTokenizerst4、r=newStringTokenizer(value.toStringO);ifC'matrixAlequals(tag)){//leftmatrix,outputkey:x,yintcol=0;while(str.hasMoreTokens()){Stringitem=str.nextToken();//currentx,y=line.colfor(inti=0;i5、ol+",”+item);context.write(outkey,outvalue);System.out.println(outkey+nIH+outvalue);}CO1++;}arow++;}elseif(HmatrixBH.equals(tag)){intcol=0;while(str.hasMoreTokens()){Stringitem=str.nextToken();//currentx,y=line,colfor(inti=0;i6、t(i+","+col);Textoutvalue=newText(”b,”+brow+”,”+item);context.write(outkey,outvalue);System.out.println(outkey+nIH+outvalue);}col++;}brow++;}}}MMReducer类代码:packagecom;importjava.io.IOException;importjava.util.HashMap;importjava.util.Iterator;importjava.u7、til.Map;importjava.util.StringTokenizer;importorg.apache.hadoop.io.Text;importorg.apache.hadoop.mapreduce.Reducer;publicclassMMReducerextendsReducer{publicvoidreduce(Textkey,Iterablevalues,Contextcontext)throwsIOException,Inter8、ruptedException{Mapmatrixa=newHashMap();Mapmatrixb=newHashMap();for(Textval:values){//valuesexample:b,0,2ora,0,4StringTokenizerstr=newStringTokenizer(vaLtoString(
2、xt,Text>{privateStringtag;//currentmatrixprivateintcrow=4;//炬阵A的彳亍数privateintccol=4;//矩阵B的列数privatestaticintarow=0;//currentarowprivatestaticintbrow=0;//currentbrow@Overrideprotectedvoidsetup(Contextcontext)throwsIOException,InterruptedException{//TODOge
3、tinputpathofinputdata,settotagFileSplitfs=(FileSplit)context.getInputSplit();tag=fs•getPath().getParent().getName();*inputdataincludetwomatrixfiles*/publicvoidmap(Objectkey,Textvalue,Contextcontext)throwsIOException,InterruptedException{StringTokenizerst
4、r=newStringTokenizer(value.toStringO);ifC'matrixAlequals(tag)){//leftmatrix,outputkey:x,yintcol=0;while(str.hasMoreTokens()){Stringitem=str.nextToken();//currentx,y=line.colfor(inti=0;i5、ol+",”+item);context.write(outkey,outvalue);System.out.println(outkey+nIH+outvalue);}CO1++;}arow++;}elseif(HmatrixBH.equals(tag)){intcol=0;while(str.hasMoreTokens()){Stringitem=str.nextToken();//currentx,y=line,colfor(inti=0;i6、t(i+","+col);Textoutvalue=newText(”b,”+brow+”,”+item);context.write(outkey,outvalue);System.out.println(outkey+nIH+outvalue);}col++;}brow++;}}}MMReducer类代码:packagecom;importjava.io.IOException;importjava.util.HashMap;importjava.util.Iterator;importjava.u7、til.Map;importjava.util.StringTokenizer;importorg.apache.hadoop.io.Text;importorg.apache.hadoop.mapreduce.Reducer;publicclassMMReducerextendsReducer{publicvoidreduce(Textkey,Iterablevalues,Contextcontext)throwsIOException,Inter8、ruptedException{Mapmatrixa=newHashMap();Mapmatrixb=newHashMap();for(Textval:values){//valuesexample:b,0,2ora,0,4StringTokenizerstr=newStringTokenizer(vaLtoString(
5、ol+",”+item);context.write(outkey,outvalue);System.out.println(outkey+nIH+outvalue);}CO1++;}arow++;}elseif(HmatrixBH.equals(tag)){intcol=0;while(str.hasMoreTokens()){Stringitem=str.nextToken();//currentx,y=line,colfor(inti=0;i6、t(i+","+col);Textoutvalue=newText(”b,”+brow+”,”+item);context.write(outkey,outvalue);System.out.println(outkey+nIH+outvalue);}col++;}brow++;}}}MMReducer类代码:packagecom;importjava.io.IOException;importjava.util.HashMap;importjava.util.Iterator;importjava.u7、til.Map;importjava.util.StringTokenizer;importorg.apache.hadoop.io.Text;importorg.apache.hadoop.mapreduce.Reducer;publicclassMMReducerextendsReducer{publicvoidreduce(Textkey,Iterablevalues,Contextcontext)throwsIOException,Inter8、ruptedException{Mapmatrixa=newHashMap();Mapmatrixb=newHashMap();for(Textval:values){//valuesexample:b,0,2ora,0,4StringTokenizerstr=newStringTokenizer(vaLtoString(
6、t(i+","+col);Textoutvalue=newText(”b,”+brow+”,”+item);context.write(outkey,outvalue);System.out.println(outkey+nIH+outvalue);}col++;}brow++;}}}MMReducer类代码:packagecom;importjava.io.IOException;importjava.util.HashMap;importjava.util.Iterator;importjava.u
7、til.Map;importjava.util.StringTokenizer;importorg.apache.hadoop.io.Text;importorg.apache.hadoop.mapreduce.Reducer;publicclassMMReducerextendsReducer{publicvoidreduce(Textkey,Iterablevalues,Contextcontext)throwsIOException,Inter
8、ruptedException{Mapmatrixa=newHashMap();Mapmatrixb=newHashMap();for(Textval:values){//valuesexample:b,0,2ora,0,4StringTokenizerstr=newStringTokenizer(vaLtoString(
此文档下载收益归作者所有