package com.delhi;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class UppercaseDriver extends Configured implements Tool {
public int run(String[] args) throws Exception{
if(args.length !=2){
System.out.printf("Two parameters are required- <input dir> <output dir>n");
return -1;}
Configuration conf = new Configuration();
Job job=Job.getInstance(conf);
job.setJobName("uppercase");
job.setJarByClass(UppercaseDriver.class);
job.setMapperClass(UpperCaseMapper.class);
job.setReducerClass(UpperCaseReduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
FileInputFormat.setInputPaths(job,new Path(args[0]));
FileOutputFormat.setOutputPath(job,new Path(args[1]));
//job.setNumReduceTasks(1);
boolean success = job.waitForCompletion(true);
return success ?0:1;
}
public static void main(String[] args) throws Exception {
int exitcode = ToolRunner.run(new UppercaseDriver(), args);
System.exit(exitcode);
}
}
这是驱动程序。
接下来是减速机程序:
package com.delhi;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class UpperCaseReduce extends Reducer< Text,LongWritable, Text, LongWritable>{
public void reduce(Text key, Iterable<LongWritable> value,
org.apache.hadoop.mapreduce.Reducer.Context context)
throws IOException, InterruptedException {
int sum=0;
System.out.println("how +++++++++++++++++" + key);
for(LongWritable st: value){
sum = (int) (sum + st.get());
}
System.out.println("how +++++++++++++++++" + key);
context.write(key, new LongWritable(sum));
}
}
接下来是mapper程序:
package com.delhi;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class UpperCaseMapper extends Mapper<Object, Text, Text, LongWritable>{
@Override
protected void map(Object key, Text value,
org.apache.hadoop.mapreduce.Mapper.Context context)
throws IOException, InterruptedException {
String line = value.toString();
String arr[] = line.split(" ");
System.out.println("hello++++++++++++++++++++++++++++");
for(String st: arr){
//context.write(new Text(st.toUpperCase().trim()),new LongWritable(1));
context.write(new Text(st),new LongWritable(1));
}
}
}
从已经存在的解决方案中,我发现在这种类型的问题中outputkeyclass和outputvalueclass应该与reducer匹配。我认为我正确地处理了这部分。我的case@override for reduce不起作用。我使用的是hadoop 7.2.3。我也尝试使用trim函数。问题是wordcount没有发生。我只给我“word1”,对于输出文件中的任何单词。我从不同的问题开始,然后就这样结束了。请帮帮我。谢谢。
1条答案
按热度按时间0aydgbwb1#
所以如果你加上
@Override
对您的reduce
方法出现错误:方法不重写其超类中的方法
所以您遇到了一个问题,方法签名与中的不匹配
Reducer
.您有:
如果更改为:
错误消失了。自从你的
reduce
方法没有重写它不会被调用的任何方法,它将使用与您的输出匹配的identity reduce。