之前写了一篇分析MapReduce实现矩阵乘法算法的文章:

【甘道夫】Mapreduce实现矩阵乘法的算法思路

为了让大家更直观的了解程序运行,今天编写了实现代码供大家參考。

编程环境:

java version "1.7.0_40"

Eclipse Kepler

Windows7 x64

Ubuntu 12.04 LTS

Hadoop2.2.0

Vmware 9.0.0 build-812388

输入数据:

A矩阵存放地址:hdfs://singlehadoop:8020/workspace/dataguru/hadoopdev/week09/matrixmultiply/matrixA/matrixa

A矩阵内容:
3 4 6
4 0 8

matrixa文件已处理为(x,y,value)格式:

0 0 3

0 1 4

0 2 6

1 0 4

1 1 0

1 2 8

B矩阵存放地址:hdfs://singlehadoop:8020/workspace/dataguru/hadoopdev/week09/matrixmultiply/matrixB/matrixb

B矩阵内容:
2 3
3 0
4 1

matrixb文件已处理为(x,y,value)格式:

0 0 2

0 1 3

1 0 3

1 1 0

2 0 4

2 1 1

实现代码:

一共三个类:

  • 驱动类MMDriver
  • Map类MMMapper
  • Reduce类MMReducer

大家可依据个人习惯合并成一个类使用。

