java 从 SparkSession 获取 JavaSparkContext
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42582951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get JavaSparkContext from a SparkSession
提问by moe
Im am using a SparkSession
to run my spark application because I use a lot of spark-sql features. I would like to use the JavaSparkContext
to create an RDD from a list. But via the Session I can only get an ordinarySparkContext
. Is there a way to transform the context in this direction?
我正在使用 aSparkSession
来运行我的 spark 应用程序,因为我使用了很多 spark-sql 功能。我想使用JavaSparkContext
从列表中创建 RDD。但是通过 Session 我只能得到一个普通的SparkContext
. 有没有办法在这个方向上改变上下文?
回答by Serhiy
After determining the SparkContext
, you could use:
确定后SparkContext
,您可以使用:
SparkContext sc = ...
JavaSparkContext jsc = JavaSparkContext.fromSparkContext(sc);
This will return you the new instance of JavaSparkContext
, but there is no problem as long as you maintain just one active instance of the SparkContext
.
这将返回 的新实例JavaSparkContext
,但只要您只维护 . 的一个活动实例就没有问题SparkContext
。
回答by Derek_M
Yep, you can do that with the spark session like this:
是的,你可以用这样的 spark 会话来做到这一点:
val spark = SparkSession.builder()
.config(sparkConf)
.getOrCreate()
val jsc = new JavaSparkContext(spark.sparkContext)
or in java, it would be:
或在 Java 中,它将是:
SparkSession spark = SparkSession.builder().config(sparkConf).getOrCreate();
JavaSparkContext jsc = new JavaSparkContext(spark.sparkContext());