简体   繁体   English

Hadoop MapReduce不写输出

[英]Hadoop MapReduce does not write output

I created a file and added some numbers like 10, 20, 220 and 228. I want to read this file inside my mapper function like below and check if a number is Amicable. 我创建了一个文件,并添加了一些数字(例如10、20、220和228)。我想在我的映射器函数中读取该文件,如下所示,并检查数字是否为Amicable。 But after compiling the class file and building the jar there's nothing inside the output file. 但是在编译了类文件并构建了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);
    }
}

Since you are not setting numReduceTask to "0", so it will go to the Reducer and try to run the reduce task. 由于您没有将numReduceTask设置为“ 0”,因此它将转到Reducer并尝试运行reduce任务。

So if you want to run map-only job, set numReduceTask to "0". 因此,如果要运行仅地图作业,请将numReduceTask设置为“ 0”。 You don't need to set ReducerClass. 您无需设置ReducerClass。 Use following in driver class. 在驱动程序类中使用以下命令。

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