MapReduce实现矩阵相乘
矩阵相乘能够查看百度百科的解释http://baike.baidu.com/view/2455255.htm?fr=aladdin
有a和b两个矩阵
a: 1 2 3
4 5 0
7 8 9
10 11 12
b: 10 15
0 2
11 9
c = a*b
1:将矩阵存到HDFS中:
矩阵a:
| 行 | 列 | 值 | hdfs存储 |
| 1 | 1 | 1 | 1,1,1 |
| 1 | 2 | 2 | 1,2,2 |
| 1 | 3 | 3 | 1,3,3 |
| 2 | 1 | 4 | 2,1,4 |
| 2 | 2 | 5 | 2,2,5 |
| 2 | 3 | 0 | 0不存储 |
| 3 | 1 | 7 | 3,1,7 |
| 3 | 2 | 8 | 3,2,8 |
| 3 | 3 | 9 | 3,3,9 |
| 4 | 1 | 10 | 4,1,10 |
| 4 | 2 | 11 | 4,2,11 |
| 4 | 3 | 12 | 4,3,12 |
矩阵b:
| 行 | 列 | 值 | hdfs存储 |
| 1 | 1 | 10 | 1,1,10 |
| 1 | 2 | 15 | 1,2,15 |
| 2 | 1 | 0 | 0不存储 |
| 2 | 2 | 2 | 2,2,2 |
| 3 | 1 | 11 | 3,1,11 |
| 3 | 2 | 9 | 3,2,9 |
2:a的map读取
读取第一个值是1,1,1。它是矩阵a的第一行第一列。那么它要在计算c(1,1) c(1,2)的时候使用(这里c仅仅用2列,假设用n列,那么它的值要在计算c(1,1),c(1,2),c(1,3)...c(1,n)的时候使用)。我们就以 key = 1,1 value = a,1,1 , key= 1,2 value = a,1,1输出两条数据(1,1) (1,2)是 c(1,1) ,c(1,2)的坐标。
b的map读取
读取第一个值是1,1,10。它是矩阵b的第一行第一列。那么它要在计算c(1,1) c(2,1) c(3,1) c(4,1)的时候使用(这里c仅仅用4行,假设用m行,那么它的值要在计算c(1,1),c(2,1),c(3,1)...c(m,1)的时候使用)。我们就以 key = 1,1 value = b,1,10 , key= 2,1 value = b,1,10 , key = 3,1 value = b,1,10 ,
key= 4,1 value = b,1,10输出
3:reduce读取计算
通过mapA和mapB的输出能够得到 key = 1,1 , value=a,1,1 value=b,1,10 value=a,2,2 value=a,3,3 value = b,3,11l来计算c(1,1)的值
代码例如以下:
package MyMatrix; import java.io.IOException;
import java.util.Iterator; import org.apache.hadoop.conf.Configuration;
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.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class MartrixMultiply{ /**
* 最后得到的矩阵的列数
*/
public static final int COL_COUNT = 2; /**
* 最后得到的矩阵的行数
*/
public static final int ROW_COUNT = 4; /**
* A矩阵的列数或者是B矩阵的行数
*/
public static final int BROW_ACOL= 3; public static class MartrixMaperA extends Mapper<LongWritable, Text, Text, Text>{ @Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException { String valueStr = value.toString();
String[] items = valueStr.split(","); int rowIndex = Integer.parseInt(items[0]);
int colIndex = Integer.parseInt(items[1]);
int valueInt = Integer.parseInt(items[2]); Text outKey = null;
Text outValue = null;
for(int i=0;i<COL_COUNT;i++){
outKey = new Text(rowIndex + "," + (i+1));
outValue = new Text("a,"+colIndex+","+valueInt);
context.write(outKey, outValue);
} } } public static class MartrixMaperB extends Mapper<LongWritable, Text, Text, Text>{ @Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException { String valueStr = value.toString();
String[] items = valueStr.split(","); int rowIndex = Integer.parseInt(items[0]);
int colIndex = Integer.parseInt(items[1]);
int valueInt = Integer.parseInt(items[2]); Text outKey = null;
Text outValue = null;
for(int i=0;i<ROW_COUNT;i++){
outKey = new Text((i+1) + "," + colIndex);
outValue = new Text("b,"+rowIndex+","+valueInt);
context.write(outKey, outValue);
} } } public static class MartrixReducer extends Reducer<Text,Text,Text,IntWritable>{ @Override
protected void reduce(Text key, Iterable<Text> values,Context context)
throws IOException, InterruptedException { String[] items = new String[3]; int[] valueA = new int[BROW_ACOL];
int[] valueB = new int[BROW_ACOL]; Iterator<Text> it = values.iterator();
while(it.hasNext()){
items = it.next().toString().split(",");
if(items[0].equals("a")){
valueA[Integer.parseInt(items[1])-1] = Integer.parseInt(items[2]);
}else if(items[0].equals("b")){
valueB[Integer.parseInt(items[1])-1] = Integer.parseInt(items[2]);
}
} int result = 0;
for(int i=0;i<BROW_ACOL;i++){
result += valueA[i]*valueB[i];
}
context.write(key, new IntWritable(result));
} } @SuppressWarnings("deprecation")
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { Path pathA = new Path("hdfs://localhost:9000/Martrix/a.txt");
Path pathB = new Path("hdfs://localhost:9000/Martrix/b.txt");
Path pathOut = new Path("hdfs://localhost:9000/Martrix/out"); Configuration conf = new Configuration();
Job job = new Job(conf,"MartrixMultiply"); job.setJarByClass(MartrixMultiply.class); MultipleInputs.addInputPath(job, pathA, TextInputFormat.class, MartrixMaperA.class);
MultipleInputs.addInputPath(job, pathB, TextInputFormat.class, MartrixMaperB.class); job.setReducerClass(MartrixReducer.class); job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); FileOutputFormat.setOutputPath(job, pathOut); if(job.waitForCompletion(true)){
System.exit(0);
}else{
System.exit(1);
} } }
MapReduce实现矩阵相乘的更多相关文章
- Python+MapReduce实现矩阵相乘
算法原理 map阶段 在map阶段,需要做的是进行数据准备.把来自矩阵A的元素aij,标识成p条<key, value>的形式,key="i,k",(其中k=1,2,. ...
- MapReduce的矩阵相乘
一.单个mapreduce的实现 转自:http://blog.sina.com.cn/s/blog_62186b460101ai1x.html 王斌_ICTIR老师的<大数据:互联网大规模数据 ...
- python版 mapreduce 矩阵相乘
参考张老师的mapreduce 矩阵相乘. 转载请注明:来自chybot的学习笔记http://i.cnblogs.com/EditPosts.aspx?postid=4541939 下面是我用pyt ...
- 利用Hadoop实现超大矩阵相乘之我见(二)
前文 在<利用Hadoop实现超大矩阵相乘之我见(一)>中我们所介绍的方法有着“计算过程中文件占用存储空间大”这个缺陷,本文中我们着重解决这个问题. 矩阵相乘计算思想 传统的矩阵相乘方法为 ...
- 利用Hadoop实现超大矩阵相乘之我见(一)
前记 最近,公司一位挺优秀的总务离职,欢送宴上,她对我说“你是一位挺优秀的程序员”,刚说完,立马道歉说“对不起,我说你是程序员是不是侮辱你了?”我挺诧异,程序员现在是很低端,很被人瞧不起的工作吗?或许 ...
- java 写一个 map reduce 矩阵相乘的案例
1.写一个工具类用来生成 map reduce 实验 所需 input 文件 下面两个是原始文件 matrix1.txt 1 2 -2 0 3 3 4 -3 -2 0 2 3 5 3 -1 2 -4 ...
- HDU1575Tr A(矩阵相乘与快速幂)
Tr A hdu1575 就是一个快速幂的应用: 只要知道怎么求矩阵相乘!!(比赛就知道会超时,就是没想到快速幂!!!) #include<iostream> #include<st ...
- <矩阵的基本操作:矩阵相加,矩阵相乘,矩阵转置>
//矩阵的基本操作:矩阵相加,矩阵相乘,矩阵转置 #include<stdio.h> #include<stdlib.h> #define M 2 #define N 3 #d ...
- POJ 2246 Matrix Chain Multiplication(结构体+栈+模拟+矩阵相乘)
题意:给出矩阵相乘的表达式,让你计算需要的相乘次数,如果不能相乘,则输出error. 思路: 参考的网站连接:http://blog.csdn.net/wangjian8006/article/det ...
随机推荐
- 2012年的MBP准备升级
2012年买的MBP MD313要升级啦! 原因是4G内存在升级10.9巨浪后,无论是登录还是打开程序都比较慢,看内存使用使用基本上是满了,因此有了升级内存的想法. 首先想到的是看最大容量,16G,所 ...
- ueditor 编辑器的配置 实现上传图片---附效果图
由于项目需要,最近使用了ueditor,实现了图片上传功能,在此分享一下遇到的一些问题. 项目使用net+mvc框架搭建,则选择的是NET版本ueditor 编辑器(可去百度官网下载), 下载完成导入 ...
- Delphi实现全局鼠标钩子
其中涉及到的一些API,网上均能查到详细的解释,这里不再熬述.源码下载 因为是全局钩子,所以要用dll注入.用到的鼠标消息结构如下: PMouseHookStruct = ^TMouseHookStr ...
- Android 为应用添加数字角标
今天在论坛上看到了一个帖子,终于搞清了我很久以来的一个困惑,android到底能不能实现ios的角标效果,QQ是怎么实现的.看了这个帖子顿时终于解除了我的困惑. 先说一个下大概的思路: 大家都知道an ...
- Qt入门学习——Qt 5 帮助文档的使用
Qt入门学习——Qt 5 帮助文档的使用 学习图形界面开发,肯定离不开帮助文档的使用,因为它不像 C 语言那样就那么几个函数接口,图形接口的接口可以用海量来形容,常用的我们可能能记住,其它的真的没有必 ...
- 杭电oj1326 Box of Bricks
Tips:先求出平均数再分别计算各数与平均数的差相加,注意两个测试结果之间要空一行 #include<iostream> using namespace std; int main() { ...
- Struts 2.3.4.1完整示例
[系统环境]Windows 7 Ultimate 64 Bit [开发环境]JDK1.6.21,Tomcat6.0.35,MyEclipse10 [其他环境]Struts2.3.4.1 [项目描述]S ...
- wxpython 树形控件全选和取消全选
#encoding:utf-8 import wx import wx.lib.agw.customtreectrl as CT class MyFrame(wx.Frame): def __init ...
- C-Free 您不能使用调试解决方案
什么时候C-Free 当您调试 找不到gdb.exe解决方案 http://www.programarts.com/ C-Free 官方网站 下载Mingw或者其他编译器 版权声明:本文博主原创文章. ...
- MPAndroidChart绘制图形表
最近一个项目需要用到表格进行统计显示,本来用的是的achartengine,后来发现一个更加强大的开源框架MPAndroidChart. 下面简单介绍下MPAndroidChart,MPAndroid ...