我正在尝试编写简单的 map reduce 程序来使用新的 API(0.20.2)找到最大的素数。这就是我的 Map 和 reduce 类的样子……
public class PrimeNumberMap extends Mapper<LongWritable, Text, IntWritable, IntWritable> {
public void map (LongWritable key, Text Kvalue,Context context) throws IOException,InterruptedException
{
Integer value = new Integer(Kvalue.toString());
if(isNumberPrime(value))
{
context.write(new IntWritable(value), new IntWritable(new Integer(key.toString())));
}
}
boolean isNumberPrime(Integer number)
{
if (number == 1) return false;
if (number == 2) return true;
for (int counter =2; counter<(number/2);counter++)
{
if(number%counter ==0 )
return false;
}
return true;
}
}
public class PrimeNumberReduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {
public void reduce ( IntWritable primeNo, Iterable<IntWritable> Values,Context context) throws IOException ,InterruptedException
{
int maxValue = Integer.MIN_VALUE;
for (IntWritable value : Values)
{
maxValue= Math.max(maxValue, value.get());
}
//output.collect(primeNo, new IntWritable(maxValue));
context.write(primeNo, new IntWritable(maxValue)); }
}
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException{
if (args.length ==0)
{
System.err.println(" Usage:\n\tPrimenumber <input Directory> <output Directory>");
System.exit(-1);
}
Job job = new Job();
job.setJarByClass(Main.class);
job.setJobName("Prime");
// Creating job configuration object
FileInputFormat.addInputPath(job, new Path (args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setMapOutputKeyClass(IntWritable.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(IntWritable.class);
String star ="*********************************************";
System.out.println(star+"\n Prime number computer \n"+star);
System.out.println(" Application started ... keeping fingers crossed :/ ");
System.exit(job.waitForCompletion(true)?0:1);
}
}
我仍然收到有关地图键不匹配的错误
java.io.IOException:映射中的键类型不匹配:预期的 org.apache.hadoop.io.IntWritable,在 org.apache.hadoop.mapred.MapTask$MapOutputBuffer.collect 收到 org.apache.hadoop.io.LongWritable(MapTask .java:1034) 在 org.apache.hadoop.mapreduce.TaskInputOutputContext.write(TaskInputOutputContext.java:80) 在 org.apache.mapTask$NewOutputCollector.write(MapTask.java:595) 在 org.apache。 hadoop.mapreduce.Mapper.map(Mapper.java:124) 在 org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:144) 在 org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java: 668) 在 org.apache.hadoop.mapred.MapTask.run(MapTask.java:334) 在 org.apache.hadoop.mapred.Child$4.run(Child.java:270) 在 java.security.AccessController.doPrivileged(本机方法)在 javax.security.auth.Subject.doAs(Subject.java:396) 在 org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1109) 在 org.apache.hadoop.mapred.Child.main(Child.java:264) 2012-06-13 14:27:21,116 INFO org.apache.hadoop.mapred.Task:为任务运行清理
有人可以建议什么是错的。我已经尝试了所有的钩子和骗子。