java 矩阵乘法的mapreduce程序实现map函数:对于矩阵M中的每个元素m(ij),产生一系列的key-value对<(i,k),(M,j,m(ij))>其中k=1,2.....知道矩阵N的总列数;对于矩阵N中的每个元素
java 矩阵乘法的mapreduce程序实现
map函数:对于矩阵M中的每个元素m(ij),产生一系列的key-value对<(i,k),(M,j,m(ij))>
其中k=1,2.....知道矩阵N的总列数;对于矩阵N中的每个元素n(jk),产生一系列的key-value对<(i , k) , (N , j ,n(jk)>, 其中i=1,2.......直到i=1,2.......直到矩阵M的总列数。
map
package com.cb.matrix;import static org.mockito.Matchers.intThat;import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapred.FileSplit;import org.apache.hadoop.mapReduce.Mapper;import com.sun.org.apache.bcel.internal.generic.NEW;public class MatrixMapper extends Mapper<Object, Text, Text, Text> { private Text map_key=new Text(); private Text map_value= new Text(); private int columnN; private int rowM; @Override protected void setup(Mapper<Object, Text, Text, Text>.Context context) throws IOException, InterruptedException { // TODO Auto-generated method stub Configuration config=context.getConfiguration(); columnN=Integer.parseInt(config.get("columnN")); rowM =Integer.parseInt(config.get("rowM")); } @Override protected void map(Object key, Text value, Mapper<Object, Text, Text, Text>.Context context) throws IOException, InterruptedException { // TODO Auto-generated method stub //得到文件名,从而区分输入矩阵M和N FileSplit fileSplit=(FileSplit)context.getInputSplit(); String fileName=fileSplit.getPath().getName(); if (fileName.contains("M")) { String[] tuple =value.toString().split(","); int i =Integer.parseInt(tuple[0]); String[] tuples=tuple[1].split("\t"); int j=Integer.parseInt(tuples[0]); int Mij=Integer.parseInt(tuples[1]); for(int k=1;k<columnN+1;k++){ map_key.set(i+","+k); map_value.set("M"+","+j+","+Mij); context.write(map_key, map_value); } } else if(fileName.contains("N")){ String[] tuple=value.toString().split(","); int j=Integer.parseInt(tuple[0]); String[] tuples =tuple[1].split("\t"); int k=Integer.parseInt(tuples[0]); int Njk=Integer.parseInt(tuples[1]); for(int i=1;i<rowM+1;i++){ map_key.set(i+","+k); map_value.set("N"+","+j+","+Njk); context.write(map_key, map_value); } } }}
--结束END--
本文标题: java 矩阵乘法的mapreduce程序实现
本文链接: https://www.lsjlt.com/news/225686.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0