如何在代码的任何位置获取当前的SparkSession? [英] How can I get the current SparkSession in any place of the codes?
问题描述
我已经在main()
函数中创建了一个会话,如下所示:
I have created a session in the main()
function, like this:
val sparkSession = SparkSession.builder.master("local[*]").appName("Simple Application").getOrCreate()
现在,如果要配置应用程序或访问属性,则可以在同一函数中使用局部变量sparkSession
.
Now if I want to configure the application or access the properties, I can use the local variable sparkSession
in the same function.
如果我想在同一项目的其他位置(例如project/module/.../.../xxx.scala
)访问此sparkSession
,该怎么办.我该怎么办?
What if I want to access this sparkSession
elsewhere in the same project, like project/module/.../.../xxx.scala
. What should I do?
推荐答案
一旦创建会话(任何地方),您就可以安全地使用:
Once a session was created (anywhere), you can safely use:
SparkSession.builder().getOrCreate()
要在代码中的任意位置获取(相同)会话,只要该会话仍然存在,就可以. Spark会维护一个活动会话,因此除非它被停止或崩溃,否则您将获得相同的会话.
To get the (same) session anywhere in the code, as long as the session is still alive. Spark maintains a single active session so unless it was stopped or crashed, you'll get the same one.
这篇关于如何在代码的任何位置获取当前的SparkSession?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!