Java MapReduce Hadoop字长频率不工作
关于这一页,我和他有一个类似的问题。我需要提供一个映射和减少方法来计算字长(1到n)频率。我已经尝试了答案的方法来实现这一点Java MapReduce Hadoop字长频率不工作,java,hadoop,Java,Hadoop,关于这一页,我和他有一个类似的问题。我需要提供一个映射和减少方法来计算字长(1到n)频率。我已经尝试了答案的方法来实现这一点 import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; impo
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
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 WordCount {
//Mapper which implement the mapper() function
public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
//public static class TokenizerMapper extends Mapper<LongWritable, Text, IntWritable, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
//check whether word is start from a or b
String wordToCheck = itr.nextToken();
word.set(String.valueOf(wordToCheck.length()));
context.write(word, one);
//if (wordToCheck.startsWith("a")||wordToCheck.startsWith("b")){
// word.set(wordToCheck);
// context.write(word, one);
//}
//check for word length
//if (wordToCheck.length() > 8) {
// }
}
}
}
//Reducer which implement the reduce() function
public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
//Driver class to specific the Mapper and Reducer
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
我在EclipseKepler中开发了这个类,并在UbuntuLTXTerminal中使用Hadoop2.6.3将这个类作为jar文件运行。有什么问题?我也试着按照答案中的建议使用IntWritable,但是,它也有类似的反应。我不是100%确定,但当您使用文件作为输入时,mapper应该为键(对应于文件中的行号)键入
LongWritable
,为值键入Text
(文件行为文本)
因此,可能的解决办法是更换
public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
公共静态类TokenizerMapper扩展映射器{
与
公共静态类TokenizerMapper扩展映射器{
问题回答得很好。非常感谢。
public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
public static class TokenizerMapper extends Mapper<LongWritable, Text, Text, IntWritable> {