Reputation: 2603
I have a Spark application which using Spark 2.0 new API with SparkSession
.
I am building this application on top of the another application which is using SparkContext
. I would like to pass SparkContext
to my application and initialize SparkSession
using existing SparkContext
.
However I could not find a way how to do that. I found that SparkSession
constructor with SparkContext
is private so I can't initialize it in that way and builder does not offer any setSparkContext
method. Do you think there exist some workaround?
Upvotes: 43
Views: 62371
Reputation: 2021
Deriving the SparkSession
object out of SparkContext
or even SparkConf
is easy. Just that you might find the API to be slightly convoluted. Here's an example (I'm using Spark 2.4
but this should work in the older 2.x
releases as well):
// If you already have SparkContext stored in `sc`
val spark = SparkSession.builder.config(sc.getConf).getOrCreate()
// Another example which builds a SparkConf, SparkContext and SparkSession
val conf = new SparkConf().setAppName("spark-test").setMaster("local[2]")
val sc = new SparkContext(conf)
val spark = SparkSession.builder.config(sc.getConf).getOrCreate()
Hope that helps!
Upvotes: 41
Reputation: 756
val sparkSession = SparkSession.builder.config(sc.getConf).getOrCreate()
Upvotes: 5
Reputation: 219
Like in the above example you cannot create because SparkSession
's constructor is private
Instead you can create a SQLContext
using the SparkContext
, and later get the sparksession from the sqlcontext like this
val sqlContext=new SQLContext(sparkContext);
val spark=sqlContext.sparkSession
Hope this helps
Upvotes: 21
Reputation: 1089
public JavaSparkContext getSparkContext()
{
SparkConf conf = new SparkConf()
.setAppName("appName")
.setMaster("local[*]");
JavaSparkContext jsc = new JavaSparkContext(conf);
return jsc;
}
public SparkSession getSparkSession()
{
sparkSession= new SparkSession(getSparkContext().sc());
return sparkSession;
}
you can also try using builder
public SparkSession getSparkSession()
{
SparkConf conf = new SparkConf()
.setAppName("appName")
.setMaster("local");
SparkSession sparkSession = SparkSession
.builder()
.config(conf)
.getOrCreate();
return sparkSession;
}
Upvotes: 7
Reputation: 2603
Apparently there is no way how to initialize SparkSession
from existing SparkContext
.
Upvotes: 13