PySpark SQL DataTypes and Usage Examples

Apache Spark is a powerful tool for processing large-scale data efficiently and PySpark is its Python API, which provides a way to harness the capabilities of Spark using Python. One of the core features of PySpark is its ability to work with structured data through Spark SQL. A solid understanding of PySpark’s SQL data types is essential for anyone looking to perform data analysis, data transformation, or data science tasks. In this guide, we will explore PySpark SQL data types and provide usage examples to demonstrate how to work with these types in PySpark.

Understanding PySpark SQL DataTypes

PySpark provides a module called pyspark.sql.types which contains data types that are used to define the schema of a DataFrame. These data types are an abstraction of the data structure used to store data. Specifying the correct data type for each column is essential for data integrity and query performance. Below, we’ll discuss the various data types available in PySpark SQL and their usage.

Basic Types

At the fundamental level, PySpark supports a variety of basic data types similar to the ones in traditional SQL systems:

  • ByteType: Represents a byte-long integer.
  • ShortType: Represents a short integer.
  • IntegerType: Represents a 4-byte integer.
  • LongType: Represents an 8-byte integer.
  • FloatType: Represents a 4-byte single-precision floating-point number.
  • DoubleType: Represents an 8-byte double-precision floating-point number.
  • DecimalType: Represents arbitrary-precision signed decimal numbers.
  • StringType: Represents a string.
  • BinaryType: Represents a binary string.
  • BooleanType: Represents a boolean value.
  • TimestampType: Represents a timestamp.
  • DateType: Represents a date.

Complex Types

Besides the basic types, PySpark SQL also supports more complex data types:

  • ArrayType: Represents an array of elements.
  • MapType: Represents a map (like a dictionary in Python) of key-value pairs.
  • StructType: Represents a row object or named columns (similar to a struct in C or an object in JavaScript).
  • StructField: Represents a field in a StructType.

Usage Examples

Let’s see some practical examples of how these data types can be used when working with PySpark DataFrames.

Creating a DataFrame with Specified Data Types

When you create a DataFrame from an existing data source or an RDD, you can specify the schema using PySpark SQL data types:


from pyspark.sql import SparkSession
from pyspark.sql.types import *

# Initialize SparkSession
spark = SparkSession.builder.appName('PySparkSQLDataTypesExample').getOrCreate()

# Define a schema
schema = StructType([
    StructField("name", StringType(), True),
    StructField("age", IntegerType(), True),
    StructField("salary", FloatType(), True)
])

# Create data
data = [("John Doe", 30, 100000.0), ("Jane Doe", 25, 150000.0), ("Mike Taylor", 45, 200000.0)]

# Create a DataFrame with the specified schema
df = spark.createDataFrame(data, schema)

df.show()

Expected output:


+----------+---+--------+
|      name|age|  salary|
+----------+---+--------+
|  John Doe| 30|100000.0|
|  Jane Doe| 25|150000.0|
|Mike Taylor| 45|200000.0|
+----------+---+--------+

Working with ArrayType

ArrayType is used when you want to store an array in a DataFrame column. Here is an example:


from pyspark.sql.functions import array

# Define schema with an ArrayType column
array_schema = StructType([
    StructField("name", StringType(), True),
    StructField("hobbies", ArrayType(StringType()), True)
])

# Create data with arrays
array_data = [("John Doe", ["football", "basketball"]), ("Jane Doe", ["reading", "travelling"])]

# Create a DataFrame with arrays
array_df = spark.createDataFrame(array_data, array_schema)

array_df.show()

Expected output:


+--------+--------------------+
|    name|             hobbies|
+--------+--------------------+
|John Doe| [football, basket...|
|Jane Doe| [reading, travell...|
+--------+--------------------+

Working with MapType

MapType is used for columns that store key-value pairs. We can create such a DataFrame as follows:


from pyspark.sql.functions import col, map_from_arrays

# Define schema with a MapType column
map_schema = StructType([
    StructField("name", StringType(), True),
    StructField("properties", MapType(StringType(), StringType()), True)
])

# Create data with maps
map_data = [("John Doe", {"hair": "black", "eye": "brown"}), ("Jane Doe", {"hair": "brown", "eye": "blue"})]

# Create a DataFrame with maps
map_df = spark.createDataFrame(map_data, map_schema)

map_df.show()

Expected output:


+--------+--------------------+
|    name|          properties|
+--------+--------------------+
|John Doe|{hair -> black, e...|
|Jane Doe|{hair -> brown, e...|
+--------+--------------------+

Working with StructType

StructType allows you to embed a structure within a column. Let’s create a DataFrame where one of the columns is a structure consisting of multiple fields:


# Define schema with a StructType column
struct_schema = StructType([
    StructField("name", StringType(), True),
    StructField("address", StructType([
        StructField("street", StringType(), True),
        StructField("city", StringType(), True),
        StructField("country", StringType(), True)
    ]), True)
])

# Create data with structures
struct_data = [("John Doe", ("123 Main St", "Anytown", "USA")), ("Jane Doe", ("456 Maple St", "Othertown", "USA"))]

# Create a DataFrame with structures
struct_df = spark.createDataFrame(struct_data, struct_schema)

struct_df.show(truncate=False)

Expected output:


+--------+---------------------------+
|name    |address                    |
+--------+---------------------------+
|John Doe|{123 Main St, Anytown, USA}|
|Jane Doe|{456 Maple St, Othertown, USA}|
+--------+---------------------------+

Querying with Data Types

You can also use data types when querying DataFrames using SQL expressions:


from pyspark.sql.functions import col, expr

# Querying using a boolean expression
bool_df = df.filter(col("age") > 30)

bool_df.show()

# Querying using SQL expression
sql_df = df.select(expr("name AS employee_name"), expr("salary * 0.10 AS bonus"))

sql_df.show()

Expected output for bool_df:


+-----------+---+--------+
|       name|age|  salary|
+-----------+---+--------+
|Mike Taylor| 45|200000.0|
+-----------+---+--------+

Expected output for sql_df:


+-------------+------+
|employee_name| bonus|
+-------------+------+
|     John Doe|10000.0|
|     Jane Doe|15000.0|
| Mike Taylor|20000.0|
+-------------+------+

The examples outlined above showcase the versatility of data types in PySpark SQL. These types form the cornerstone of structured data processing, enabling data engineers and scientists to handle complex datasets reliably and with ease.

About Editorial Team

Our Editorial Team is made up of tech enthusiasts who are highly skilled in Apache Spark, PySpark, and Machine Learning. They are also proficient in Python, Pandas, R, Hive, PostgreSQL, Snowflake, and Databricks. They aren't just experts; they are passionate teachers. They are dedicated to making complex data concepts easy to understand through engaging and simple tutorials with examples.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top