spark context from spark session pyspark

Spark Context from Spark Session in PySpark

By John King
AD

In the world of Apache Spark, frequently developers find themselves working with SparkContext and SparkSession. While they might seem similar, they serve different purposes and possess distinct features. Let's delve into both, starting with SparkContext, which was the primary entry point for interactive Spark applications, now largely superseded by the more intuitive SparkSession.

SparkContext encapsulates a running Spark application, providing methods to access Spark services such as paralleled execution on cluster with fault tolerance. It is basically a top-level object of Spark application for providing entry point to Spark functionality housed in SparkConf.

Understanding SparkContext

SparkContext is not just a component of Spark's API; it's what makes a Spark program, a Spark program. It is the core entry point to every Spark functionality. It provides you capabilities like access to Spark's services, resources management, and job scheduling. Essentially, the SparkContext sits on top of a running Spark application.

AD
PySpark 4.0 Tutorial For Beginners with Examples - Spark By {Examples}
PySpark 4.0 Tutorial For Beginners with Examples - Spark By {Examples}
an info sheet describing how to use spark architecture for your website or blog, with the text below it
an info sheet describing how to use spark architecture for your website or blog, with the text below it
an abstract blue and yellow light in the middle of a dark background with swirly lines
an abstract blue and yellow light in the middle of a dark background with swirly lines

However, with the advent of SparkSession, developers increasingly prefer using SparkSession over SparkContext because it simplifies application configuration and provides a more intuitive API. But let's not dismiss SparkContext yet; we have lots to explore in terms of understanding since it is still a fundamental component of Apache Spark.

Configuring a SparkContext

SparkContext configuration is generally done through a SparkConf object. The SparkConf object is passed to the constructor of SparkContext. For example, setting a property might look like this:

a person holding a large metal object with sparks coming out of it's end
a person holding a large metal object with sparks coming out of it's end
#FireSafetyBasics #FireScienceForBeginners
#FireSafetyBasics #FireScienceForBeginners
Your Spark Job Is Running. Then a Server Dies. Here’s Exactly What Happens Next.
Your Spark Job Is Running. Then a Server Dies. Here’s Exactly What Happens Next.
```python conf = SparkConf().setAppName("MyApp").setMaster("local") sc = SparkContext(conf = conf) ```

Here, "MyApp" is set as the application name, and "local" is used as the Spark master node address.

Launching Parallel Jobs

Once you have your SparkContext, you can run any number of parallel jobs at the same time. This makes it easy to distribute the processing of large data sets across a cluster. You can take advantage of this feature to run multiple applications simultaneously on Spark.

AD

SparkContext comes with a simplistic method like Parallelize which allows you to convert an in-memory collection into an RDD (Resilient Distributed Dataset). Here's how you do it:

Sparks
Sparks
a sparkler is lit up in the night sky
a sparkler is lit up in the night sky
Sparkling background
Sparkling background
a close up of a sparkler on a black background with lots of lightening
a close up of a sparkler on a black background with lots of lightening
a hand holding a sparkler in front of a house at night with the light turned on
a hand holding a sparkler in front of a house at night with the light turned on
fireworks are lit up in the dark with bright blue and yellow lights shining from them
fireworks are lit up in the dark with bright blue and yellow lights shining from them
```python data = [1, 2, 3, 4, 5] distData = sc.parallelize(data) ```

Now, `distData` is an RDD and ready to be processed on a distributed manner.

SparkSession: A More Intuitive Approach

SparkSession is introduced in Spark 2.0 as a replacement for SparkContext, SparkConf, and SparkHiveContext. It is a single entry point to all core Spark APIs. It encapsulates the functionality of SparkContext, providing a clean and clear API to Spark users.

AD

Creating a SparkSession is as simple as calling `SparkSession.builder` with builders to configure SparkSession, and then calling `getOrCreate` to return an existing SparkSession or create a new one:

a person holding a sparkler in their hand while it's being lit up
a person holding a sparkler in their hand while it's being lit up
a sparkler is lit up in the dark by some water and trees behind it
a sparkler is lit up in the dark by some water and trees behind it
welders are working on some kind of metal thing that looks like firework
welders are working on some kind of metal thing that looks like firework
```python ss = SparkSession.builder.appName("MyApp")\ .config("spark.some.config.option", "some-value")\ .getOrCreate() ```

Accessing SparkContext through SparkSession

One clear advantage of SparkSession is that it provides a simpler and more intuitive API than using SparkContext directly. For instance, if you've created a SparkSession, you can easily get the SparkContext:

```python sc = ss.sparkContext ```

This makes it easier to use Spark's APIs as everything can be accessed via the SparkSession object.

AD

Creating DataFrames and Datasets

SparkSession provides methods to create DataFrames and Datasets, two crucial data abstractions in Spark 2.0 and beyond.

Here's how you can create a DataFrame from an RDD:

```python df = ss.createDataFrame([(1, 'a'), (2, 'b')], schema = ['id', 'value']) ```

This is just one example; there are plenty of ways to create DataFrames and datasets using SparkSession.

In the ever-evolving field of big data processing, technologies such as Spark continuously improve, offering developers richer tools to work with. By grasping the nuances of SparkSession and SparkContext, developers are better equipped to tackle a broad spectrum of projects. As a result, the learning curve for working with these technologies will likely provide a substantial boost to the efficiency and effectiveness of your coding journey.

AD