1 描述
假定有一个 n*n 的矩阵 M ,其第 i 行第 j 列的元素记为 。假定有一个 n 维向量 v ,其第 j 个元素记为 。于是,矩阵 M 和向量 v 的乘积结果是一个 n 维向量 x,其第 i 个元素 为
如:
要求输入:
11 22 33 33 44 55 66 77 88
输出:
0 220 1 418 2 715
2 实现思路
假如这里 n 很大,但还没有大到向量 v 不足以放入内存的地步。将矩阵 M 存放在一个文件中,向量 v 作为常量数组放在程序中。那么我们便可以从矩阵元素在文件中的位置确定该元素的行列下标。同样, v 向量的元素 ,可以通过数组下标获取该元素的行列下标。
Map 函数:
对矩阵元素 , Map 任务会产生键值对( i, )。因此,计算 的所有 n 个求和项 的键值都相同。
Reduce 函数:
Reduce 任务将所有与给定键 i 关联的值相加即可得到( i , )。
逻辑图:
3 代码实现
public class MatrixVectorCompute {
public static class TokenizerMapper extends
Mapper<Object, Text, Text, IntWritable> {
private Text lineNumber = new Text(); // 矩阵行序号
private static int i = 0;
private final static int[] vector = {2, 3, 4}; // 向量值
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
int j = 0; // 向量序号
lineNumber.set(i + "");
while (itr.hasMoreTokens()) {
int result = vector[j] * Integer.parseInt(itr.nextToken());
IntWritable one = new IntWritable(result);
context.write(lineNumber, one);
j ++;
}
i ++;
}
}
public static class IntSumReducer extends
Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = new Job(conf, "word count11");
job.setJarByClass(MatrixVectorCompute.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path("input"));
FileOutputFormat.setOutputPath(job, new Path("output"));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}