参考:http://blog.csdn.net/xyilu/article/details/9066973文章

文字未得及得总结,明天再写文字,先贴代码

package matrix;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.RunningJob;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
public class SparseMatrix { public static class SparseMatrixMapper extends Mapper<LongWritable, Text, Text, Text> { private String flag;// A同现矩阵 or B评分矩阵 @Override
protected void setup(Context context) throws IOException, InterruptedException {
FileSplit split = (FileSplit) context.getInputSplit();
flag = split.getPath().getName();// 判断读的数据集 // System.out.println(flag);
}
private static final int rowNum = 4;// 矩阵A的行数
public static final int colA=3; //矩阵A的列数,B的行数
private static final int colNum = 2;// 矩阵B的列数 @Override
public void map(LongWritable key, Text values, Context context) throws IOException, InterruptedException { String str=values.toString();
String[] line=Recommend.DELIMITER.split(str);
if(line.length==0)return;
if(flag.equals("a.txt")){
if(line.length!=3)return;
String row=line[0];
String col=line[1];
String val=line[2];
Text k=new Text();
Text v=new Text();
for(int i=1;i<=colNum;i++){
k.set(row+","+i);
v.set("a,"+col+","+val);
System.out.println(k.toString()+" "+v.toString());
context.write(k, v);
}
}
if(flag.equals("b.txt")){
String row=line[0];
String col=line[1];
String val=line[2];
Text k=new Text();
Text v=new Text();
for(int i=1;i<=rowNum;i++){
k.set(i+","+col);
v.set("b,"+row+","+val);
context.write(k, v);
System.out.println(k.toString()+" "+v.toString());
}
} } }
public static class SparseMatrixReducer extends Reducer<Text, Text, Text, Text> {
@Override
public void reduce(Text key,Iterable<Text> values, Context context) throws IOException, InterruptedException {
Map<String,Double> map=new HashMap<String ,Double>();
Double[] A=new Double[SparseMatrixMapper.colA];
Double[] B=new Double[SparseMatrixMapper.colA];
for(int i=0;i<A.length;i++){
A[i]=0.0;
B[i]=0.0;
}
for(Text line:values){
String val=line.toString();
if(val.contains("a")){
String[] arr=Recommend.DELIMITER.split(val);
int n=Integer.valueOf(arr[1]);
A[n-1]=Double.valueOf(arr[2]);
}
else if(val.contains("b")){
String[] arr=Recommend.DELIMITER.split(val);
int n=Integer.valueOf(arr[1]);
B[n-1]=Double.valueOf(arr[2]);
}
}
Double sum=0.0;
for(int i=0;i<SparseMatrixMapper.colA;i++){
sum=sum+A[i]*B[i];
}
Text v=new Text();
v.set(sum.toString());
context.write(key, v); }
}
public static void run(Map<String, String> path) throws IOException, InterruptedException, ClassNotFoundException {
JobConf conf = Recommend.config(); String input1 = path.get("matrixMult"); String output = path.get("matrixMultOut"); HdfsDAO hdfs = new HdfsDAO(Recommend.HDFS, conf); hdfs.rmr(output);
hdfs.rmr(input1);
hdfs.mkdirs(input1);
hdfs.copyFile("datafile/week5/SparseMatrix/a.txt", input1);
hdfs.copyFile("datafile/week5/SparseMatrix/b.txt", input1);
Job job = new Job(conf);
job.setJarByClass(SparseMatrix.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); job.setMapperClass(SparseMatrixMapper.class);
job.setReducerClass(SparseMatrixReducer.class); job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class); FileInputFormat.setInputPaths(job, new Path(input1));
FileOutputFormat.setOutputPath(job, new Path(output)); job.waitForCompletion(true);
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

基于MapReduce的矩阵乘法的更多相关文章

  1. 【甘道夫】MapReduce实现矩阵乘法--实现代码

    之前写了一篇分析MapReduce实现矩阵乘法算法的文章: [甘道夫]Mapreduce实现矩阵乘法的算法思路 为了让大家更直观的了解程序运行,今天编写了实现代码供大家參考. 编程环境: java v ...

  2. MapReduce实现矩阵乘法

