Custom Dstinct Mapper中的NullPointerException

时间:2016-08-02 15:46:39

标签: hazelcast hazelcast-imap

我正在使用hazelcast 3.6.1并使用自定义mapreduce实现不同的聚合功能,以获得solr facet类型的结果。

public class DistinctMapper implements Mapper<String, Employee, String, Long>{

    private transient SimpleEntry<String, Employee> entry = new SimpleEntry<String, Employee>();

    private static final Long ONE = Long.valueOf(1L);

    private Supplier<String, Employee, String> supplier;

    public DistinctMapper(Supplier<String, Employee, String> supplier) {
        this.supplier = supplier;
    }

    @Override
    public void map(String key, Employee value, Context<String, Long> context) {
        System.out.println("Object "+ entry + " and key "+key);
        entry.setKey(key);
        entry.setValue(value);
        String fieldValue = (String) supplier.apply(entry);
        //getValue(value, fieldName);
        if (null != fieldValue){
            context.emit(fieldValue, ONE);
        }
    }
}

并且mapper因NullPointerException而失败。和sysout语句说entry对象为null。

SimpleEntry:https://github.com/hazelcast/hazelcast/blob/v3.7-EA/hazelcast/src/main/java/com/hazelcast/mapreduce/aggregation/impl/SimpleEntry.java

你能在上面的代码中指出我的问题吗?感谢。

1 个答案:

答案 0 :(得分:1)

entry字段是暂时的。这意味着它没有被序列化,所以当在hazecalst节点上反序列化DistinctMapper对象时,它的值为null。 删除瞬态将解决NullPointerException

旁注: 你为什么需要这个输入字段?它似乎没有任何用处。