1

我编写了一个代码,用于在 mapreduce 中计算 3x3 矩阵的行列式。

import java.io.*;
import java.util.*;

import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.util.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class Determinant extends Configured implements Tool {

public static int matrix_row=3;
//public static int sum=0;
public static int matrix_col=3;
public static int check_matrix[][]=new int[matrix_row][matrix_col];

public static class PairValue implements WritableComparable {
    public int col;
    public int num;
    public void write (DataOutput out)
        throws IOException
    {
        out.writeInt(col);
        out.writeInt(num);
    }
    public void readFields (DataInput in)
        throws IOException
    {
        col = in.readInt();
        num = in.readInt();
    }
    public int compareTo (Object other) {
        PairValue o = (PairValue)other;
        if (this.col < o.col) {
            return -1;
        } else if (this.col > o.col) {
            return +1;
        }
        if (this.num < o.num) {
            return -1;
        } else if (this.num > o.num) {
            return +1;
        }
        return 0;
    }
    public int hashCode () {
        return col << 16 + num;
    }
}

public static class MapClass extends Mapper<Object,Text,IntWritable,PairValue> {

    private static IntWritable row_num=new IntWritable(0);
    private PairValue pair=new PairValue();

    public void map(Object key,Text value,Context context) throws IOException ,InterruptedException {

        String line=value.toString();
        StringTokenizer token=new StringTokenizer(line);
        row_num.set(Integer.parseInt((token.nextToken()).toString()));
        pair.col=Integer.parseInt((token.nextToken()).toString());
        pair.num=(Integer.parseInt((token.nextToken()).toString()));
        context.write(row_num,pair);
    }

}

public static class ReduceClass extends Reducer<IntWritable,PairValue,IntWritable,IntWritable> {


    private int A[]=new int [matrix_col];
    private int minor(int pr,int pc) throws IOException {

        int result=0;
        int i=0;double l=0.0;int j=0;
        if((matrix_row-pc)==0) return 1;
        for(int r=0;r<matrix_row;r++) {
            int t=check_matrix[r][0];
            if(t!=0) {
                check_matrix[r][0]=0;
                l=Math.pow(-1,i);
                j=(int)l;
                result=result+A[pc] *j *minor(r,pc+1);
                check_matrix[r][0]=1;
                i++;
            }
        }
        return result;
    }

    public void reduce(IntWritable row,Iterable<PairValue> values,Context context) throws IOException, InterruptedException {

        int r=row.get();int d=0;double l=0.0;int j;
        check_matrix[r][0]=0;
        for(PairValue value : values){
            A[value.col]=value.num;
        }
        l=Math.pow(-1,r);
        j=(int)l;
        d=(A[0] *j*  minor(r,1));
        check_matrix[r][0]=1;
        context.write(row, new IntWritable(d));
    }
}

public int run(String args[]) throws Exception {

    Configuration conf = new Configuration();
    Job job = new Job(conf, "Determinant");
    job.setJarByClass(Determinant.class);
    job.setMapperClass(MapClass.class);
    //job.setCombinerClass(ReduceClass.class);
    job.setReducerClass(ReduceClass.class);
    job.setMapOutputKeyClass(IntWritable.class);
    job.setMapOutputValueClass(PairValue.class);
    job.setOutputKeyClass(IntWritable.class);
    job.setOutputValueClass(IntWritable.class);

    List<String> other_args = new ArrayList<String>();
    for(int i=0; i < args.length; ++i) {
        other_args.add(args[i]);
    }
    FileInputFormat.addInputPath(job, new Path(other_args.get(0)));
    FileOutputFormat.setOutputPath(job, new Path(other_args.get(1)));

    job.waitForCompletion(true);
    return 0;
}

public static void main(String args[]) throws Exception {
    for(int i=0;i<matrix_row;i++)
        for(int j=0;j<matrix_col;j++)
            check_matrix[i][j]=1;
    int res = ToolRunner.run(new Configuration(), new Determinant(), args);
    //System.out.println(sum);
    System.exit(res);    
}

}

reducer 类的输出为每个行元素提供了次要。添加这些未成年人将给出行列式。因此我添加了一个全局变量sum来获取它。但是变量sum总是返回 0。reducer 的输出也包含零。次要函数的变量“结果”没有得到更新。请看一下

请帮忙。我按照此链接编写代码。

4

0 回答 0