Menu
CoddyTech

Sessions and Actions

Part of the Fundamentals section of Coddy's PySpark journey. Lesson 3 of 18.

A SparkSession manages access to Spark. SparkSession.builder.appName("Practice").getOrCreate() creates a session or reuses one already active in the process. Call spark.stop() when the program is finished.

Spark delays most data processing. A transformation such as select builds a plan and returns a new DataFrame. An action such as count() or collect() asks Spark to execute it. collect() transfers all result rows to Python, so use it only for small results.

numbers = spark.range(4)
print(numbers.count())
spark.stop()

The example prints 4. range(4) describes rows with values from 0 through 3; the count is an action.

appName("Practice") sets a label for the Spark application; it does not change the data or number of rows.

challenge icon

Challenge

Beginner

Change the program to create 7 numbered rows. Print their count, then stop the session. Print only 7.

Try it yourself

from pyspark.sql import SparkSession
spark = SparkSession.builder.appName("Learning").getOrCreate()
numbers = spark.range(1)
print(numbers.count())
spark.stop()
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Online Python compiler