簡體   English   中英

Hadoop MapReduce不寫輸出

[英]Hadoop MapReduce does not write output

我創建了一個文件,並添加了一些數字(例如10、20、220和228)。我想在我的映射器函數中讀取該文件,如下所示,並檢查數字是否為Amicable。 但是在編譯了類文件並構建了jar之后,輸出文件內部什么都沒有了。

public class FriendlyNumbers {

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "befriended numbers");
        job.setJarByClass(FriendlyNumbers.class);
        job.setMapperClass(FriendlyNumberMapper.class);
//      job.setCombinerClass(IntSumReducer.class);
        job.setReducerClass(FriendlyNumberKeywordReducer.class);

        job.setMapOutputKeyClass(IntWritable.class);
        job.setMapOutputValueClass(NumberCouple.class);
        job.setOutputKeyClass(IntWritable.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

class FriendlyNumberMapper extends Mapper<Object, Text, IntWritable, NumberCouple> {

    // process all the input data
    // the data come's from the file file0

    private IntWritable number = new IntWritable(); // number from file
    private IntWritable sum = new IntWritable(); // number from calculateSum()
    private NumberCouple numberCouple = new NumberCouple();

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

        StringTokenizer numberTokens = new StringTokenizer(value.toString());

        // loop trough all given numbers
        while (numberTokens.hasMoreTokens()) {

            int parsedNumberToken = Integer.parseInt(numberTokens.nextToken());
            int calculatedSum = calculateSum(parsedNumberToken);

            // set stuff
            number.set(parsedNumberToken);
            sum.set(calculatedSum);
            numberCouple.set(number, sum);

            context.write(sum, numberCouple);

            if (number.get() != sum.get()) {
                context.write(number, numberCouple);
            }
        }
    }

    // the actual sum to check if a number is amicable
    public int calculateSum(int number) {
        int sum = 0;

        for (int i = 1; i <= number / 2; i++) {
            if (number % i == 0) {
                sum += i;
            }
        }
        return sum;
    }
}

class FriendlyNumberKeywordReducer extends Reducer<IntWritable, NumberCouple, IntWritable, IntWritable> {

    // combine data 
    // in this case: get only the befriended numbers and remove others

    public void reduce(IntWritable key, Iterable<NumberCouple> values, Context context) throws IOException, InterruptedException {
        //
    }   
}

class NumberCouple implements WritableComparable<NumberCouple> {

    private IntWritable number;
    private IntWritable sum;

    public NumberCouple() {
        set(new IntWritable(), new IntWritable());
    }

    public NumberCouple(NumberCouple couple) {
        set(new IntWritable(couple.number.get()), new IntWritable(couple.sum.get()));
    }

    public NumberCouple(int number, int sum) {
        set(new IntWritable(number), new IntWritable(sum));
    }

    public void set(IntWritable number, IntWritable sum) {
        this.number = number;
        this.sum = sum;
    }

    public IntWritable getNumber() {
        return this.number;
    }

    public IntWritable getSum() {
        return this.sum;
    }

    @Override
    public void write(DataOutput out) throws IOException {
        number.write(out);
        sum.write(out);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        number.readFields(in);
        sum.readFields(in);
    }

    @Override
    public int compareTo(NumberCouple o) {
         return number.compareTo(o.number);
    }
}

由於您沒有將numReduceTask設置為“ 0”,因此它將轉到Reducer並嘗試運行reduce任務。

因此,如果要運行僅地圖作業,請將numReduceTask設置為“ 0”。 您無需設置ReducerClass。 在驅動程序類中使用以下命令。

Job job = Job.getInstance(conf, "befriended numbers");

// Set this property to Zero to run map-only job 
job.setNumReduceTasks(0);

job.setJarByClass(FriendlyNumbers.class);
job.setMapperClass(FriendlyNumberMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setMapOutputKeyClass(IntWritable.class);
job.setMapOutputValueClass(NumberCouple.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(IntWritable.class);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM