2012-05-21 72 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