mr计算框架实现矩阵乘积

package chengji;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
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.output.FileOutputFormat;

import java.io.IOException;

/**
 * Created by lenovo on 2018/12/26.
 */
public class Martrixs {
    public static final int WIDTH = 5;
    public static final int LENGTH = 1;
    public static final int MATRIX_K = 5;

    public static class MatrixMapper extends Mapper<Object, Text, Text, Text> {
        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            String[] values = value.toString().split(" ");
            if (values[0].equals("M")) {
                for (int i = 1; i <= LENGTH; i++) { //把矩阵每个元素转换为 i,k,M,j,v 形式
                    context.write(new Text(values[1]+" "+i), new Text(values[0]+" "+values[2]+" "+values[3]));
                }
            } else {
                for(int i=1;i<=WIDTH;i++){
                    //把矩阵每个元素转换为 i,k,M,j,v 形式
                    context.write(new Text(i+" "+values[2]), new Text(values[0]+" "+values[1]+" "+values[3]));
                }
            }
        }
    }

    public static class MatrixReducer extends Reducer<Text, Text, Text, Text> {
        public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            //对获得的五元组进行处理
            Integer[] m_matrix = new Integer[MATRIX_K];
            Integer[] n_matrix = new Integer[MATRIX_K];
            int i = 0;
            for (Text value : values) {
                String temp[] = value.toString().split(" ");
                if (temp[0].equals("M")) {//如果是 M 矩阵
                    m_matrix[Integer.parseInt(temp[1])] = Integer.parseInt(temp[2]
                    );
                } else {
                    n_matrix[Integer.parseInt(temp[1])] = Integer.parseInt(temp[2]
                    );
                }
            }
            //对两个矩阵进行相乘相加
            int result = 0;
            for (i = 0; i < MATRIX_K; i++) {
                if (m_matrix[i] != null && n_matrix[i] != null) {
                    result += m_matrix[i] * n_matrix[i];
                }
            }
            System.out.println(key + "+++++++" + result);
            context.write(key, new
                    Text(result + "")
            );
        }
    }

    public static class Runs{
        public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
            long a = System.currentTimeMillis();
            Configuration conf = new Configuration();
            //conf.set("mapreduce.framework.name","local");
            //定义一个主类,描述成一个job
            Job job = Job.getInstance(conf );

            //指定job所在的jar包
            job.setJarByClass(Runs.class);

            //指定mapper所在的类
            job.setMapperClass(MatrixMapper.class);
            //设置reduce所在的类
            job.setReducerClass(MatrixReducer.class);

            //设置业务逻辑mapper输出的kv类型
            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(Text.class);

            //设置业务逻辑reduce 输出的kv类型
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(Text.class);
            //指定分片个数
            //job.setNumReduceTasks(6);
            //指定自定义组件
            //   job.setPartitionerClass(provincePartitioner.class);
            //指定要处理数据所在的位置
            FileInputFormat.setInputPaths(job, "/wordcount/input/1.txt");
            //指定处理后的数据所保存的位置
            FileOutputFormat.setOutputPath(job, new Path("/wordcount/outputss"));

            //向yarn提交job
            boolean flag = job.waitForCompletion(true);
            long b = System.currentTimeMillis();
            System.out.println(b-a);
            System.exit(flag? 0:1);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值