java 矩陣乘法的mapreduce程序?qū)崿F(xiàn)
java 矩陣乘法的mapreduce程序?qū)崿F(xiàn)
map函數(shù):對(duì)于矩陣M中的每個(gè)元素m(ij),產(chǎn)生一系列的key-value對(duì)<(i,k),(M,j,m(ij))>
其中k=1,2.....知道矩陣N的總列數(shù);對(duì)于矩陣N中的每個(gè)元素n(jk),產(chǎn)生一系列的key-value對(duì)<(i , k) , (N , j ,n(jk)>, 其中i=1,2.......直到i=1,2.......直到矩陣M的總列數(shù)。
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;
/**
* 執(zhí)行map()函數(shù)前先由conf.get()得到main函數(shù)中提供的必要變量
* 也就是從輸入文件名中得到的矩陣維度信息
*/
@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
//得到文件名,從而區(qū)分輸入矩陣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);
}
}
}
}
reduce函數(shù):對(duì)于每個(gè)鍵(i,k)相關(guān)聯(lián)的值(M,j,m(ij))及(N,j,n(jk)),根據(jù)相同的j值將m(ij)和n(jk)分別存入不同的數(shù)組中,然后將倆者的第j個(gè)元素抽取出來(lái)分別相乘,最后相加,即可得到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<Text, Text, Text, Text> {
private int sum=0;
private int columnM;
@Override
protected void setup(Reducer<Text, Text, Text, Text>.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<Text> arg1, Reducer<Text, Text, Text, Text>.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<columnM+1;j++){
sum+=M[j]*N[j];
}
arg2.write(arg0, new Text(Integer.toString(sum)));
sum=0;
}
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- 深入探究如何使用Java編寫MapReduce程序
- Java大數(shù)據(jù)處理的核心技術(shù)MapReduce框架
- Java大數(shù)據(jù)開(kāi)發(fā)Hadoop?MapReduce
- java實(shí)現(xiàn)MapReduce對(duì)文件進(jìn)行切分的示例代碼
- Java基礎(chǔ)之MapReduce框架總結(jié)與擴(kuò)展知識(shí)點(diǎn)
- Java/Web調(diào)用Hadoop進(jìn)行MapReduce示例代碼
- java連接hdfs ha和調(diào)用mapreduce jar示例
- Java編寫Mapreduce程序過(guò)程淺析
相關(guān)文章
Java并發(fā)之原子性 有序性 可見(jiàn)性及Happen Before原則
一提到happens-before原則,就讓人有點(diǎn)“丈二和尚摸不著頭腦”。這個(gè)涵蓋了整個(gè)JMM中可見(jiàn)性原則的規(guī)則,究竟如何理解,把我個(gè)人一些理解記錄下來(lái)。下面可以和小編一起學(xué)習(xí)Java 并發(fā)四個(gè)原則2021-09-09
springboot+kafka中@KafkaListener動(dòng)態(tài)指定多個(gè)topic問(wèn)題
這篇文章主要介紹了springboot+kafka中@KafkaListener動(dòng)態(tài)指定多個(gè)topic問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
劍指Offer之Java算法習(xí)題精講二叉樹(shù)專題篇上
跟著思路走,之后從簡(jiǎn)單題入手,反復(fù)去看,做過(guò)之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化2022-03-03
springboot中如何配置LocalDateTime JSON返回時(shí)間戳
這篇文章主要介紹了springboot中如何配置LocalDateTime JSON返回時(shí)間戳問(wèn)題。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
使用spring?security?BCryptPasswordEncoder接入系統(tǒng)
這篇文章主要介紹了使用spring?security?BCryptPasswordEncoder接入系統(tǒng)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
springcloud項(xiàng)目快速開(kāi)始起始模板的實(shí)現(xiàn)
本文主要介紹了springcloud項(xiàng)目快速開(kāi)始起始模板思路的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12

