1

I am new to java/hadoop and am currently trying to recreate the results of a code I found here: https://sunilmistri.wordpress.com/2015/02/13/mapreduce-example-for-minimum-and-maximum-value-by-group-key/

In contrast to the example, I only have 3 "columns", each with an integer number, delimited by a tab (/t), e.g. 100 115 3 The first two numbers are nodes, the third the weight between the nodes. Now I try to find the min and max weights for the first node. The only things I have changed in the code are the delimiter (/t) and that I put both classes in one file (which, what I read, should be ok). Now I get an error "java.lang.Object cannot be converted to minmaxduration" in the line indicated below. I found similar questions like incompatible types : java.lang.Object cannot be converted to T but this didn't really help me. How can I fix this? Thanks in advance.

import java.io.IOException;
import java.io.DataInput;
import java.io.DataOutput;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.io.*;
import org.apache.hadoop.util.*;

import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class Q1{

  public static class DurationMapper
       extends Mapper<Object, Text, Text, MinMaxDuration>{

    private Text month= new Text();
    private Integer minduration;
    private Integer maxduration;


    private MinMaxDuration outPut= new MinMaxDuration();

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

        String[] campaignFields= value.toString().split("/t");
        //10001,'telephone','may','mon',100,1,999,0,'nonexistent','no',0
        month.set(campaignFields[0]);
        minduration=Integer.parseInt(campaignFields[2]);
        maxduration=Integer.parseInt(campaignFields[2]);

        if (month == null || minduration == null || maxduration== null) {
            return;
        }   
        try {
            outPut.setMinDuration(minduration);
            outPut.setMaxDuration(maxduration);
            context.write(month,outPut);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     }
  }

  public static class DurationReducer
       extends Reducer<Text,MinMaxDuration,Text,MinMaxDuration> {
      private MinMaxDuration resultRow = new MinMaxDuration();

     public void reduce(Text key, Iterable values,
                       Context context
                       ) throws IOException, InterruptedException {
      Integer minduration = 0;
      Integer maxduration = 0;

      resultRow.setMinDuration(null);
      resultRow.setMaxDuration(null);

      for (MinMaxDuration val : values) { //ERROR HERE

          minduration = val.getMinDuration();
          maxduration = val.getMaxDuration();
          // get min score 
          if (resultRow.getMinDuration()==null || minduration.compareTo(resultRow.getMinDuration())<0) {
        resultRow.setMinDuration(minduration);
        }            
      // get min bonus                       
      if (resultRow.getMaxDuration()==null || maxduration.compareTo(resultRow.getMaxDuration())>0) {
            resultRow.setMaxDuration(maxduration);
        }
       } // end of for loop
     context.write(key, resultRow);
    }
  }

  public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    Job job = Job.getInstance(conf, "Campaign Duration Count");
    job.setJarByClass(CampaignMinMax.class);
    job.setMapperClass(DurationMapper.class);
    job.setCombinerClass(DurationReducer.class);
    job.setReducerClass(DurationReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(MinMaxDuration.class);
    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}

class MinMaxDuration implements Writable {
    // declare variables
    Integer minDuration;
    Integer maxDuration;
    // constructor
    public MinMaxDuration() {
        minDuration=0;
        maxDuration=0;
    }
    //set method
    void setMinDuration(Integer duration){
        this.minDuration=duration;
    }
    void setMaxDuration(Integer duration){
        this.maxDuration=duration;
    }
    //get method
    Integer getMinDuration() {
        return minDuration;
    }
    Integer getMaxDuration(){
        return maxDuration;
    }

    // write method
            public void write(DataOutput out) throws IOException {
            // what order we want to write !
                out.writeInt(minDuration);
                out.writeInt(maxDuration);
        }

         // readFields Method
        public void readFields(DataInput in) throws IOException {
            minDuration=new Integer(in.readInt());
            maxDuration=new Integer(in.readInt());
        }

        public String toString() {
            return minDuration + "\t" + maxDuration;
        }

  }



5
  • Did you try giving type for the Iterable? like Iterable<MinMaxDuration> ? Commented Oct 16, 2019 at 11:20
  • Hi Kris, thanks for answer. If I do that, I get "java.lang.Object cannot be converted to java.lang.Iterable" Commented Oct 16, 2019 at 12:24
  • Hi kris, thanks so much! Sorry, I misunderstood and put the Iterable<MinMaxDuration> in the Error line. It works with "public void reduce(Text key, Iterable<MinMaxDuration< values. Can I upvote somehow? Commented Oct 16, 2019 at 12:29
  • bummer, my output is empty but the code works... Commented Oct 16, 2019 at 12:34
  • You can accept the answer Commented Oct 17, 2019 at 5:09

1 Answer 1

1

You are missing the type specification in the method signature. So it will be considered to be a generic Object and not your concrete type. So change the method signature from

public void reduce(Text key, Iterable values,Context context) throws IOException, InterruptedException 

to

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

So you specifiying the proper type.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.