2012-03-14 2 views
1

최소 10 번 이상 발생하는 단어를 출력하도록 아래 코드를 수정했습니다. 하지만 작동하지 않습니다. 출력 파일이 전혀 변경되지 않습니다. 작동 시키려면 어떻게해야합니까?Hadoop 단어 수에서 예기치 않은 결과가 발생했습니다.

import java.io.IOException; 
import java.util.*; 

import org.apache.hadoop.fs.Path; 
import org.apache.hadoop.conf.*; 
import org.apache.hadoop.io.*; 
import org.apache.hadoop.mapreduce.*; 
import org.apache.hadoop.mapreduce.lib.input.*; 
import org.apache.hadoop.mapreduce.lib.output.*; 
import org.apache.hadoop.util.*; 
// ... 
public class WordCount extends Configured implements Tool { 
// ... 
public static class Map extends 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, Context context) 
      throws IOException, InterruptedException { 
     String line = value.toString(); 
     StringTokenizer tokenizer = new StringTokenizer(line); 
     while (tokenizer.hasMoreTokens()) { 
      word.set(tokenizer.nextToken()); 
      context.write(word, one); 
     } 
    } 
} 

public static class Reduce extends 
     Reducer<Text, IntWritable, Text, IntWritable> { 
    public void reduce(Text key, Iterable<IntWritable> values, 
      Context context) throws IOException, InterruptedException { 

     int sum = 0; 
     for (IntWritable val : values) { 
      sum += val.get(); 
     } 
        // where I modified, but not working, the output file didnt change 
     if(sum >= 10) 
     { 
      context.write(key, new IntWritable(sum)); 
     } 
    } 
} 

public int run(String[] args) throws Exception { 
    Job job = new Job(getConf()); 
    job.setJarByClass(WordCount.class); 
    job.setJobName("wordcount"); 

    job.setOutputKeyClass(Text.class); 
    job.setOutputValueClass(IntWritable.class); 

    job.setMapperClass(Map.class); 
    //job.setCombinerClass(Reduce.class); 
    job.setReducerClass(Reduce.class); 

    job.setInputFormatClass(TextInputFormat.class); 
    job.setOutputFormatClass(TextOutputFormat.class); 

    FileInputFormat.setInputPaths(job, new Path(args[0])); 
    FileOutputFormat.setOutputPath(job, new Path(args[1])); 

    boolean success = job.waitForCompletion(true); 
    return success ? 0 : 1; 
} 

public static void main(String[] args) throws Exception { 
    int ret = ToolRunner.run(new WordCount(), args); 
    System.exit(ret); 
} 
} 

답변

1

코드가 완전히 유효 보인다. 나는 당신의 데이터 세트가 충분히 크다고 의심 할 수있다. 그래서 단어들은 10 번 이상 나타난다. 새로운 결과가 실제로 나오는지 확인하십시오.

0

기본 Hadoop 카운터를보고 무슨 일이 일어나는지 알 수 있습니다.

+0

같은 파일을 먹일 경우 실제 출력을 게시 할 수 있다면 그것은 또한 도움이 될 것이다, 입력 그룹을 줄일 수는 출력 기록을 줄일 수와 동일한 경우, 모든 출력 결과가> = 10 인 경우에는 @David가 남긴 주석을 지원합니다 –

0

코드가 맞습니다. 아마 코드를 수정하기 전에 생성 된 출력을 읽고있을 것입니다. 또는 코드를 수정 한 후 이전에 사용한 jar 파일을 업데이트하지 않았습니까?

0

코드가 유효합니다. 적어도 이것을 실행하는 데 사용했던 명령 줄이 필요합니다. 당신이 그것을 특히이

one 
two two 
three three three 

기타 20까지