我需要运行wordcount,这将给我所有的单词和他们的出现,但排序的出现,而不是按字母表
我知道我需要为此创建两个作业,然后一个接一个地运行,我使用hadoopmapreduce使用mapper和reducer从排序的字数中进行排序
package org.myorg;
import java.io.IOException;
import java.util.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.mapreduce.Job;
public class WordCount {
public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
output.collect(word, one);
}
}
}
public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
int sum = 0;
while (values.hasNext()) {
sum += values.next().get();
}
output.collect(key, new IntWritable(sum));
}
}
class Map1 extends MapReduceBase implements Mapper<Object, Text, IntWritable, Text> {
public void map(Object key, Text value, OutputCollector<IntWritable, Text> collector, Reporter arg3) throws IOException {
String line = value.toString();
StringTokenizer stringTokenizer = new StringTokenizer(line);
{
int number = 999;
String word = "empty";
if (stringTokenizer.hasMoreTokens()) {
String str0 = stringTokenizer.nextToken();
word = str0.trim();
}
if (stringTokenizer.hasMoreElements()) {
String str1 = stringTokenizer.nextToken();
number = Integer.parseInt(str1.trim());
}
collector.collect(new IntWritable(number), new Text(word));
}
}
}
class Reduce1 extends MapReduceBase implements Reducer<IntWritable, Text, IntWritable, Text> {
public void reduce(IntWritable key, Iterator<Text> values, OutputCollector<IntWritable, Text> arg2, Reporter arg3) throws IOException {
while ((values.hasNext())) {
arg2.collect(key, values.next());
}
}
}
public static void main(String[] args) throws Exception {
JobConf conf = new JobConf(WordCount.class);
conf.setJobName("wordCount");
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);
conf.setMapperClass(Map.class);
conf.setCombinerClass(Reduce.class);
conf.setReducerClass(Reduce.class);
conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);
FileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path("/tmp/temp"));
//JobClient.runJob(conf);
//------------------------------------------------------------------
JobConf conf2 = new JobConf(WordCount.class);
conf2.setJobName("WordCount1");
conf2.setOutputKeyClass(Text.class);
conf2.setOutputValueClass(IntWritable.class);
conf2.setMapperClass(Map1.class);
conf2.setCombinerClass(Reduce1.class);
conf2.setReducerClass(Reduce1.class);
conf2.setInputFormat(TextInputFormat.class);
conf2.setOutputFormat(TextOutputFormat.class);
FileInputFormat.setInputPaths(conf2, new Path("/tmp/temp/part-00000"));
FileOutputFormat.setOutputPath(conf2, new Path(args[1]));
Job job1 = new Job(conf);
Job job2 = new Job(conf2);
job1.submit();
if (job1.waitForCompletion(true)) {
job2.submit();
job1.waitForCompletion(true);
}
}
}
它不工作了,我应该在这里改变什么,或者为什么它不工作???
1条答案
按热度按时间nbysray51#
如果程序运行到:
那么问题就出在你的最后一行:
作业已提交,但未排队等待处理。试试这个:
来处理你的分拣机工作。我已经用mr的新api尝试了你的代码,流程正常。
加上最后一行。