【问题标题】:Received unexpected output in MRUnit在 MRUnit 中收到意外的输出
【发布时间】:2014-09-08 11:45:16
【问题描述】:

我收到以下 MRUnit 错误:

ERROR mrunit.TestDriver:收到意外输出(60,mrdp.MyCustomClass@73207f36)

ERROR mrunit.TestDriver:位置 0 处缺少预期输出(60,mrdp.MyCustomClass@6f73cf45)

我创建了一个实现WritableMyCustomClass,并具有4 个int 属性。这是我的 Mapper 的输出值。

以下是mapper的MRUnit测试代码:

@Test
public void testMapper() throws IOException {
    MyCustomClass result = new MyCustomClass();
    result.setAttr1(1);
    result.setAttr2(0);
    result.setAttr3(0);
    result.setAttr4(0);

    mapDriver.withInput(new LongWritable(1), new Text("60,5596,1,256"));
    mapDriver.addOutput(new Text("60"), result);
    mapDriver.runTest();
}

当在上面的 new Text("60,5596,1,256") 中找到“1”时,我的 Mapper 应该调用它的设置器 setAttr1(1)

如何使用自定义类(具有多个属性)测试此结果?作业执行成功,只是不知道如何让MRUnit测试工作。

$ hadoop fs -cat patterns/minmaxcount/outuserprefs/part*
23  mrdp.MyCustomClass@4cf15f6c
60  mrdp.MyCustomClass@4cf15f6c

【问题讨论】:

    标签: java hadoop mapreduce mrunit


    【解决方案1】:

    如果您想测试相等性,您需要为您的自定义类覆盖 equals()hascode()。如果你不这样做,就没有办法测试“语义平等”。将使用默认的 Object 方法。这就是你所面临的。更多讨论请见Why do I need to override the equals and hashCode methods in Java?

    下面是一个使用自定义类CustomClass 的简单JUnit 测试。我注释掉了equalshashcode。如果您运行测试,它将失败,并显示与您收到的类似的消息。如果您删除 cmets 并运行它,它将通过。

    import static org.junit.Assert.*;
    import org.junit.Test;
    
    public class CustomClass {
    
        String firstName;
        String lastName;
    
        public void setFirstName(String firstName) { this.firstName = firstName; }
        public void setLastName(String lastName) { this.lastName = lastName; }
    
        @Test
        public void testEqaulity() {
            CustomClass clazz1 = new CustomClass();
            clazz1.setFirstName("Stack");
            clazz1.setLastName("Overflow");
    
            CustomClass clazz2 = new CustomClass();
            clazz2.setFirstName("Stack");
            clazz2.setLastName("Overflow");
    
            assertEquals(clazz1, clazz2);
        }
    
        /*
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result
                    + ((firstName == null) ? 0 : firstName.hashCode());
            result = prime * result
                    + ((lastName == null) ? 0 : lastName.hashCode());
            return result;
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            CustomClass other = (CustomClass) obj;
            if (firstName == null) {
                if (other.firstName != null)
                    return false;
            } else if (!firstName.equals(other.firstName))
                return false;
            if (lastName == null) {
                if (other.lastName != null)
                    return false;
            } else if (!lastName.equals(other.lastName))
                return false;
            return true;
        }
        */
    }
    

    如果您没有实现这些方法的经验或知识,大多数 IDE 都可以选择为您创建它们。

    • Eclipse:右键点击类->源码->生成equals和hashcode
    • Netbeans:右键单击源代码编辑器 -> 插入代码 -> equals() hashcode()

    在这两种情况下,您都需要选择要在 equals 和 hashcode 中包含(检查)的属性。这是我使用的仅有的两个 IDE :-)

    【讨论】:

    猜你喜欢
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    相关资源
    最近更新 更多