How to Remove Duplicate Rows in Spark

Apache Spark is a powerful, distributed data processing engine that allows for fast computations on large datasets. A common data wrangling task involves removing duplicate rows from a DataFrame to achieve a set of unique records for subsequent analyses. In this guide, we will cover various methods to remove duplicates in Spark using Scala as the programming language. We will explore multiple scenarios and provide detailed examples, optimizing performance for different contexts.

Understanding Duplicate Rows in Spark

In Spark, a duplicate row is a row that is exactly identical to another row within the same DataFrame. However, the definition of a duplicate can vary based on the context. Sometimes, you may want to consider a row as a duplicate only if certain key columns are the same, and sometimes, the entire row must match. Spark provides flexible options to deal with both situations.

Using dropDuplicates to Remove All Duplicate Rows

The simplest way to remove all duplicate rows from a DataFrame in Spark is to use the dropDuplicates() method. This will remove all rows in the DataFrame that have exactly the same values in all columns.

Here’s a basic example:


import org.apache.spark.sql.SparkSession

val spark = SparkSession.builder.appName("RemoveDuplicates").getOrCreate()
import spark.implicits._

// Sample data
val data = Seq(
  ("John", "Doe", 30),
  ("Jane", "Doe", 30),
  ("John", "Doe", 30)
)

// Create DataFrame
val df = data.toDF("firstName", "lastName", "age")

// Show original DataFrame
df.show()

// Remove duplicate rows
val distinctDF = df.dropDuplicates()

// Show DataFrame after removing duplicates
distinctDF.show()

Output:


+---------+--------+---+
|firstName|lastName|age|
+---------+--------+---+
|     John|     Doe| 30|
|     Jane|     Doe| 30|
|     John|     Doe| 30|
+---------+--------+---+

+---------+--------+---+
|firstName|lastName|age|
+---------+--------+---+
|     Jane|     Doe| 30|
|     John|     Doe| 30|
+---------+--------+---+

As observed from the output, the dropDuplicates() method removed the third row from the DataFrame since it was identical to the first row.

Specifying Columns to Consider for Duplicate Rows

Instead of treating the entire row to determine duplicates, we sometimes need to specify only a subset of columns. This is achieved by passing a list of column names to the dropDuplicates() method. When a list of column names is provided, only the specified columns are considered for evaluating duplicates. Here’s how you can do this:


// Remove duplicate rows based on specific columns
val distinctDF = df.dropDuplicates("firstName", "lastName")

// Show DataFrame after removing duplicates
distinctDF.show()

Output:


+---------+--------+---+
|firstName|lastName|age|
+---------+--------+---+
|     Jane|     Doe| 30|
|     John|     Doe| 30|
+---------+--------+---+

The method now only considers the “firstName” and “lastName” columns when looking for duplicates. Since there are no more duplicate rows based on these two columns, the resulting DataFrame remains the same.

Using distinct to Remove Exact Duplicates

Alternatively, Spark also provides the distinct() method to remove duplicates. This is equivalent to using dropDuplicates() without specifying any columns. However, it’s more intuitive to use when you wish to remove completely identical rows without focusing on specific columns.


// Remove duplicate rows using distinct
val distinctDF = df.distinct()

// Show DataFrame after removing duplicates
distinctDF.show()

Output:


+---------+--------+---+
|firstName|lastName|age|
+---------+--------+---+
|     Jane|     Doe| 30|
|     John|     Doe| 30|
+---------+--------+---+

The result is the same as using dropDuplicates() without arguments, as it removes all rows with identical values in all columns.

Handling Duplicate Rows with a Custom Logic

Sometimes, you might have a more complex requirement for identifying duplicates, such as comparing the rows based on a custom condition. For this purpose, Spark SQL functions or DataFrame transformations can be employed.

Using Spark SQL

To employ custom logic, you can register the DataFrame as a temporary view and use Spark SQL to write a query that encapsulates your logic.


df.createOrReplaceTempView("people")

// Remove duplicates with a custom logic using Spark SQL
val customDistinctDF = spark.sql("""
  SELECT firstName, lastName, age
  FROM (
    SELECT *,
           row_number() OVER (PARTITION BY firstName, lastName ORDER BY age DESC) as rn
    FROM people
  ) WHERE rn = 1
""")

// Show DataFrame after applying custom logic to remove duplicates
customDistinctDF.show()

Output:


+---------+--------+---+
|firstName|lastName|age|
+---------+--------+---+
|     John|     Doe| 30|
|     Jane|     Doe| 30|
+---------+--------+---+

In this example, we used the window function row_number to assign a unique number to each row within the partition of “firstName” and “lastName”, ordered by “age” in descending order. We then filtered for rows where the row number is 1, which effectively gives us one record per group, based on our defined logic.

Considerations for Large Datasets

Removing duplicate rows on large datasets can become computationally expensive, as it involves shuffling data across partitions and possibly across different nodes in your Spark cluster. To optimize for performance:

– Ensure that you define a good partitioning strategy that aligns with the columns used for deduplication.
– If you’re working within a cluster, consider using the salting technique to evenly distribute data across partitions and prevent skewed processing.
– Persist the DataFrame before performing deduplication if you plan to perform multiple actions on the deduplicated result. This will avoid recomputing the entire deduplication process.
– Monitor the Spark UI to understand the job execution plan and identify possible bottlenecks in your deduplication logic.

If executed carefully and with the appropriate considerations for the dataset’s specifics and system resources, removing duplicates in Spark can be an efficient process that significantly improves the quality of your datasets for downstream applications and analyses.

To conclude, Spark offers several ways to remove duplicate rows, and choosing the right method depends on the specifics of the data and the definition of duplicates in the context of your application. By leveraging the dropDuplicates() and distinct() methods and incorporating custom logic with Spark SQL where needed, you can manage duplicates effectively in your Spark applications.

About Editorial Team

Our Editorial Team is made up of tech enthusiasts deeply skilled in Apache Spark, PySpark, and Machine Learning, alongside proficiency in Pandas, R, Hive, PostgreSQL, Snowflake, and Databricks. They're not just experts; they're passionate educators, dedicated to demystifying complex data concepts through engaging and easy-to-understand tutorials.

Leave a Comment

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

Scroll to Top