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;
}
}