QBoard » Big Data » Big Data - Spark » Difference between DataFrame, Dataset, and RDD in Spark

Difference between DataFrame, Dataset, and RDD in Spark

  • I'm just wondering what is the difference between an RDD and DataFrame (Spark 2.0.0 DataFrame is a mere type alias for Dataset[Row]) in Apache Spark?

    Can you convert one to the other?
      December 23, 2020 12:21 PM IST
    0
  • RDDs vs Dataframes vs Datasets

      RDDs Dataframes Datasets
    Data Representation RDD is a distributed collection of data elements without any schema. It is also the distributed collection organized into the named columns It is an extension of Dataframes with more features like type-safety and object-oriented interface.
    Optimization No in-built optimization engine for RDDs. Developers need to write the optimized code themselves. It uses a catalyst optimizer for optimization. It also uses a catalyst optimizer for optimization purposes.
    Projection of Schema Here, we need to define the schema manually. It will automatically find out the schema of the dataset. It will also automatically find out the schema of the dataset by using the SQL Engine.
    Aggregation Operation RDD is slower than both Dataframes and Datasets to perform simple operations like grouping the data. It provides an easy API to perform aggregation operations. It performs aggregation faster than both RDDs and Datasets. Dataset is faster than RDDs but a bit slower than Dataframes.

     

      December 11, 2021 3:21 PM IST
    0
  • Apache Spark provide three type of APIs

    RDD
    DataFrame
    Dataset


    Comparing RDD, Dataframe and Dataset APIs



    Here is the APIs comparison between RDD, Dataframe and Dataset.

    RDD
    The main abstraction Spark provides is a resilient distributed dataset (RDD), which is a collection of elements partitioned across the nodes of the cluster that can be operated on in parallel.

    RDD Features:-
    Distributed collection:
    RDD uses MapReduce operations which is widely adopted for processing and generating large datasets with a parallel, distributed algorithm on a cluster. It allows users to write parallel computations, using a set of high-level operators, without having to worry about work distribution and fault tolerance.

    Immutable: RDDs composed of a collection of records which are partitioned. A partition is a basic unit of parallelism in an RDD, and each partition is one logical division of data which is immutable and created through some transformations on existing partitions.Immutability helps to achieve consistency in computations.

    Fault tolerant: In a case of we lose some partition of RDD , we can replay the transformation on that partition in lineage to achieve the same computation, rather than doing data replication across multiple nodes.This characteristic is the biggest benefit of RDD because it saves a lot of efforts in data management and replication and thus achieves faster computations.

    Lazy evaluations: All transformations in Spark are lazy, in that they do not compute their results right away. Instead, they just remember the transformations applied to some base dataset . The transformations are only computed when an action requires a result to be returned to the driver program.

    Functional transformations: RDDs support two types of operations: transformations, which create a new dataset from an existing one, and actions, which return a value to the driver program after running a computation on the dataset.

    Data processing formats:
    It can easily and efficiently process data which is structured as well as unstructured data.

    Programming Languages supported:
    RDD API is available in Java, Scala, Python and R.

    RDD Limitations:-
    No inbuilt optimization engine: When working with structured data, RDDs cannot take advantages of Spark’s advanced optimizers including catalyst optimizer and Tungsten execution engine. Developers need to optimize each RDD based on its attributes.

    Handling structured data: Unlike Dataframe and datasets, RDDs don’t infer the schema of the ingested data and requires the user to specify it.

    Dataframes
    Spark introduced Dataframes in Spark 1.3 release. Dataframe overcomes the key challenges that RDDs had.

    A DataFrame is a distributed collection of data organized into named columns. It is conceptually equivalent to a table in a relational database or a R/Python Dataframe. Along with Dataframe, Spark also introduced catalyst optimizer, which leverages advanced programming features to build an extensible query optimizer.

    Dataframe Features:-
    Distributed collection of Row Object: A DataFrame is a distributed collection of data organized into named columns. It is conceptually equivalent to a table in a relational database, but with richer optimizations under the hood.

    Data Processing: Processing structured and unstructured data formats (Avro, CSV, elastic search, and Cassandra) and storage systems (HDFS, HIVE tables, MySQL, etc). It can read and write from all these various datasources.

    Optimization using catalyst optimizer: It powers both SQL queries and the DataFrame API. Dataframe use catalyst tree transformation framework in four phases,

    1.Analyzing a logical plan to resolve references
    2.Logical plan optimization
    3.Physical planning
    4.Code generation to compile parts of the query to Java bytecode.
    Hive Compatibility: Using Spark SQL, you can run unmodified Hive queries on your existing Hive warehouses. It reuses Hive frontend and MetaStore and gives you full compatibility with existing Hive data, queries, and UDFs.

    Tungsten: Tungsten provides a physical execution backend whichexplicitly manages memory and dynamically generates bytecode for expression evaluation.

    Programming Languages supported:
    Dataframe API is available in Java, Scala, Python, and R.

    Dataframe Limitations:-
    Compile-time type safety: As discussed, Dataframe API does not support compile time safety which limits you from manipulating data when the structure is not know. The following example works during compile time. However, you will get a Runtime exception when executing this code.


    Example:

    case class Person(name : String , age : Int)
    val dataframe = sqlContext.read.json("people.json")
    dataframe.filter("salary > 10000").show
    => throws Exception : cannot resolve 'salary' given input age , name
    This is challenging specially when you are working with several transformation and aggregation steps.

    Cannot operate on domain Object (lost domain object): Once you have transformed a domain object into dataframe, you cannot regenerate it from it. In the following example, once we have create personDF from personRDD, we won’t be recover the original RDD of Person class (RDD[Person]).


    Example:

    case class Person(name : String , age : Int)
    val personRDD = sc.makeRDD(Seq(Person("A",10),Person("B",20)))
    val personDF = sqlContext.createDataframe(personRDD)
    personDF.rdd // returns RDD[Row] , does not returns RDD[Person]
    Datasets API
    Dataset API is an extension to DataFrames that provides a type-safe, object-oriented programming interface. It is a strongly-typed, immutable collection of objects that are mapped to a relational schema.

    At the core of the Dataset, API is a new concept called an encoder, which is responsible for converting between JVM objects and tabular representation. The tabular representation is stored using Spark internal Tungsten binary format, allowing for operations on serialized data and improved memory utilization. Spark 1.6 comes with support for automatically generating encoders for a wide variety of types, including primitive types (e.g. String, Integer, Long), Scala case classes, and Java Beans.

    Dataset Features:-
    Provides best of both RDD and Dataframe: RDD(functional programming, type safe), DataFrame (relational model, Query optimazation , Tungsten execution, sorting and shuffling)

    Encoders: With the use of Encoders, it is easy to convert any JVM object into a Dataset, allowing users to work with both structured and unstructured data unlike Dataframe.

    Programming Languages supported: Datasets API is currently only available in Scala and Java. Python and R are currently not supported in version 1.6. Python support is slated for version 2.0.

    Type Safety: Datasets API provides compile time safety which was not available in Dataframes. In the example below, we can see how Dataset can operate on domain objects with compile lambda functions.

    Example:

    case class Person(name : String , age : Int)
    val personRDD = sc.makeRDD(Seq(Person("A",10),Person("B",20)))
    val personDF = sqlContext.createDataframe(personRDD)
    val ds:Dataset[Person] = personDF.as[Person]
    ds.filter(p => p.age > 25)
    ds.filter(p => p.salary > 25)
    // error : value salary is not a member of person
    ds.rdd // returns RDD[Person]


    Interoperable: Datasets allows you to easily convert your existing RDDs and Dataframes into datasets without boilerplate code.


    Datasets API Limitation:-
    Requires type casting to String: Querying the data from datasets currently requires us to specify the fields in the class as a string. Once we have queried the data, we are forced to cast column to the required data type. On the other hand, if we use map operation on Datasets, it will not use Catalyst optimizer.

      August 11, 2021 1:28 PM IST
    0
  • A DataFrame is defined well with a google search for "DataFrame definition":

    A data frame is a table, or two-dimensional array-like structure, in which each column contains measurements on one variable, and each row contains one case.

    So, a DataFrame has additional metadata due to its tabular format, which allows Spark to run certain optimizations on the finalized query.

    An RDD, on the other hand, is merely a Resilient Distributed Dataset that is more of a blackbox of data that cannot be optimized as the operations that can be performed against it, are not as constrained.

    However, you can go from a DataFrame to an RDD via its rdd method, and you can go from an RDD to a DataFrame (if the RDD is in a tabular format) via the toDF method

    In general it is recommended to use a DataFrame where possible due to the built in query optimization.

    This post was edited by Viaan Prakash at December 29, 2020 12:24 PM IST
      December 29, 2020 12:23 PM IST
    0