How to create empty RDD or DataFrame in PySpark Azure Databricks?

Are you looking to find out how to create an empty RDD in the Azure Databricks cloud, or maybe you are looking for a solution, to create an empty DataFrame of PySpark in Azure Databricks? If you are looking for any of these problem solutions, you have landed on the correct page. I will also help you how to use PySpark different functions to create empty RDD/DataFrame with multiple examples in Azure Databricks. I will explain it by taking a practical example. So please don’t waste time let’s start with a step-by-step guide to understand how to create an empty RDD/DataFrame using various functions in PySpark.

In this blog, I will teach you the following with practical examples:

  • Creating empty RDD
  • Creating empty DataFrame
  • Converting empty RDD to DataFrame

Gentle reminder:

In Databricks,

  • sparkSession made available as spark
  • sparkContext made available as sc

In case, you want to create it manually, use the below code.

from pyspark.sql.session import SparkSession

spark = SparkSession.builder 
    .master("local[*]") 
    .appName("azurelib.com") 
    .getOrCreate()

sc = spark.sparkContext

How to create an empty PySpark RDD in Azure Databricks?

Let’s see how to create an empty PySpark RDD in Azure Databricks using various methods.

Example:

# Method 1:
sc.emptyRDD().collect()

# Method 2:
sc.parallelize([]).collect()

# The above codes generates same output as mentioned below.
"""
Output:

[]

"""

How to create an empty PySpark DataFrame in Azure Databricks?

Let’s see how to create an empty PySpark DataFrame in Azure Databricks.

Examples:

# a) Without schema
spark.createDataFrame(sc.emptyRDD(), schema=StructType([])).show()

"""
Output:

++
||
++
++

"""
# b) With schema

from pyspark.sql.types import StructType, StructField, StringType, IntegerType

with_schema = StructType([
    StructField("first_name", StringType()),
    StructField("last_name", StringType()),
    StructField("age", IntegerType())])

spark.createDataFrame(sc.emptyRDD(), schema=with_schema).show()

"""
Output:

+----------+---------+---+
|first_name|last_name|age|
+----------+---------+---+
+----------+---------+---+


"""

How to convert PySpark empty RDD into DataFrame in Azure Databricks?

Let’s see how to convert a PySpark empty RDD into DataFrame in Azure Databricks.

Examples:

ddl_schema = "first_name STRING, last_name STRING, age INT"
sc.emptyRDD().toDF(schema=ddl_schema).printSchema()

"""
Output:

root
 |-- first_name: string (nullable = true)
 |-- last_name: string (nullable = true)
 |-- age: integer (nullable = true)

"""

I have attached the complete code used in this blog in notebook format to this GitHub link. You can download and import this notebook in databricks, jupyter notebook, etc.

When should you use the PySpark empty RDD or DataFrame in Azure Databricks?

We can use an empty RDD or DataFrame when we don’t receive files for processing.

Real World Use Case Scenarios for PySpark empty RDD or DataFrame in Azure Databricks?

Assume that you actively receive files for data processing and you use RDD for processing it by using map(), and flatMap() transformation. What happens to the execution when you don’t receive any file for processing? In this situation creating an empty RDD helps.

What are the alternatives to the PySpark empty RDD or DataFrame functions in PySpark Azure Databricks?

There are multiple alternatives to the PySpark empty RDD or DataFrame, which are as follows:

  • Passing an empty list into parallelize() function
  • Using the emptyRDD() function

Final Thoughts

In this article, we have learned about creating an empty RDD and DataFrame in Azure Databricks, along with the examples explained clearly. I have also covered different scenarios with practical examples that could be possible. I hope the information that was provided helped in gaining knowledge.

Please share your comments and suggestions in the comment section below and I will try to answer all your queries as time permits.

PySpark in Azure Databricks, as explained by Arud Seka Berne S on azurelib.com.

As a big data engineer, I design and build scalable data processing systems and integrate them with various data sources and databases. I have a strong background in Python and am proficient in big data technologies such as Hadoop, Hive, Spark, Databricks, and Azure. My interest lies in working with large datasets and deriving actionable insights to support informed business decisions.