2014-11-03 121 views
1

我期待的輸出是輸入文件中每個單詞的計數。但我的輸出是整個輸入文件,就像它一樣。 我對映射程序類使用extends Mapper<LongWritable, Text, Text, IntWritable>,對於reducer類使用Reducer<Text, IntWritable, Text, IntWritable>。 這裏是我的代碼MapReduce WordCount程序 - 輸出與輸入文件相同

driver.java

public class driver extends Configured implements Tool{ 
 
     
 
    public int run(String[] args) throws Exception 
 
     { 
 
     Configuration conf = new Configuration(); 
 
       Job job = new Job(conf, "wordcount"); 
 
       
 
       job.setMapperClass(mapper.class); 
 
       job.setReducerClass(reducer.class); 
 
       
 
       job.setOutputKeyClass(Text.class); 
 
       job.setOutputValueClass(Text.class); 
 
       job.setInputFormatClass(KeyValueTextInputFormat.class); 
 
       
 
       FileInputFormat.addInputPath(job, new Path(args[0])); 
 
       FileOutputFormat.setOutputPath(job, new Path(args[1])); 
 
       
 
       job.waitForCompletion(true); 
 
       //JobClient.runJob((JobConf) conf); 
 
       //System.exit(job.waitForCompletion(true) ? 0 : 1); 
 
      return 0; 
 
     } 
 
     
 
     public static void main(String[] args) throws Exception 
 
     { 
 
      long start = System.currentTimeMillis(); 
 
      //int res = ToolRunner.run(new Configuration(), new driver(),args); 
 
      
 
      int res = ToolRunner.run(new Configuration(), new driver(),args); 
 
      
 
      long stop = System.currentTimeMillis(); 
 
      System.out.println ("Time: " + (stop-start)); 
 
      System.exit(res); 
 
     } 
 
}

mapper.java

public class mapper extends Mapper<LongWritable, Text, Text, IntWritable> 
 
{ 
 
     //hadoop supported data types 
 
     private final static IntWritable one = new IntWritable(1); 
 
     private Text word = new Text(); 
 
     
 
     //map method that performs the tokenizer job and framing the initial key value pairs 
 
     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); 
 
      } 
 
     } 
 
}

熱度cer.java
public class reducer extends Reducer<Text, IntWritable, Text, IntWritable> 
 
{ 
 
     //reduce method accepts the Key Value pairs from mappers, do the aggregation based on keys and produce the final out put 
 
     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)); 
 
     } 
 
}

+0

您使用的是哪個版本的hadoop?你導入了什麼Mapper和Reducer? – vefthym 2014-11-03 10:16:41

回答

0

請,如果你與你的代碼。此代碼面臨的問題運行該代碼包含映射器,減速器和主要功能。

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.mapred.*; 
import org.apache.hadoop.util.*; 

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)); 
    } 
} 

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(args[1])); 

    JobClient.runJob(conf); 
    } 
} 

2)之後,創建這個代碼說wordcount.jar保存在你的home目錄(/home/user/wordcount.jar一罐),並運行以下命令:

hadoop jar wordcount.jar classname /inputfile /outputfile/

這將在hadoop的/(root)目錄下創建一個文件輸出文件。查看結果爲

hadoop dfs -cat /outputfile/part-m-00000 

這將成功運行您的wordcount程序。

+0

謝謝我熟悉擴展MapReduceBase類的代碼。但我希望我的映射器類和reducer類擴展** Mapper類**和** Reducer類** insead。 – kishorer747 2014-11-03 09:56:48

0

試試這個,

import java.io.IOException; 
import java.util.Iterator; 
import java.util.StringTokenizer; 

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.mapred.FileInputFormat; 
import org.apache.hadoop.mapred.FileOutputFormat; 
import org.apache.hadoop.mapred.JobClient; 
import org.apache.hadoop.mapred.JobConf; 
import org.apache.hadoop.mapred.MapReduceBase; 
import org.apache.hadoop.mapred.Mapper; 
import org.apache.hadoop.mapred.OutputCollector; 
import org.apache.hadoop.mapred.Reducer; 
import org.apache.hadoop.mapred.Reporter; 
import org.apache.hadoop.mapred.TextInputFormat; 
import org.apache.hadoop.mapred.TextOutputFormat; 


public class WordCount { 

    public static class Map extends MapReduceBase implements 
      Mapper<LongWritable, Text, Text, IntWritable> { 

     @Override 
     public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) 
       throws IOException { 

      String line = value.toString(); 
      StringTokenizer tokenizer = new StringTokenizer(line); 
      System.out.println(line); 
      while (tokenizer.hasMoreTokens()) { 
       value.set(tokenizer.nextToken()); 
       output.collect(value, new IntWritable(1)); 
      } 

     } 
    } 

    public static class Reduce extends MapReduceBase implements 
      Reducer<Text, IntWritable, Text, IntWritable> { 

     @Override 
     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)); 
     } 
    } 

    public static void main(String[] args) throws Exception,IOException { 

     JobConf conf = new JobConf(WordCount.class); 
     conf.setJobName("WordCount"); 

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

     conf.setMapperClass(Map.class); 
     conf.setReducerClass(Reduce.class); 

     conf.setInputFormat(TextInputFormat.class); 
     conf.setOutputFormat(TextOutputFormat.class); 

     FileInputFormat.setInputPaths(conf, new Path("/home/user17/test.txt")); 
     FileOutputFormat.setOutputPath(conf, new Path("hdfs://localhost:9000/out2")); 

     JobClient.runJob(conf); 

    } 
} 

化妝罐子,並執行給定的關於命令

hadoop jar WordCount.jar WordCount /inputfile /outputfile 
1

您是通過MapReduce的新老&困惑的API命令。我認爲你試圖在新的API中編寫WordCount程序,但是從舊的API(舊的博客文章也許)中摘錄了一些片段。您可以自己找到問題,如果您只是將@override註釋同時添加到地圖&縮減方法中。

見進化後會發生什麼對他們說:

你只寫了兩個新的方法,指定舊的簽名,所以他們只是不覆蓋任何東西,無處是調用。代碼沒有做任何事情,因爲被調用的實際方法有空體(我認爲沒有默認實現,並且如果只有身份操作)。

無論如何,你應該遵循編碼的基本約定。

+0

感謝您的建議,將嘗試一下。 – kishorer747 2014-11-03 13:19:45

+0

+1我同意。新API中的默認映射器和縮減器實際上分別是IdentityMapper和IdentityReducer,這就是爲什麼輸入在輸出中被複制的原因。 – vefthym 2014-11-03 16:40:02

+0

我不知道@vefthym,OP如何賺取upvotes這樣一個愚蠢的錯誤。 Hadoop應該不贊成使用碰撞類,它會讓人困惑不已。現在這些東西已經滿溢了。 – blackSmith 2014-11-04 04:48:04