2012-05-21 67 views
0
public static class MapClass extends MapReduceBase implements 
     Mapper<LongWritable, Text, Text, IntWritable> { 

    private Text word = new Text(); 

    public void map(LongWritable key, Text value, 
        OutputCollector<Text, IntWritable> output, 
        Reporter reporter) throws IOException { 
     String line = value.toString(); 
     String num = Integer.parseInt(line); 

     IntWritable one = new IntWritable(num); 

     word.set(「key」); 
     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; 
     int count = 0; 
     int avg = 0; 
     while (values.hasNext()) { 
      sum += values.next().get(); 
      count++; 
     } 
     avg = sum/count; 
     output.collect(key, new IntWritable(count)); 
    } 
} 

看到output.collect()特別,我打印鍵&計數值.. 對於任何輸入文件,輸出爲 鍵2 請幫我... (如何輸出始終是2,即使100號作爲輸入??)map-reduce代碼的輸出是什麼?

+0

conf.setOutputKeyClass(Text.class); conf.setOutputValueClass(IntWritable.class); conf.setMapperClass(MapClass.class); conf.setCombinerClass(Reduce.class); conf.setReducerClass(Reduce.class); – Amnesiac

+0

你可以編輯你的問題。不要垃圾評論。 – Jeremy

+0

我沒有發表任何評論.. – Amnesiac

回答

0

如果你想測試你的代碼,

下面是一個使用JUnit測試mockito框架,假設你在示例類中使用了縮減器和映射器。

@RunWith(MockitoJUnitRunner.class) 
public class TestMapper { 

@Mock 
OutputCollector<Text, IntWritable> output; 

@Mock 
Reporter reporter; 

@Test 
public void testMap() throws Exception { 
    ExampleClass.MapClass mapper = new ExampleClass.MapClass(); 
    mapper.map(new LongWritable(0), new Text("1"), output, reporter); 
    mapper.map(new LongWritable(0), new Text("2"), output, reporter); 
    mapper.map(new LongWritable(0), new Text("3"), output, reporter); 
    verify(output, times(1)).collect(new Text("key"), new IntWritable(1)); 
    verify(output, times(1)).collect(new Text("key"), new IntWritable(2)); 
    verify(output, times(1)).collect(new Text("key"), new IntWritable(3)); 
} 

@Test 
public void testReduce() throws Exception { 
    ExampleClass.Reduce reducer = new ExampleClass.Reduce(); 
    List<IntWritable> list = Arrays.asList(new IntWritable(1), new IntWritable(2), new IntWritable(3)); 

    reducer.reduce(new Text("key"), list.iterator(), output, reporter); 

    verify(output, times(1)).collect(new Text("key"), new IntWritable(3)); 
} 
} 
+0

@joey ...嘿,但輸出應該是數字的計數嗎?你說什麼?? – Amnesiac

+0

如何使用JUNIT測試我的代碼? – Amnesiac

+0

假設您使用IDE,只需將該類放入測試包中,添加mockito依賴關係jar(來自http://code.google.com/p/mockito/downloads/list),IDE應該讓您將其作爲測試課程運行。 – Joey