【甘道夫】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,通过 ...
随机推荐
- 七. Python基础(7)--文件的读写
七. Python基础(7)--文件的读写 1 ● 文件读取的知识补充 f = open('file', encoding = 'utf-8') content1 = f.read() content ...
- Interlocked单向链式栈
线程同步一大部分与原子访问(atomic access)有关, 所谓原子访问, 指的是一个线程在访问某个资源的同时能够保证没有其他线程会在同一时刻访问同一资源. Interlocked单向链式栈的操作 ...
- CentOS 查看文件大小 du -hs filename
du -hs [filename] 查看目录大小 [root@localhost opt]# 16M apache-tomcat- df -hv 查看整个磁盘使用状况 [root@rabbit66 ...
- tf 模型保存
tf用 tf.train.Saver类来实现神经网络模型的保存和读取.无论保存还是读取,都首先要创建saver对象. 用saver对象的save方法保存模型 保存的是所有变量 save( sess, ...
- Linux 虚拟内存机制
每个进程都有自己独立的4G内存空间,各个进程的内存空间具有类似的结构. Linux内存管理采用的是页式管理,使用的是多级页表,动态地址转换机构与主存.辅存共同实现虚拟内存 一个新进程建立的时候,将会建 ...
- L310
Facelift( 紧肤术) followed by a week on a beach in Thailand? Hip surgery with a side of shopping inSing ...
- ubuntu下pyspark的安装
1.安装jkd1.8(这里不再描述) 2.直接在终端输入pip install pyspark(官网提供的最简单的一种安装方式) 过程如下: Collecting pyspark Downloadin ...
- day 关于生成器的函数
def average(): count=0 toatal=0 average=0 while True: value=yield average toatal+=value count+=1 ave ...
- 32位linux(ubuntu) exec: arm-none-linux-gnueabi-g++未找到;The tslib functionality test failed!
请先参考:http://blog.csdn.net/ankwyq/article/details/7768809 通过上面那篇文章,我确实把问题又推进了一步,接下来就是下面这个问题: exec: ar ...
- 启动和停止mysql的正确姿势
1.如果是用脚本起的.那就用脚本停 2.最好用mysql_safe起,mysqladmin -uroot -p shutdown -S /tmp/mysql.sock停 mysqld_safe --d ...