Java – Hadoop MapReduce Reducer in Java implementation

Hadoop MapReduce Reducer in Java implementation… here is a solution to the problem.

Hadoop MapReduce Reducer in Java implementation

I’m writing a Java implementation in the Hadoop MapReduce Framework. I’m writing a class called CombinePatternReduce.class. To debug the reducer in Eclipse, I wrote a main() function as follows:

@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException, InterruptedException{
    Text key = new Text("key2:::key1:::_ performs better than _");
    IntWritable count5 = new IntWritable(5);
    IntWritable count3 = new IntWritable(3);
    IntWritable count8 = new IntWritable(8);
    List<IntWritable> values = new ArrayList<IntWritable>();
    values.add(count5);
    values.add(count3);
    values.add(count8);
    CombinePatternReduce reducer = new CombinePatternReduce();
    Context dcontext = new DebugTools.DebugReducerContext<Text, IntWritable, KeyPairWritableComparable, WrapperDoubleOrPatternWithWeightWritable>(reducer, key, count3);  here is the problem
    reducer.reduce(key, values, dcontext);      
}

DebugTools.DebugReducerContext is a class I wrote to facilitate the debugging process, as follows:

public static class DebugReducerContext<KIN, VIN, KOUT, VOUT> extends Reducer<KIN, VIN, KOUT, VOUT>. Context {
    DebugTools dtools = new DebugTools();
    DataOutput out = dtools.new DebugDataOutputStream(System.out);

public DebugReducerContext(Reducer<KIN, VIN, KOUT, VOUT> reducer, Class<KIN> keyClass, Class<VIN> valueClass) throws IOException, InterruptedException{
        reducer.super(new Configuration(), new TaskAttemptID(), new DebugRawKeyValueIterator(), null, null, 
                null, null, null, null, keyClass, valueClass);
    }

@Override
    public void write(Object key, Object value) throws IOException, InterruptedException {
        writeKeyValue(key, value, out);
    }

@Override
    public void setStatus(String status) {
        System.err.println(status);
    }
}

The problem is in the first part of the code, which is main() when I write

Context dcontext = new DebugTools.DebugReducerContext<Text, IntWritable, KeyPairWritableComparable, WrapperDoubleOrPatternWithWeightWritable>(reducer, key, count3);

There is an error

The constructor DebugTools.DebugReducerContext<Text,IntWritable,KeyPairWritableComparable,WrapperDoubleOrPatternWithWeightWritable>( CombinePatternReduce, Text, IntWritable) is undefined.

When I write

Context dcontext = new DebugTools.DebugReducerContext<Text, IntWritable, KeyPairWritableComparable, WrapperDoubleOrPatternWithWeightWritable>(reducer, key, values);

There is an error

The constructor DebugTools.DebugReducerContext<Text,IntWritable,KeyPairWritableComparable,WrapperDoubleOrPatternWithWeightWritable>( CombinePatternReduce, Text, List<IntWritable>) is undefined.

Since the documentation for Reducer.Context

public Reducer.Context(Configuration conf,
                       TaskAttemptID taskid,
                       RawKeyValueIterator input,
                       Counter inputKeyCounter,
                       Counter inputValueCounter,
                       RecordWriter<KEYOUT,VALUEOUT> output,
                       OutputCommitter committer,
                       StatusReporter reporter,
                       RawComparator<KEYIN> comparator,
                       Class<KEYIN> keyClass,
                       Class<VALUEIN> valueClass)
                throws IOException,
                       InterruptedException

I need to pass in a Class<KEYIN> keyClass and Class<VALUEIN> valueClass.

Solution

Obviously, the class constructor has 3 parameters. An instance of a reducer, a class of keys, and a class of values.

Instead of actually passing keys and values. You need to provide it with a class link

Context dcontext = new DebugTools.DebugReducerContext<Text, IntWritable, KeyPairWritableComparable, WrapperDoubleOrPatternWithWeightWritable>(reducer, Text.class, IntWritable.class);

Essentially, this is a type that reaffirms that the context should be able to handle reduced values.

Related Problems and Solutions