我是java/hadoop新手,目前正在尝试重新创建我在此处找到的代码的结果:https://sunilmistri.wordpress.com/2015/02/13/mapreduce-example-for-minimum-and-maximum-value-by-group-key/
与示例不同,我只有3个“列”,每个列都有一个整数,由制表符(/t)分隔,例如100 115 3前两个数字是节点,第三个是节点之间的权重。现在我试着找到第一个节点的最小和最大权重。我在代码中唯一更改的是分隔符(/t),并且我将这两个类放在一个文件中(据我所知,应该可以)。现在我在下面指示的行中得到一个错误“java.lang.object cannot be converted to minmaxduration”。我发现类似的问题,比如不兼容类型:java.lang.object不能转换为t,但这并没有真正帮助我。我怎样才能解决这个问题?提前谢谢。
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;
}
}
1条答案
按热度按时间o0lyfsai1#
方法签名中缺少类型规范。所以它将被认为是一个通用的
Object
而不是你的具体类型。所以将方法签名从至
所以你指定了正确的类型。