Non-static field ‘func1‘ cannot be referenced from from a static context

解决方案:

加上static

举例如下:

public PairFunction func1 = new PairFunction <Tuple2<Long,Long>, String, Long>()
{
    private static final long serialVersionUID = 1L;
    @Override
    public Tuple2<String, Long> call(Tuple2<Long, Long> tuple)
            throws Exception {
        Random random = new Random();
        int prefix = random.nextInt(10);
        return new Tuple2<String, Long>(prefix + "_" + tuple._1, tuple._2);
    }
};

 改成

public static   PairFunction func1 = new PairFunction <Tuple2<Long,Long>, String, Long>()
{
    private static final long serialVersionUID = 1L;
    @Override
    public Tuple2<String, Long> call(Tuple2<Long, Long> tuple)
            throws Exception {
        Random random = new Random();
        int prefix = random.nextInt(10);
        return new Tuple2<String, Long>(prefix + "_" + tuple._1, tuple._2);
    }
};

猜你喜欢

转载自blog.csdn.net/appleyuchi/article/details/107743364