【问题标题】:Mocking the context object in MR unit在 MR 单元中模拟上下文对象
【发布时间】:2014-12-15 11:53:33
【问题描述】:

我是 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 单元测试中,我正在模拟上下文类以及键、值

@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。我这样做是正确的还是有更好的方法来测试映射。

【问题讨论】:

  • 您为什么不使用 MRUnit 的 MapperDriver 来测试这个?
  • 我使用了MRUnit的MapDriver,这样就不需要mock配置对象了,我们可以用MapDriver.setConfiguration(conf)来设置conf。但是我需要模拟键和值并将测试运行为 mapDriver.withInputKey(key).withInputValue().withOutput()..runTest();谢谢。
  • 您在评论结束时迷失了我 - 您能否将您的问题更新为您目前的问题,并列出仍然导致您出现问题的问题 - 谢谢

标签: hadoop hbase mrunit


【解决方案1】:

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

Configuration conf = mapdriver.getConfiguration();
conf.set("Year","2013");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-19
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 2014-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多