java 矩阵乘法的mapreduce程序实现

网友投稿 246 2023-05-09


java 矩阵乘法的mapreduce程序实现

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 {

private Text map_key=new Text();

private Text map_value= new Text();

private int columnN;

private int rowM;

/**sNMMWMWKxl

* 执行map()函数前先由conf.get()得到main函数中提供的必要变量

* 也就是从输入文件名中得到的矩阵维度信息

*/

@Override

protected void setup(Mapper.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.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(http://int k=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

map_key.set(i+","+k);

map_value.set("N"+","+j+","+Njk);

context.write(map_key, map_value);

}

}

}

}

reduce函数:对于每个键(i,k)相关联的值(M,j,m(ij))及(N,j,n(jk)),根据相同的j值将m(ij)和n(jk)分别存入不同的数组中,然后将俩者的第j个元素抽取出来分别相乘,最后相加,即可得到p(jk)的值。

reducer

package com.cb.matrix;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Reducer;

public class MatrixReducer extends Reducer {

private int sum=0;

private int columnM;

@Override

protected void setup(Reducer.Context context) throws IOException, InterruptedException {

// TODO Auto-generated method stub

Configuration conf =context.getConfiguration();

columnM=Integer.parseInt(conf.get("columnM"));

}

@Override

protected void reduce(Text arg0, Iterable arg1, Reducer.Context arg2)

throws IOException, InterruptedException {

// TODO Auto-generated method stub

int[] M=new int[columnM+1];

int[] N=new int[columnM+1];

for(Text val:arg1){

String[] tuple=val.toString().split(",");

if(tuple[0].equals("M")){

M[Integer.parseInt(tuple[1])]=Integer.parseInt(tuple[2]);

}else{

N[Integer.parseInt(tuple[1])]=Integer.parseInt(tuple[2]);

}

for(int j=1;j

sum+=M[j]*N[j];

}

arg2.write(arg0, new Text(Integer.toString(sum)));

sum=0;

}

}

}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

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

map_key.set(i+","+k);

map_value.set("N"+","+j+","+Njk);

context.write(map_key, map_value);

}

}

}

}

reduce函数:对于每个键(i,k)相关联的值(M,j,m(ij))及(N,j,n(jk)),根据相同的j值将m(ij)和n(jk)分别存入不同的数组中,然后将俩者的第j个元素抽取出来分别相乘,最后相加,即可得到p(jk)的值。

reducer

package com.cb.matrix;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Reducer;

public class MatrixReducer extends Reducer {

private int sum=0;

private int columnM;

@Override

protected void setup(Reducer.Context context) throws IOException, InterruptedException {

// TODO Auto-generated method stub

Configuration conf =context.getConfiguration();

columnM=Integer.parseInt(conf.get("columnM"));

}

@Override

protected void reduce(Text arg0, Iterable arg1, Reducer.Context arg2)

throws IOException, InterruptedException {

// TODO Auto-generated method stub

int[] M=new int[columnM+1];

int[] N=new int[columnM+1];

for(Text val:arg1){

String[] tuple=val.toString().split(",");

if(tuple[0].equals("M")){

M[Integer.parseInt(tuple[1])]=Integer.parseInt(tuple[2]);

}else{

N[Integer.parseInt(tuple[1])]=Integer.parseInt(tuple[2]);

}

for(int j=1;j

sum+=M[j]*N[j];

}

arg2.write(arg0, new Text(Integer.toString(sum)));

sum=0;

}

}

}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

map_key.set(i+","+k);

map_value.set("N"+","+j+","+Njk);

context.write(map_key, map_value);

}

}

}

}

reduce函数:对于每个键(i,k)相关联的值(M,j,m(ij))及(N,j,n(jk)),根据相同的j值将m(ij)和n(jk)分别存入不同的数组中,然后将俩者的第j个元素抽取出来分别相乘,最后相加,即可得到p(jk)的值。

reducer

package com.cb.matrix;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Reducer;

public class MatrixReducer extends Reducer {

private int sum=0;

private int columnM;

@Override

protected void setup(Reducer.Context context) throws IOException, InterruptedException {

// TODO Auto-generated method stub

Configuration conf =context.getConfiguration();

columnM=Integer.parseInt(conf.get("columnM"));

}

@Override

protected void reduce(Text arg0, Iterable arg1, Reducer.Context arg2)

throws IOException, InterruptedException {

// TODO Auto-generated method stub

int[] M=new int[columnM+1];

int[] N=new int[columnM+1];

for(Text val:arg1){

String[] tuple=val.toString().split(",");

if(tuple[0].equals("M")){

M[Integer.parseInt(tuple[1])]=Integer.parseInt(tuple[2]);

}else{

N[Integer.parseInt(tuple[1])]=Integer.parseInt(tuple[2]);

}

for(int j=1;j

sum+=M[j]*N[j];

}

arg2.write(arg0, new Text(Integer.toString(sum)));

sum=0;

}

}

}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

sum+=M[j]*N[j];

}

arg2.write(arg0, new Text(Integer.toString(sum)));

sum=0;

}

}

}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:深入理解Angular4中的依赖注入
下一篇:怎样实现接口(实现接口怎么写)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~