火花流中的广播变量空指针异常

时间:2015-06-23 15:04:18

标签: apache-spark spark-streaming

我有一个火花流应用程序,我需要访问保存在HashMap中的模型。 在本地安装中使用广播变量运行相同的代码没有问题。但是当我在我的spark测试集群上部署它时,我得到一个空指针异常。

我已将模型存储在可序列化的HashMap中。我使用声明为全局静态变量的广播变量来广播此hashmap:

public static Broadcast<HashMap<String,FieldModel>> br;
HashMap<String,FieldModel> hm = checkerObj.getModel(esserver, type);
br = ssc.sparkContext().broadcast(hm);

我需要在mapper阶段访问此模型,并根据检查进行一些操作。以下是我如何访问广播变量的片段。

JavaDStream<Tuple3<Long,Double,String>> split = matched.map(new GenerateType2Scores());

class GenerateType2Scores implements Function<String, Tuple3<Long, Double, String>> {
    @Override
    public Tuple3<Long, Double, String> call(String s) throws Exception{

        Long time = Type2ViolationChecker.getMTS(s);
        HashMap<String,FieldModel> temphm= Type2ViolationChecker.br.value();

        Double score = Type2ViolationChecker.getAnomalyScore(temphm,s);
        return new Tuple3<Long, Double, String>(time,score, s);}
}

temphm应该引用存储在广播变量中的hashmap。 任何人都可以帮助我理解在JAVA中访问广播变量的正确方法是什么?

我已经创建了一个要点来引用代码:https://gist.github.com/nipunarora/ed987e45028250248edc

1 个答案:

答案 0 :(得分:2)

感谢@ user52045,我找到了答案。

广播变量必须声明为final,并且不能为全局引用声明为static:P

相关问题