    简单回想一下矩阵乘法: 矩阵乘法要求左矩阵的列数与右矩阵的行数相等.m×n的矩阵A,与n×p的矩阵B相乘,结果为m×p的矩阵C.具体内容能够查看:矩阵乘法. 为了方便描写叙述,先进行如果: 矩阵A的行 ...

  3. 基于OpenMP的矩阵乘法实现及效率提升分析

    一.  矩阵乘法串行实现 例子选择两个1024*1024的矩阵相乘,根据矩阵乘法运算得到运算结果.其中,两个矩阵中的数为double类型,初值由随机数函数产生.代码如下: #include <i ...

  4. 基于MapReduce的矩阵乘法运算

    1.采用两个MapReduce运算串联来实现 Pik= Mij*Njk 第一步: Map函数:将每个矩阵运算mij传给键值对(j,(M,i,mij)),将每个矩阵元素njk传给键值对(j,(N,k,n ...

  5. mapreduce 实现矩阵乘法

    import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs ...

  6. 【BZOJ-4386】Wycieczki DP + 矩阵乘法

    4386: [POI2015]Wycieczki Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 197  Solved: 49[Submit][Sta ...

  7. Python+MapReduce实现矩阵相乘

    算法原理 map阶段 在map阶段,需要做的是进行数据准备.把来自矩阵A的元素aij,标识成p条<key, value>的形式,key="i,k",(其中k=1,2,. ...

  8. 矩阵乘法的MapReduce实现

    对于任意矩阵M和N,若矩阵M的列数等于矩阵N的行数,则记M和N的乘积为P=M*N,其中mik 记做矩阵M的第i行和第k列,nkj记做矩阵N的第k行和第j列,则矩阵P中,第i行第j列的元素可表示为公式( ...

  9. MapReduce实现大矩阵乘法

    来自:http://blog.csdn.net/xyilu/article/details/9066973 引言 何 为大矩阵?Excel.SPSS,甚至SAS处理不了或者处理起来非常困难,需要设计巧 ...

随机推荐

  1. hd acm1425

    给你n个整数,请按从大到小的顺序输出其中前m大的数. 先看代码: #include<stdio.h>#include<string.h>#define MAX 1100000i ...

  2. 《python基础教程(第二版)》学习笔记 文件和素材(第11章)

    <python基础教程(第二版)>学习笔记 文件和素材(第11章) 打开文件:open(filename[,mode[,buffering]]) mode是读写文件的模式f=open(r' ...

  3. EntityFramework 学习 一 Add New Entity using DBContext in Disconnected Scenario

    using System; using System.Collections.Generic; public partial class Student { public Student() { th ...

  4. 算法(Algorithms)第4版 练习 1.5.5

    对于quick-find,对每个输入数据对,其最少的循环次数为N(sites) 故对于109 sites和106 input pairs,其总的指令次数为:sum = 10^9 * 10^6 * 10 ...

  5. FineReport报表使用

    FineReport报表是帆软公司推出的可以嵌入java的免费报表. FineReport有2部分组成,一有c/s端的报表工具制作cpt结尾的报表文件:二是 java调用报表的web程序. 这里主要说 ...

  6. css 盒子模型 以及 box-sizing属性

    在标准的盒子模型下,css中 width,padding以及border的关系 关于css中的width和padding以及border的关系. 在css中,width和height指的是内容区域的宽 ...

  7. HTML5 Video Blob

    我的博客搬家到https://www.w2le.com/了 <video src="blob:http://www.bilibili.com/d0823f0f-2b2a-4fd6-a9 ...

  8. linux命令学习笔记(9):touch 命令

    linux的touch命令不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件. .命令格式: touch [选项]... 文件... .命令参数: -a 或--t ...

  9. linux命令学习笔记(11):nl命令

    nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文件内容自动的加上行号!其默认的结果与 等等的功能. .命令格式: nl [选项]... [文件]... .命令参数: -b :指定行号 ...

  10. 机器学习 Logistic Regression

    Logistic Regression 之前我们讨论过回归问题,并且讨论了线性回归模型.现在我们来看看分类问题,分类问题与回归问题类似,只不过输出变量一个是离散的,一个是连续的.我们先关注二分类问题, ...