对于任意矩阵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实现的更多相关文章

  1. MapReduce实现矩阵乘法

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

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

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

  3. MapReduce实现大矩阵乘法

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

  4. *HDU2254 矩阵乘法

    奥运 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submissi ...

  5. *HDU 1757 矩阵乘法

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  6. CH Round #30 摆花[矩阵乘法]

    摆花 CH Round #30 - 清明欢乐赛 背景及描述 艺术馆门前将摆出许多花,一共有n个位置排成一排,每个位置可以摆花也可以不摆花.有些花如果摆在相邻的位置(隔着一个空的位置不算相邻),就不好看 ...

  7. POJ3070 Fibonacci[矩阵乘法]

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13677   Accepted: 9697 Descri ...

  8. bzoj 2738 矩阵乘法

    其实这题跟矩阵乘法没有任何卵关系,直接整体二分,用二维树状数组维护(刚刚学会>_<),复杂度好像有点爆炸(好像有十几亿不知道是不是算错了),但我们不能怂啊23333. #include&l ...

  9. 【BZOJ-2476】战场的数目 矩阵乘法 + 递推

    2476: 战场的数目 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 58  Solved: 38[Submit][Status][Discuss] D ...

随机推荐

  1. Java-instanceof关键字

    一.instanceof 在编写代码过程中,如果不知道一个对象属于哪一个类,这时instanceof关键字起到决定性作用,他会以boolean(true\false)反馈结果 class A{ pub ...

  2. SQL NULL 值【摘自W3C】

    SQL NULL 值 本章讲解 IS NULL 和 IS NOT NULL 操作符. NULL 值是遗漏的未知数据.默认地,表的列可以存放 NULL 值. 如果表中的某个列是可选的,那么我们可以在不向 ...

  3. break,continue的使用

    break,continue 使用break命令允许跳出所有循环下面的例子中,脚本进入死循环直至用户输入数字大于5.要跳出这个循环,返回到shell提示符下,就要使用break命令. #!/bin/b ...

  4. sqlserver和Oracle内部的错误数据修复(DBCC、DBMS_REPAIR)

    数据库长时间运行后,因断电.操作系统.物理存储等的原因可能会造成数据库内部的逻辑或物理错误,我们可以使用一般的方式尝试修复. 对于sqlserver 我们可以使用DBCC命令: -- sqlserve ...

  5. [zz] Principal Components Analysis (PCA) 主成分分析

    我理解PCA应该分为2个过程:1.求出降维矩阵:2.利用得到的降维矩阵,对数据/特征做降维. 这里分成了两篇博客,来做总结. http://matlabdatamining.blogspot.com/ ...

  6. jquery autocomplete实现读取sql数据库自动补全TextBox

    转自我本良人 原文 jquery autocomplete实现读取sql数据库自动补全TextBox 项目需要这样子一个功能,其他部门提的意见,只好去实现了哦,搞了好久才弄出来,分享一下. 1.前台页 ...

  7. Async Programming - 1 async-await 糖的本质(1)

    这一个系列的文章主要来讲 C# 中的语言特性 async-await 在语言层面的本质,我们都知道 await 是编译器进行了一个 rewrite,然而这个 rewrite 并不是直接 rewrite ...

  8. LINUX 磁盘如何分区

    fdisk -l  可以查看当前磁盘 假设未分配磁盘为/dev/sdb    size=10G fdisk /dev/sdb  (m for help) 按照提示应该可以分区成功,注意一点   一个磁 ...

  9. JavaScript的学习5

    一.DOM对象 1.DOM对象:Document  Object  Model  文档对象模型,主要是用来提供了操作HTML文档的属性与方法 2.DOM的分类: a.核心DOM:为操作XML和HTML ...

  10. 【HOW】如何手工编辑InfoPath文件

    因为直接打开InfoPath的配置文件进行编辑会有些无从下手,所以下面以修改“节”的Margin为例来说明手工编辑InfoPath文件的过程. 1. 新建一个“节”,并在此节中创建一个数字类型字段“n ...