【甘道夫】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实现矩阵乘法--实现代码的更多相关文章
- 【甘道夫】Win7x64环境下编译Apache Hadoop2.2.0的Eclipse小工具
目标: 编译Apache Hadoop2.2.0在win7x64环境下的Eclipse插件 环境: win7x64家庭普通版 eclipse-jee-kepler-SR1-win32-x86_64.z ...
- MapReduce实现矩阵乘法
简单回想一下矩阵乘法: 矩阵乘法要求左矩阵的列数与右矩阵的行数相等.m×n的矩阵A,与n×p的矩阵B相乘,结果为m×p的矩阵C.具体内容能够查看:矩阵乘法. 为了方便描写叙述,先进行如果: 矩阵A的行 ...
- 【甘道夫】官方网站MapReduce代码注释具体实例
引言 1.本文不描写叙述MapReduce入门知识,这类知识网上非常多.请自行查阅 2.本文的实例代码来自官网 http://hadoop.apache.org/docs/current/hadoop ...
- mapreduce 实现矩阵乘法
import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs ...
- 基于MapReduce的矩阵乘法
参考:http://blog.csdn.net/xyilu/article/details/9066973文章 文字未得及得总结,明天再写文字,先贴代码 package matrix; import ...
- 【甘道夫】怎样在cdh5.2上执行mahout的itemcf on hadoop
环境: hadoop-2.5.0-cdh5.2.0 mahout-0.9-cdh5.2.0 步骤: 基本思路是,将mahout下的全部jar包都引入hadoop的classpath就可以,所以改动了$ ...
- 【甘道夫】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 ***欢迎转载.请注明来 ...
- 【甘道夫】通过Mahout构建贝叶斯文本分类器案例具体解释
背景&目标: 1.sport.tar 是体育类的文章,一共同拥有10个类别. 用这些原始材料构造一个体育类的文本分类器,并測试对照bayes和cbayes的效果: 记录分类器的构造 ...
- 【甘道夫】Win7环境下Eclipse连接Hadoop2.2.0
准备: 确保hadoop2.2.0集群正常执行 1.eclipse中建立javaproject,导入hadoop2.2.0相关jar包 2.在src根文件夹下拷入log4j.properties,通过 ...
随机推荐
- 浅议APC
0x01 APC中断请求级别 在Intel x86体系结构中,外部硬件中断是通过处理器上的"中断管脚"或者一个称为"本地APIC(local APIC)" ...
- Problem B: 重载函数:max
Description 编写两个名为max的函数,它们是重载函数 ,用于求两个整数或实数的最大值.它们的原型分别是: int max(int a,int b); double max(double a ...
- git相关知识点
git add 和 git stage 有什么区别: 工作区(Working Directory).暂存区(Stage)和历史记录区(History)以及转换关系不能少: git stage 是 gi ...
- 数据库别名AS区别
Oracle之别名小结 MySQL表别名.字段别名注意事项 字段别名:可加 as ,也可以不加,可以加单|双引号,也可以不加: 表别名:可加 as ,也可以不加,但是一定不能加单|双引号! Orac ...
- 『转』VC++ webbrowser函数使用范例
/*============================说明部分================================= 实现一下函数需包含头文件 #include <Winine ...
- css 伪类: 1)a:link , a:visited, a:hover, a:active 2):first-child
1. 链接伪类: <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=&qu ...
- (Object String 类中的方法练习)
package com.zs.demo1; public class Demo1 { public static void main(String[] args) { fun1(); fun2(); ...
- mysql取差集、交集、并集
mysql取差集.交集.并集 博客分类: Mysql数据库 需求:从两个不同的结果集(一个是子集,一个是父集),字段为电话号码phone_number,找出父集中缺少的电话号码,以明确用户身份. 结合 ...
- 鼠标滑过元素,div显示,并根据scrollTop向下移动
如上图所示,通道有很多个,表格只有一个. 注意:滑过通道时鼠标如果停留在上面,那么表格才显示,鼠标滑过表格时,表格不消失 <div id="lineContent"> ...
- JavaBasic_03
变量 变量定义的格式: 数据类型 变量名 = 初始化值; 可以在一行中定义多个变量(在实际开发中不推荐,推荐一行定义一个变量)数据类型 变量名1=初始化值,变量名,...,变量名n // ...