MapReduce: program of Matrix Multiplication

1.Matrix Multiplication 's basic principle:

(1) Sum the result of Matrix A's row multiplying Matrix B's column as the new Matrix.
(2)The Matrix A's row subscript become the new Matrix 's row subscript and The Matrix B's column subscript become the new Matrix 's column.

2.When we code , How do we think deeply the basic principle:

(1)The factor 's row subscript in Matrix A must be equal to the factor's column subscript in Matrix B.
(2)Matrix A 's all row factor must be multiply with Matrix B 's all column factor.

(3)In MapReduce Parallel programming framework, the same key value will be shuffle in a same list passing to Reduce.

4In Reduce, Sum the result of Matrix A's row multiplying Matrix B's column as the new Matrix, and  write the sum to output.

so, we need to design the arithmetic so as to achieve MatrixMultiplication.

(1)First ,we need to construt the key in map in order to shuffle Matrix A's row factor and Matrix B's columnfactor, which participate in the multiplication, in a same  reduce input value list.

(2)In the value list, we need another factor of the value in map to control suming the Multiplication 's result.

3.Design the arithmetic:

Pminj  = (A * B)minj = sum(Ami mj * Bmj nj);

In the Map,the Key and the Value be designed as this:

Matrix A:

key:[mi,  nj]

value:[A,  mj,  Ami mj ]

Matrix B:

key:[mi,  nj]

value:[A,  mj,  Ami mj ]

4.The Program :

package com.catchingsun.matrix;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

public class MatrixMultiply {

    private static final int columnN = 3;
    private static final int rowM = 5;
    private static final int columnM = 6;

    public static class MatrixMap extends Mapper<Object, Text, Text, Text > {
        private Text map_key = new Text();
        private Text map_value = new Text();

        private static int mmi = 0;
        private static int ni = 0;

        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            FileSplit fileSplit = (FileSplit) context.getInputSplit();
            String fileName = fileSplit.getPath().getName();
            int mj = 0;
            if (fileName.contains("M")) {
                mmi++;
                String[] tuple = value.toString().split(",");
                for (String s : tuple) {
                    mj++;
                    for (int k = 1; k < columnN + 1; k++) {
                        map_key.set(mmi + "," + k);
                        map_value.set("M" + "," + mj + "," + s);
                        context.write(map_key, map_value);
                    }
                }
            } else if (fileName.contains("N")) {
                ni++;
                int nj = 0;
                String[] tuple = value.toString().split(",");
                for (String s : tuple) {
                    nj++;
                    for (int i = 1; i < rowM + 1; i++) {
                        Text str = new Text();
                        map_key.set(i + "," + nj);
                        map_value.set("N" + "," + ni + "," + s);
                        context.write(map_key, map_value);
                    }
                }
            }
        }
    }

    public static class MatrixReduce extends Reducer<Text, Text, Text, Text> {

        private int sum = 0;
        private int M[] = new int[columnM + 1];
        private int N[] = new int[columnM + 1];

        public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException{
            for(Text val : values){
                String [] tuple = val.toString().split(",");
                if(tuple[0].equals("M")){
                    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(Integer.toString(sum)));
            sum = 0;
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();

        Job job = new Job(conf, "MatrixMultiply");
        job.setJarByClass(MatrixMultiply.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        job.setMapperClass(MatrixMultiply.MatrixMap.class);
        job.setReducerClass(MatrixMultiply.MatrixReduce.class);

        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);

        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        job.waitForCompletion(true);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值