简体   繁体   中英

Hadoop jar command error for multiple mapper inputs and 1 reducer output (Join 2 values from 2 files)

Here is my sample program joining 2 datasets. The program has 2 mappers and 1 reducer joining the values obtained from 2 different mappers having 2 different files as input.

I am getting an error in the hadoop jar command.

command:

hadoop jar /home/rahul/Downloads/testjars/datajoin.jar DataJoin /user/rahul/cust.txt /user/rahul/delivery.txt /user/rahul/output

Error: Invalid number of arguments Datajoin

It is actually expecting only 1 input path and 1 output path whereas in my command I have 2 inputs for 2 different mappers and 1 output.

Can anyone help me out ?

Code:

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class DataJoin {

    public static class TokenizerMapper1 extends Mapper {

        private Text word = new Text();

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

            String itr[] = value.toString().split("::");
            word.set(itr[0].trim());
            context.write(word, new Text("CD~" + itr[1]));
        }
    }

    public static class TokenizerMapper2 extends Mapper {

        private Text word = new Text();

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

            String itr[] = value.toString().split("::");
            word.set(itr[0].trim());
            context.write(word, new Text("DD~" + itr[1]));
        }
    }

    public static class IntSumReducer extends Reducer {
        private Text result = new Text();

        public void reduce(Text key, Iterable values, Context context)
                throws IOException, InterruptedException {
            String sum = "";
            for (Text val : values) {
                sum += val.toString();
            }
            result.set(sum);
            context.write(key, result);
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        String[] otherArgs = new GenericOptionsParser(conf, args)
                .getRemainingArgs();
        if (otherArgs.length != 2) {
            System.err.println("Usage: DataJoin ");
            System.exit(2);
        }
        Job job = new Job(conf, "Data Join");
        job.setJarByClass(DataJoin.class);
        job.setReducerClass(IntSumReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        MultipleInputs.addInputPath(job, new Path(otherArgs[0]),
                TextInputFormat.class, TokenizerMapper1.class);
        MultipleInputs.addInputPath(job, new Path(otherArgs[1]),
                TextInputFormat.class, TokenizerMapper2.class);
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[2]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

You have error in this portion

if (otherArgs.length != 2) {
   System.err.println("Usage: DataJoin ");
   System.exit(2);
}

Your argument is of length 3. 2 inputs and 1 output .

Argument count starts from 1,2... not from 0,1....

Change to

if (otherArgs.length != 3) {
   System.err.println("Usage: DataJoin ");
   System.exit(0);
}

This solves your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM