Built with Tutify.io

    AIStudyLabs

    DataFrame - Filter Rows

    DataFrame - Filter Rows

    Introduction

    Spark where() function is used to filter the rows from DataFrame or Dataset based on the given condition or SQL expression.

    Spark DataFrame where() Syntaxes

    // Spark DataFrame where() Syntaxes
    1) where(condition: Column): Dataset[T]
    2) where(conditionExpr: String): Dataset[T] //using SQL expression
    3) where(func: T => Boolean): Dataset[T]
    4) where(func: FilterFunction[T]): Dataset[T]
    

    The first signature is used with condition with Column names using $colname, col("colname"), 'colname and df("colname") with condition expression.

    The second signature will be used to provide SQL expressions to filter rows.

    The third signature can be used for SQL functions where the function is applied on each row, and the result with “true” is returned.

    The fourth signature is used with FilterFunction class.

    Before we start with examples, first, let’s create a DataFrame.

    import org.apache.spark.sql.{Row, SparkSession}
    import org.apache.spark.sql.types.{ArrayType, StringType, StructType}
    import org.apache.spark.sql.functions.array_contains
    
    val arrayStructureData = Seq(
        Row(Row("James","","Smith"),List("Java","Scala","C++"),"OH","M"),
        Row(Row("Anna","Rose",""),List("Spark","Java","C++"),"NY","F"),
        Row(Row("Julia","","Williams"),List("CSharp","VB"),"OH","F"),
        Row(Row("Maria","Anne","Jones"),List("CSharp","VB"),"NY","M"),
        Row(Row("Jen","Mary","Brown"),List("CSharp","VB"),"NY","M"),
        Row(Row("Mike","Mary","Williams"),List("Python","VB"),"OH","M")
    )
    
    val arrayStructureSchema = new StructType()
        .add("name",new StructType()
          .add("firstname",StringType)
          .add("middlename",StringType)
          .add("lastname",StringType))
        .add("languages", ArrayType(StringType))
        .add("state", StringType)
        .add("gender", StringType)
    
    val df = spark.createDataFrame(
        spark.sparkContext.parallelize(arrayStructureData),arrayStructureSchema)
    
    df.printSchema()
    df.show()
    

    This yields below schema and DataFrame results.

    // Output:
    root
     |-- name: struct (nullable = true)
     |    |-- firstname: string (nullable = true)
     |    |-- middlename: string (nullable = true)
     |    |-- lastname: string (nullable = true)
     |-- languages: array (nullable = true)
     |    |-- element: string (containsNull = true)
     |-- state: string (nullable = true)
     |-- gender: string (nullable = true)
    
    +--------------------+------------------+-----+------+
    |                name|         languages|state|gender|
    +--------------------+------------------+-----+------+
    |    [James, , Smith]|[Java, Scala, C++]|   OH|     M|
    |      [Anna, Rose, ]|[Spark, Java, C++]|   NY|     F|
    | [Julia, , Williams]|      [CSharp, VB]|   OH|     F|
    |[Maria, Anne, Jones]|      [CSharp, VB]|   NY|     M|
    |  [Jen, Mary, Brown]|      [CSharp, VB]|   NY|     M|
    |[Mike, Mary, Will...|      [Python, VB]|   OH|     M|
    +--------------------+------------------+-----+------+
    

    Use Column with the condition to filter the rows from DataFrame, using this you can express complex conditions by referring to column names using col(name), $"colname" or dfObject("colname"), this approach is mainly used while working with DataFrames. Use “===” for comparison.

    // DataFrame where() with Column condition
    df.where(df("state") === "OH")
        .show(false)
    

    This yields the below DataFrame results.

    // Output:
    +----------------------+------------------+-----+------+
    |name                  |languages         |state|gender|
    +----------------------+------------------+-----+------+
    |[James, , Smith]      |[Java, Scala, C++]|OH   |M     |
    |[Julia, , Williams]   |[CSharp, VB]      |OH   |F     |
    |[Mike, Mary, Williams]|[Python, VB]      |OH   |M     |
    +----------------------+------------------+-----+------+
    

    where() with SQL Expression

    If you have an SQL background, you can use that knowledge in Spark to filter DataFrame rows with SQL expressions.

    // DataFrame where() with SQL Expression
    df.where("gender == 'M'")
        .show(false)
    
    // Output:
    +----------------------+------------------+-----+------+
    |name                  |languages         |state|gender|
    +----------------------+------------------+-----+------+
    |[James, , Smith]      |[Java, Scala, C++]|OH   |M     |
    |[Maria, Anne, Jones]  |[CSharp, VB]      |NY   |M     |
    |[Jen, Mary, Brown]    |[CSharp, VB]      |NY   |M     |
    |[Mike, Mary, Williams]|[Python, VB]      |OH   |M     |
    +----------------------+------------------+-----+------+
    

    Filtering with multiple conditions

    To filter rows on DataFrame based on multiple conditions, you can use either Column with a condition or SQL expression. Below is just a simple example, you can extend this with AND(&&), OR(||), and NOT(!) conditional expressions as needed.

    // Multiple condition
    df.where(df("state") === "OH" && df("gender") === "M")
        .show(false)
    

    This yields the below DataFrame results.

    // Output:
    +----------------------+------------------+-----+------+
    |name                  |languages         |state|gender|
    +----------------------+------------------+-----+------+
    |[James, , Smith]      |[Java, Scala, C++]|OH   |M     |
    |[Mike, Mary, Williams]|[Python, VB]      |OH   |M     |
    +----------------------+------------------+-----+------+
    

    Filtering on an Array column

    You can use the first syntax when you want to filter rows from DataFrame based on the value present in an array collection column. The example below uses array_contains() SQL function which checks if a value contains in an array; if present it returns true, otherwise false. You will learn more about SQL functions in later chapters.

    import org.apache.spark.sql.functions.array_contains
    
    // Filtering on an Array column
    df.where(array_contains(df("languages"),"Java"))
        .show(false)
    

    This yields the below DataFrame results.

    // Output:
    +----------------+------------------+-----+------+
    |name            |languages         |state|gender|
    +----------------+------------------+-----+------+
    |[James, , Smith]|[Java, Scala, C++]|OH   |M     |
    |[Anna, Rose, ]  |[Spark, Java, C++]|NY   |F     |
    +----------------+------------------+-----+------+
    

    Filtering on Nested Struct columns

    If your DataFrame consists of nested struct columns, you can use any of the above syntaxes to filter the rows based on the nested column.

    // Struct condition
    df.where(df("name.lastname") === "Williams")
        .show(false)
    

    This yields the below DataFrame results.

    // Output:
    +----------------------+------------+-----+------+
    |name                  |languages   |state|gender|
    +----------------------+------------+-----+------+
    |[Julia, , Williams]   |[CSharp, VB]|OH   |F     |
    |[Mike, Mary, Williams]|[Python, VB]|OH   |M     |
    +----------------------+------------+-----+------+
    

    Apache Spark Tutorial with Scala Examples — DataFrame Narrow Transformation

    Introduction to Apache Spark

    Setup Apache Spark Environment

    SparkSession & SparkContext

    Resilient Distributed Datasets (RDDs)

    Apache Spark Concepts

    Cache, Persistence and Shared Variables

    DataFrame Introduction

    DataFrame Narrow Transformation

    DataFrame Wider Transformation

    DataFrame Actions

    Spark SQL Functions

    Advanced DataFrame Features

    Using SQL Queries

    Spark Read & Write CSV Files

    Working with JSON Files

    Working with Parquet Files

    Spark Dataset

    What Next?

    Tutorial Not Found