DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Apache Spark 4.0: Transforming Big Data Analytics to the Next Level
  • Spark Job Optimization
  • All You Need to Know About Apache Spark
  • Iceberg Catalogs: A Guide for Data Engineers

Trending

  • 12 Principles for Better Software Engineering
  • How My AI Agents Learned to Talk to Each Other With A2A
  • Cloud Hardware Diagnostics for AI Workloads
  • Top 5 Trends in Big Data Quality and Governance in 2025
  1. DZone
  2. Data Engineering
  3. Big Data
  4. Apache Spark: 3 Reasons Why You Should Not Use RDDs

Apache Spark: 3 Reasons Why You Should Not Use RDDs

When it comes to building an enterprise-grade app in Spark, RDD isn't a good choice. Why? And if RDD is not a good choice, then what should we use?

By 
Himanshu Gupta user avatar
Himanshu Gupta
·
Mar. 08, 18 · Opinion
Likes (11)
Comment
Save
Tweet
Share
46.3K Views

Join the DZone community and get the full member experience.

Join For Free

Apache Spark: whenever we hear these two words, the first thing that comes to our mind is RDDs, (resilient distributed datasets). Now, it has been more than five years since Apache Spark came into existence and after its arrival, a lot of things changed in the big data industry. The major change was dethroning of Hadoop MapReduce. Spark literally replaced MapReduce because of the easy-to-use API in Spark, lesser operational cost due to efficient use of resources, compatibility with a lot of existing technologies like YARN/Mesos, fault tolerance, and security.

Due to these reasons, a lot of organizations have migrated their big data applications to Spark and the first thing they learn is how to use RDDs. This makes sense, as RDD is the building block of Spark and the whole idea of Spark is based on RDD. Also, it is the perfect replacement for MapReduce. So, whoever wants to learn Spark should know about RDDs.

But when it comes to building an enterprise-grade application based on Spark, RDD isn't a good choice. Why? You will get to know that when you will read the reasons given below. If RDD is not a good choice, then what should we use? The obvious answer is DataFrames/Datasets.

Now, let's come to reasons for not using RDDs:

1. Outdated

Yes! You read it right: RDDs are outdated. And the reason behind it is that as Spark became mature, it started adding features that were more desirable by industries like data warehousing, big data analytics, and data science.

In order to fulfill the needs of these industries, Spark has come up with a solution that can work like a silver bullet and solve the problem of being fit for all sorts of industries.

To do that, it introduced DataFrames and Datasets, distributed collections of data with the benefits of Spark SQL's optimized execution engine. We'll get to what Spark SQL's optimized execution is later on, but for now, we know that Spark has come up with two new types of data structures that have more benefits than RDD.

2. Hard to Use

The next reason to not use RDD is the API it provides. Most of the operations like counting, grouping, etc. are pretty straightforward and easy-to-use APIs functions are built-in. But when it comes to operations like aggregation or finding averages, it becomes really hard to code using RDD.

For example, say we have a text file and we want to find out the average frequency of all the words in it.

First, let's code it using RDD:

val linesRDD = sparkContext.textFile("file.txt")
val wordCountRDD = linesRDD.flatMap(_.split(" ")).map((_, 1)).reduceByKey(_ + _)

val (word, (sum, n)) = wordCountRDD.map { case (word, count) => (word, (count, 1)) } .reduce { case ((word1, (count1, n1)), (word2, (count2, n2))) => ("", (count1 + count2, n1 + n2)) }

val average = sum.toDouble / n

Now, let's try to solve the same problem using DataFrames:

val linesDF = sparkContext.textFile("file.txt").toDF("line")
val wordsDF = linesDF.explode("line", "word")((line: String) => line.split(" "))
val wordCountDF = wordsDF.groupBy("word").count()
val average = wordCountDF.agg(avg("count"))

The difference between the two solutions is clear. The first will definitely take you some time to understand what the developer is trying to do. But the second one is pretty straightforward and anyone who knows SQL will understand it in one go.

So, we saw that RDDs can sometimes be tough to use if the problem at hand is like the one above.

3. Slow Speed

Last, but not least, a reason to not use RDD is its performance, which can be a major issue for some applications. Since this is an important reason, we will take a closer look at it.

For example, say we have 100M numbers to be counted. Now, 100M doesn't seem to be a big number when we talk about big data, but the important thing here to notice will be the difference in speed of the DataFrame/Dataset and RDD. Now, let's see the example:

scala> val ds = spark.range(100000000)
ds: org.apache.spark.sql.Dataset[Long] = [id: bigint]

When I ran ds.count, it gave a result of, of course, 100,000,000, in about 0.2s (on a 4 core/8 GB machine). Also, the DAG created is as follows:

On the other hand, when I ran ds.rdd.count, which first converts the Dataset into RDD and then run a count on it, then it gave me the result in about 4s (on the same machine). Also, the DAG it creates is different:

Looking at the results and DAGs above, two questions will definitely arise in your mind:

  1. Why is ds.rdd.count is creating only one stage whereas ds.count created two stages?
  2. Why is ds.rdd.count slower than ds.count even though ds.rdd.count only has one stage to execute?

The answer to these questions is as follows:

  1. Both the counts are effectively two-step operations. The difference is that with ds.count, the final aggregation is performed by one of the executors, while ds.rdd.count aggregates the final result on the drive. Therefore, this step is not reflected in the DAG.
  2. ds.rdd.count has to initialize (and later garbage collect) 100 million row objects, which is a costly operation and accounts for the majority of the time difference between the two operations.

So, in conclusion, avoid RDDs wherever you can and use DataFrames/Datasets instead. But this doesn't mean that we shouldn't learn RDDs at all. After all, they are the building blocks of Spark and they can't be ignored while learning Spark. But we should avoid them in our applications.

I hope you found this post interesting and now, you will be able to make others believe they should not use RDDs anymore.

This article was first published on the Knoldus blog.

Apache Spark Big data

Published at DZone with permission of Himanshu Gupta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Apache Spark 4.0: Transforming Big Data Analytics to the Next Level
  • Spark Job Optimization
  • All You Need to Know About Apache Spark
  • Iceberg Catalogs: A Guide for Data Engineers

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: