concept RDD in category pyspark

This is an excerpt from Manning's book Data Analysis with Python and PySpark.
This book will focus mostly on the data frame as our main data structure. I’ll discuss about the RDD in Chapter 8, when we discuss about lower-level PySpark programming and how to embed our own Python functions in our programs.
This section covers the resilient distributed dataset (RDD) and how you use it to manipulate data. My goal is to provide a good overview of what an RDD is and how you manipulate data using Python functions. Besides being useful in itself, this section’s material is a perfect introduction to user-defined functions, the data frame’s answer to the RDD operating model. I cover user-defined functions in the following sections.
The RDD can be thought of as the ultimate flexible data container structure. You can cram pretty much anything that can be pickled (python’s way of serializing objects) into it. Where the data frame has good structure documentation though
Columnobjects, types, and schemas, the RDD does not force you into a specific layout. Each element is independent of the other. In listing 8.1, I create a list containing multiple objects of different types, then promote it to an RDD via theparallelizemethod. The resulting RDD is depicted in figure 8.1.
map()is the easiest one to figure out: it applies the function taken as a parameter to every element of the RDD. I try, and fail, to map a simple function that adds 1 to a value in listing 8.2. What’s happening?Listing 8.2. Mapping a simple function
add_one()to each element to an RDD.def add_one(value): return value + 1 #1 collection_rdd = collection_rdd.map(add_one) #2 print(collection_rdd.collect()) #3 # Stack trace galore! The important bit, you'll get one of the following: # TypeError: can only concatenate str (not "int") to str # TypeError: unsupported operand type(s) for +: 'dict' and 'int' # TypeError: can only concatenate tuple (not "int") to tupleTo understand why our code is failing, we’ll break down the mapping process, illustrated in figure 8.2. I apply the
add_one()function to each element in the RDD by passing it as an argument to themap()method.add_one()is a regular Python function, applied to regular Python objects. Since we have incompatible types (taking an example,"two" + 1is not a legal operation in Python), three of our elements areTypeError. When Icollect()the RDD to peek at the values, it explodes into a stack trace right in my REPL.
The RDD is a lazy collection. If you have an error in your function application, it will not be visible until you perform an action (such as
collect()), just like with the data frame.Figure 8.2. Applying the
add_one()function to each element of the RDD via map. If the function cannot be applied, an error will be raised during action time.![]()