Question

I am a hadoop newbie.

I want to get a partition number on output file.

At first, I made a customized partitioner.


public static class MyPartitioner extends Partitioner<Text, LongWritable> {

    public int getPartition(Text key, LongWritable value, int numReduceTasks) {

    int numOfChars = key.toString().length();
        return numOfChars % numReduceTasks;
    }
}

It works. But, I want to output partition numbers 'visually' on Reducer.

How can I get a partiton number ??

Below is my reducer source.


public static class MyReducer extends Reducer<Text, LongWritable, Text, Text>{

    private Text textList = new Text();

    public void reduce(Text key, Iterable<LongWritable> values, Context context)
      throws IOException, InterruptedException {

        String list = new String();

            for(LongWritable value: values) {
                list = new String(list + "\t" + value.toString());
            }

            textList.set(list);

            context.write(key, textList);

    }

}

I want to put a partition number on 'list' respectively. There will be '0' or '1'.

list = new String(list + "\t" + value.toString() + "\t" + ??);

It would be great if someone helps me.

+

Thanks to the answer, I got a solution. But, It didn't work and I think I did something wrong.

Below is the modified MyPartitioner.


public static class MyPartitioner extends Partitioner {

    public int getPartition(Text key, LongWritable value, int numReduceTasks) {

        int numOfChars = key.toString().length();
        return numOfChars % numReduceTasks;

        private int bring_num = 0;      
        public void configure(JobConf job) {
            bring_num = jobConf.getInt(numOfChars & numReduceTasks);
}

    }

}
Was it helpful?

Solution

Add the below code to the Reducer class to get the partition number in a class variable which can be later used in the reducer method.

String partition;
protected void setup(Context context) throws IOException,
    InterruptedException {
    Configuration conf = context.getConfiguration();
    partition = conf.get("mapred.task.partition");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top