矩阵乘法的MapReduce实现
对于任意矩阵M和N,若矩阵M的列数等于矩阵N的行数,则记M和N的乘积为P=M*N,其中mik 记做矩阵M的第i行和第k列,nkj记做矩阵N的第k行和第j列,则矩阵P中,第i行第j列的元素可表示为公式(1-1):
pij=(M*N)ij=∑miknkj=mi1*n1j+mi2*n2j+……+mik*nkj (公式1-1)
由公式(1-1)可以看出,最后决定pij是(i,j),所以可以将其作为Reducer的输入key值。为了求出pij分别需要知道mik和nkj,对于mik,其所需要的属性有矩阵M,所在行数i、所在列数k,和其本身的数值大小mik;同样对于nkj,其所需要的属性有矩阵N,所在行数k、所在列数j,和其本身数值大小nkj,这些属性值可由Mapper处理得到
Map函数:对于矩阵M中的每个元素mik ,产生一系列的key-value对<(i,j),(M,k,mik)>,其中,k=1,2……直到矩阵N的总列数,对于矩阵N的每个元素nkj,产生一系列的key-value对,<(i,j),(N,k,nkj)>,其中i=1,2……直到矩阵M的总行数
Reduce函数:对于每个键(i,j)相关联的值(M,k,mik)及(N,k,nkj),根据相同的k值将mik和nkj分别放入不同的数组中,然后将两者的第k个元素抽取出来分别相乘,再累加,即可得到pij的值
有M和N两个文件分别存放两个矩阵,文件内容的每一行的形式是“行号,列号\t元素值”,本例中,使用shell脚本生成数据
代码1-2
root@lejian:/data# cat matrix
#!/bin/bash
for i in `seq 1 $1`
do
for j in `seq 1 $2`
do
s=$(($RANDOM % 100))
echo -e "$i,$j\t$s" >> M_$1_$2
done
done for i in `seq 1 $2`
do
for j in `seq 1 $3`
do
s=$(($RANDOM%100))
echo -e "$i,$j\t$s" >> N_$2_$3
done
done
代码1-3,执行matrix脚本,生成一个2行3列和3行3列的矩阵,并在HDFS下新建一个data文件夹,将生成的两个矩阵放入data文件夹下
代码1-3
root@lejian:/data# ./matrix 2 3 3
root@lejian:/data# cat M_2_3
1,1 6
1,2 84
1,3 40
2,1 51
2,2 37
2,3 97
root@lejian:/data# cat N_3_3
1,1 97
1,2 34
1,3 95
2,1 93
2,2 10
2,3 70
3,1 71
3,2 24
3,3 47
root@lejian:/data# hadoop fs -mkdir /data
root@lejian:/data# hadoop fs -put /data/M_2_3 /data/
root@lejian:/data# hadoop fs -put /data/N_3_3 /data/
root@lejian:/data# hadoop fs -ls -R /data
-rw-r--r-- 1 root supergroup 41 2017-01-07 11:57 /data/M_2_3
-rw-r--r-- 1 root supergroup 63 2017-01-07 11:57 /data/N_3_3
矩阵乘法Mapper类程序如代码1-4
代码1-4
package com.hadoop.mapreduce; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit; public class MatrixMapper extends Mapper<LongWritable, Text, Text, Text> { private int columnN = 0;
private int rowM = 0;
private Text mapKey = new Text();
private Text mapValue = new Text(); protected void setup(Context context) throws IOException, InterruptedException {
Configuration conf = context.getConfiguration();
columnN = Integer.parseInt(conf.get("columnN"));
rowM = Integer.parseInt(conf.get("rowM"));
}; protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
FileSplit file = (FileSplit) context.getInputSplit();
String fileName = file.getPath().getName();
String line = value.toString();
String[] tuple = line.split(",");
if (tuple.length != 2) {
throw new RuntimeException("MatrixMapper tuple error");
}
int row = Integer.parseInt(tuple[0]);
String[] tuples = tuple[1].split("\t");
if (tuples.length != 2) {
throw new RuntimeException("MatrixMapper tuples error");
}
if (fileName.contains("M")) {
matrixM(row, Integer.parseInt(tuples[0]), Integer.parseInt(tuples[1]), context);
} else {
matrixN(row, Integer.parseInt(tuples[0]), Integer.parseInt(tuples[1]), context);
} }; private void matrixM(int row, int column, int value, Context context) throws IOException, InterruptedException {
for (int i = 1; i < columnN + 1; i++) {
mapKey.set(row + "," + i);
mapValue.set("M," + column + "," + value);
context.write(mapKey, mapValue);
}
} private void matrixN(int row, int column, int value, Context context) throws IOException, InterruptedException {
for (int i = 1; i < rowM + 1; i++) {
mapKey.set(i + "," + column);
mapValue.set("N," + row + "," + value);
context.write(mapKey, mapValue);
}
} }
矩阵乘法Reducer类程序如代码1-5
代码1-5
package com.hadoop.mapreduce; 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 columnM = 0; protected void setup(Context context) throws IOException, InterruptedException {
Configuration conf = context.getConfiguration();
columnM = Integer.parseInt(conf.get("columnM"));
}; protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
int sum = 0;
int[] m = new int[columnM + 1];
int[] n = new int[columnM + 1];
for (Text val : values) {
String[] tuple = val.toString().split(",");
if (tuple.length != 3) {
throw new RuntimeException("MatrixReducer tuple error");
}
if ("M".equals(tuple[0])) {
m[Integer.parseInt(tuple[1])] = Integer.parseInt(tuple[2]);
} else {
n[Integer.parseInt(tuple[1])] = Integer.parseInt(tuple[2]);
}
}
for (int i = 1; i < columnM + 1; i++) {
sum += m[i] * n[i];
}
context.write(key, new Text(sum + ""));
}; }
矩阵乘法主函数如代码1-5
package com.hadoop.mapreduce; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class Matrix { public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
if (args == null || args.length != 5) {
throw new RuntimeException("请输入输入路径、输出路径、矩阵M的行数、矩阵M的列数、矩阵N的列数");
}
Configuration conf = new Configuration();
conf.set("rowM", args[2]);
conf.set("columnM", args[3]);
conf.set("columnN", args[4]);
Job job = Job.getInstance(conf);
job.setJobName("Matrix");
job.setJarByClass(Matrix.class);
job.setMapperClass(MatrixMapper.class);
job.setReducerClass(MatrixReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPaths(job, args[0]);
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
} }
运行代码1-5,运行结果如代码1-6所示(注:代码1-6省略部分MapReduce执行内容)
代码1-6
root@lejian:/data# hadoop jar matrix.jar com.hadoop.mapreduce.Matrix /data/ /output/ 2 3 3
…………
root@lejian:/data# hadoop fs -ls -R /output
-rw-r--r-- 1 root supergroup 0 2017-01-07 12:04 /output/_SUCCESS
-rw-r--r-- 1 root supergroup 57 2017-01-07 12:04 /output/part-r-00000
root@lejian:/data# hadoop fs -cat /output/part-r-00000
1,1 11234
1,2 2004
1,3 8330
2,1 15275
2,2 4432
2,3 11994
矩阵乘法的MapReduce实现的更多相关文章
- MapReduce实现矩阵乘法
简单回想一下矩阵乘法: 矩阵乘法要求左矩阵的列数与右矩阵的行数相等.m×n的矩阵A,与n×p的矩阵B相乘,结果为m×p的矩阵C.具体内容能够查看:矩阵乘法. 为了方便描写叙述,先进行如果: 矩阵A的行 ...
- 【甘道夫】MapReduce实现矩阵乘法--实现代码
之前写了一篇分析MapReduce实现矩阵乘法算法的文章: [甘道夫]Mapreduce实现矩阵乘法的算法思路 为了让大家更直观的了解程序运行,今天编写了实现代码供大家參考. 编程环境: java v ...
- MapReduce实现大矩阵乘法
来自:http://blog.csdn.net/xyilu/article/details/9066973 引言 何 为大矩阵?Excel.SPSS,甚至SAS处理不了或者处理起来非常困难,需要设计巧 ...
- *HDU2254 矩阵乘法
奥运 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submissi ...
- *HDU 1757 矩阵乘法
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- CH Round #30 摆花[矩阵乘法]
摆花 CH Round #30 - 清明欢乐赛 背景及描述 艺术馆门前将摆出许多花,一共有n个位置排成一排,每个位置可以摆花也可以不摆花.有些花如果摆在相邻的位置(隔着一个空的位置不算相邻),就不好看 ...
- POJ3070 Fibonacci[矩阵乘法]
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13677 Accepted: 9697 Descri ...
- bzoj 2738 矩阵乘法
其实这题跟矩阵乘法没有任何卵关系,直接整体二分,用二维树状数组维护(刚刚学会>_<),复杂度好像有点爆炸(好像有十几亿不知道是不是算错了),但我们不能怂啊23333. #include&l ...
- 【BZOJ-2476】战场的数目 矩阵乘法 + 递推
2476: 战场的数目 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 58 Solved: 38[Submit][Status][Discuss] D ...
随机推荐
- rabbitmq 重复ACK导致消息丢失
rabbitmq 重复确认导致消息丢失 背景 rabbitmq 在应用场景中,大多采用工作队列 work-queue的模式. 在一个常见的工作队列模式中,消费者 worker 将不断的轮询从队列中拉取 ...
- time 模块
import time导入时间模块print(time.strftime("%Y-%m-%d %X"))其中 time.strftime("Y-%m-%d")输 ...
- Android模拟器连接本李服务器localhost
Android模拟(simulator)把它自己作为了localhost,也就是说,代码中使用 localhost或者127.0.0.1来访问,都是访问模拟器自己! 如果你想在模拟器simulator ...
- 承接Unity3D外包公司 — 技术分享
Cardboard SDK for Unity的使用 上一篇文章作为系列的开篇,主要是讲了一些虚拟现实的技术和原理,本篇就会带领大家去看一看谷歌的Cardboard SDK for Unity,虽然目 ...
- ife-task0003学习收获总结
+ 编写可维护的css原则是,尽量减少改动时要编辑的地方(减少代码重复). + CSS布局奇淫技巧之-高度自适应 高度自适应:通过绝对定位来解决高度自适应的问题,元素不设置高度,设置绝对定位,将top ...
- ORM框架详解
.Net开源微型ORM框架测评 什么是ORM? 对象关系映射(英语:Object Relation Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序技术,用于实现面向对象 ...
- Spring Boot(3)---自定义spring boot starter 问题
1. "Failed to process import candidates for configuration class [com.simple.....]": 主要原因: ...
- OAF_开发系列10_实现OAF动态LOV设定
20150712 Created By BaoXinjian
- Dialog_ _dialog系统样式讲解 及 透明背景
AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this,AlertDialog.THEME_TRADITIO ...
- 【解决】SharePoint 2013 当鼠标悬停在用户名称上时页面会崩溃
参考下面文章,此问题是由于IE中的Office插件版本不正确所致,只需在 控制面板 > 程序和功能 中修复 Office 的安装即可. http://sympmarc.com/2013/11/2 ...