SparkContext.getOrCreate() purpose

落花浮王杯 提交于 2019-12-21 17:56:25

问题


What is the purpose of the getOrCreate method from SparkContext class? I don't understand when we should use this method.

If I have 2 spark applications that are run with spark-submit, and in the main method I instantiate the spark context with SparkContext.getOrCreate, both app will have the same context?

Or the purpose is simpler, and the only purpose is when I create a spark app, and I don't want to send the spark context as a parameter to a method, and I will get it as a singleton object?


回答1:


If I have 2 spark applications that are run with spark-submit, and in the main method I instantiate the spark context with SparkContext.getOrCreate, both app will have the same context?

No, SparkContext is a local object. It is not shared between applications.

when I create a spark app, and I don't want to send the spark context as a parameter to a method, and I will get it as a singleton object?

This is exactly the reason. SparkContext (or SparkSession) are ubiquitous in Spark applications and core Spark's source, and passing them around would a huge burden.

It also useful for multithreaded applications where arbitrary thread can initalize contexts.

About docs:

is function may be used to get or instantiate a SparkContext and register it as a singleton object. Because we can only have one active SparkContext per JVM, this is useful when applications may wish to share a SparkContext.

Driver runs in its own JVM and there is no built-in mechanism to share it between multiple full-fledged Java applications (proper application executing its own main. Check Is there one JVM per Java application? and Why have one JVM per application? for related general questions). Application refers to "logical application" where multiple modules execute its own code - one example is SparkJob on spark-jobserver. This scenario is no different than passing SparkContext to a function.



来源:https://stackoverflow.com/questions/47813646/sparkcontext-getorcreate-purpose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!