package dataguru.matrixmultiply;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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 MMDriver { public static void main(String[] args) throws Exception { // set configuration
Configuration conf = new Configuration(); // create job
Job job = new Job(conf,"MatrixMultiply");
job.setJarByClass(dataguru.matrixmultiply.MMDriver.class); // specify Mapper & Reducer
job.setMapperClass(dataguru.matrixmultiply.MMMapper.class);
job.setReducerClass(dataguru.matrixmultiply.MMReducer.class); // specify output types of mapper and reducer
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class); // specify input and output DIRECTORIES
Path inPathA = new Path("hdfs://singlehadoop:8020/workspace/dataguru/hadoopdev/week09/matrixmultiply/matrixA");
Path inPathB = new Path("hdfs://singlehadoop:8020/workspace/dataguru/hadoopdev/week09/matrixmultiply/matrixB");
Path outPath = new Path("hdfs://singlehadoop:8020/workspace/dataguru/hadoopdev/week09/matrixmultiply/matrixC");
FileInputFormat.addInputPath(job, inPathA);
FileInputFormat.addInputPath(job, inPathB);
FileOutputFormat.setOutputPath(job,outPath); // delete output directory
try{
FileSystem hdfs = outPath.getFileSystem(conf);
if(hdfs.exists(outPath))
hdfs.delete(outPath);
hdfs.close();
} catch (Exception e){
e.printStackTrace();
return ;
} // run the job
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
package dataguru.matrixmultiply;

import java.io.IOException;

import java.util.StringTokenizer;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Mapper;

import org.apache.hadoop.mapreduce.lib.input.FileSplit;

public class MMMapper extends Mapper
package dataguru.matrixmultiply;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Reducer.Context; public class MMReducer extends Reducer { public void reduce(Text key, Iterable values, Context context)
throws IOException, InterruptedException { Map matrixa = new HashMap();
Map matrixb = new HashMap(); for (Text val : values) { //values example : b,0,2 or a,0,4
StringTokenizer str = new StringTokenizer(val.toString(),",");
String sourceMatrix = str.nextToken();
if ("a".equals(sourceMatrix)) {
matrixa.put(str.nextToken(), str.nextToken()); //(0,4)
}
if ("b".equals(sourceMatrix)) {
matrixb.put(str.nextToken(), str.nextToken()); //(0,2)
}
} int result = 0;
Iterator iter = matrixa.keySet().iterator();
while (iter.hasNext()) {
String mapkey = iter.next();
result += Integer.parseInt(matrixa.get(mapkey)) * Integer.parseInt(matrixb.get(mapkey));
} context.write(key, new Text(String.valueOf(result)));
}
}

终于输出结果:

0,0 42
0,1 15
1,0 40
1,1 20

【甘道夫】MapReduce实现矩阵乘法--实现代码的更多相关文章

  1. 【甘道夫】Win7x64环境下编译Apache Hadoop2.2.0的Eclipse小工具

    目标: 编译Apache Hadoop2.2.0在win7x64环境下的Eclipse插件 环境: win7x64家庭普通版 eclipse-jee-kepler-SR1-win32-x86_64.z ...

  2. MapReduce实现矩阵乘法

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

  3. 【甘道夫】官方网站MapReduce代码注释具体实例

    引言 1.本文不描写叙述MapReduce入门知识,这类知识网上非常多.请自行查阅 2.本文的实例代码来自官网 http://hadoop.apache.org/docs/current/hadoop ...

  4. mapreduce 实现矩阵乘法

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

  5. 基于MapReduce的矩阵乘法

    参考:http://blog.csdn.net/xyilu/article/details/9066973文章 文字未得及得总结,明天再写文字,先贴代码 package matrix; import ...

  6. 【甘道夫】怎样在cdh5.2上执行mahout的itemcf on hadoop

    环境: hadoop-2.5.0-cdh5.2.0 mahout-0.9-cdh5.2.0 步骤: 基本思路是,将mahout下的全部jar包都引入hadoop的classpath就可以,所以改动了$ ...

  7. 【甘道夫】Hive 0.13.1 on Hadoop2.2.0 + Oracle10g部署详细解释

    环境: hadoop2.2.0 hive0.13.1 Ubuntu 14.04 LTS java version "1.7.0_60" Oracle10g ***欢迎转载.请注明来 ...

  8. 【甘道夫】通过Mahout构建贝叶斯文本分类器案例具体解释

    背景&目标: 1.sport.tar 是体育类的文章,一共同拥有10个类别.    用这些原始材料构造一个体育类的文本分类器,并測试对照bayes和cbayes的效果:    记录分类器的构造 ...

  9. 【甘道夫】Win7环境下Eclipse连接Hadoop2.2.0

    准备: 确保hadoop2.2.0集群正常执行 1.eclipse中建立javaproject,导入hadoop2.2.0相关jar包 2.在src根文件夹下拷入log4j.properties,通过 ...

随机推荐

  1. [Leetcode221]最大面积 Maximal Square

    [题目] Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's a ...

  2. eclipse搭建ssm框架

    新建数据库ssm 建立数据库表user CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT , `sex` varchar(255) ...

  3. Mysql使用information.shema.tables查询数据库表大小

    简介: information_schema数据库中的表都是只读的,不能进行更新.删除和插入等操作,也不能加触发器,因为它们实际只是一个视图,不是基本表,没有关联的文件. 元数据描述数据的数据,用于描 ...

  4. TortoiseGit 的下载与安装

    一.下载 访问https://tortoisegit.org/ 二.安装 然后就next,install 配置参考:2. TortoiseGit安装与配置

  5. netty源码理解(一):new一个NioEventLoopGroup的时候做了哪些事

    好了,回到构造方法的调用中

  6. Java 容器

    摘录来至https://www.cnblogs.com/LipeiNet/p/5888513.html https://www.cnblogs.com/acm-bingzi/p/javaMap.htm ...

  7. github咋用昂

    github-trend:https://github.com/trending github-usingway:https://zhuanlan.zhihu.com/p/41899093 githu ...

  8. python自学第四天,字符串用法

    String 的用法 names="张三 welcome {city}" print(names.capitalize())#首字母大写 print(names.count(&qu ...

  9. Power BI与Tableau基于Google搜索上的比较

    在数据分析领域里,不少的数据爱好者都会关心什么数据分析产品最好用?最重要的是,很多的企业也特别希望员工能真正知道如何使用这些BI平台以确保公司的投资是值得.同类的文章,小悦也曾发布过,可参考最近< ...

  10. Oracle client安装教程

    一.下载 下载地址:http://download.csdn.net/detail/qq_35624642/9773986 这是Oracle Instant Client的CSDN下载地址. 要注意第 ...