2

我是 Hadoop 新手,这是我的第一个映射程序,我正在通过 MR 单元对其进行单元测试。

我正在传递我通过配置对象设置的参数(年份)

    Configuration config =new Configuration()           
    config.set("Year", "2012");
    Job job=new Job(config ,"Yearly");

我的映射器:

public void map(ImmutableBytesWritable row, Result values, Context context)throws IOException, InterruptedException 
{   
  Configuration conf = context.getConfiguration();
  String Year= conf.get("Year");
}

在 MR 单元测试中,我正在模拟上下文类以及 key 、 value

@Test
public void testMapper() throws IOException, InterruptedException
{
  context = mock(Mapper.Context.class);

  Configuration conf=mock(Configuration.class);
  when(conf.get("Year")).thenReturn("2012");
  when(context.getConfiguration()).thenReturn(conf);
  mapper.map(row, result, context);
}

但是无法在映射中获取值(Year),接收 null。我这样做是正确的还是有更好的方法来测试映射。

4

1 回答 1

2

您应该在测试代码中从 mapdriver 获取配置,如下所示:

Configuration conf = mapdriver.getConfiguration();
conf.set("Year","2013");
于 2014-12-15T03:53:33.750 回答