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); } } }
mr计算框架实现矩阵乘积
最新推荐文章于 2022-02-28 14:16:19 发布