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

Broadcast variable Null pointer exception in spark streaming

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

我在可序列化的 HashMap 中存储了一个模型。我使用声明为全局静态变量的广播变量来广播此哈希图:

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

我需要在我的映射器阶段访问这个模型,并根据检查做一些操作。以下是我如何访问广播变量的片段。

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 应该引用存储在广播变量中的哈希映射。 谁能帮我理解在 JAVA 中访问广播变量的正确方法是什么?

我创建了一个要点来参考代码:https://gist.github.com/nipunarora/ed987e45028250248edc

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

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