如何设置和火花取得静态变量? [英] how to set and get static variables from spark?

查看:138
本文介绍了如何设置和火花取得静态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一类这样的:

public class Test {
    private static String name;

    public static String getName() {
        return name;
    }

    public static void setName(String name) {
        Test.name = name;
    }

    public static void print() {
        System.out.println(name);
    }

}

在我的星火司机,我设置这样的名称,并调用打印()命令:

in my Spark driver, I'm setting the name like this and calling the print() command:

public final class TestDriver{

    public static void main(String[] args) throws Exception {
        SparkConf sparkConf = new SparkConf().setAppName("TestApp");
        // ...
        // ...
        Test.setName("TestName")
        Test.print();
        // ...
    }
}

不过,我得到一个 NullPointerException异常。我如何将值传递给全局变量和使用它吗?

However, I'm getting a NullPointerException. How do I pass a value to the global variable and use it?

推荐答案

好吧,有基本方法采取知道师傅执行人的值:

Ok, there is basically 2 ways to take a value known to the master to the executors:


  1. 将一个闭包内的值被序列化到执行者来执行任务。这是最常见的一种,非常简单/优雅。样品和DOC 这里

  2. 创建与数据广播的变量。这是很好的一个大尺寸的不可变的数据,所以要保证它是只发送一次。还不错,如果同样的数据被反复使用。样品和DOC 这里

  1. Put the value inside a closure to be serialized to the executors to perform a task. This is the most common one and very simple/elegant. Sample and doc here.
  2. Create a broadcast variable with the data. This is good for immutable data of a big size, so you want to guarantee it is send only once. Also good if the same data is used over and over. Sample and doc here.

没有必要使用静态变量在任何一种情况下。但是,如果你想对你的遗嘱执行人的虚拟机可使用固定的值,你需要做的其中之一:

No need to use static variables in either case. But, if you DO want to have static values available on your executor VMs, you need to do one of these:


  1. 如果该值是固定的或配置可执行人节点上(坛子,里面等生活),那么你可以有一个懒惰的VAL,保证只有一次初始化。

  2. 您可以致电mapPartitions()与使用的上述2个选项中的一个code,则该值存储静态变量/对象。 mapPartitions是保证一次为每个分区(不是每一次行好得多)运行,并有利于这种事情(初始化数据库连接,等等)。

希望这有助于!

P.S:至于你例外:我没有看到它在该code样品,我敢打赌,这是在其他地方发生。

P.S: As for you exception: I just don't see it on that code sample, my bet is that it is occurring elsewhere.

编辑额外澄清:懒惰VAL解决方法是斯卡拉,不涉及星火...

Edit for extra clarification: The lazy val solution is simply Scala, no Spark involved...

object MyStaticObject
{
  lazy val MyStaticValue = {
     // Call a database, read a file included in the Jar, do expensive initialization computation, etc
     4
  }
} 

由于每个执行人对应一个JVM,一旦类被加载 MyStaticObject 将被初始化。在关键字保证了 MyStaticValue 变量只能初始化第一次它实际上是要求,并保持它的价值至今。

Since each Executor corresponds to a JVM, once the classes are loaded MyStaticObject will be initialized. The lazy keyword guarantees that the MyStaticValue variable will only be initialized the first time it is actually requested, and hold its value ever since.

这篇关于如何设置和火花取得静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