In the last post, we learned the basics of Spark RDDs. Now, we will have a look at data transformation in Apache Spark. One data transformation was already used in the last tutorial: filter. We will have a look at this now as the first transformation. But before we start, we need to have a look at one major thing: lambda expressions. Each of the functions we use for data transformations in Spark take functions in the form of a lambda expression. In our last tutorial, we filtered data based on a lambda expression. Let’s recall the code to have a deeper look into it:
sp_pos = spark_data.filter(lambda x: x>0.0).collect()
What is crucial for us is the statement that is in the braces “()” after the filter command. In there, we see the following: “lamdba x: x>0.0”. Bascially, with “lambda x” we state that the following evaluation should be applied to all items in the dataset. For each iteration, we use “x” as the variable. So it reads like: “apply the evaluation, if the variable is greater than 0, to all items in our dataset”. If x would be a complex dataset, we could also use it’s fields and methods. But in our case, it is a simple number.
One more thing that is important for transformations: transformations in Spark are always “lazy” bound to it’s execution. So calling the filter function does nothing, until you call an execution on it. The one we used above is “.collect()”. Collect() in our case calls the filter.
Filtering in data is one of the most frequent used transformations. The filter criteria is parsed as a lambda expression. You can also chain different filter criteria easily, since we have late binding on it.
We extend the above sample by only showing numbers that are smaller than 3.0. The code for that looks the following:
sp_pos = spark_data.filter(lambda x: x>0.0).filter(lambda y: y<3.0).collect() sp_pos
Another important transformation is sorting data. At some point, you want to arrange the data in either ascending or descending order. This is done with the “sortBy” function. The “sortBy” function takes a lambda expression that takes the field to filter. In our above example, this isn’t relevant since we only have one item per RDD. Let’s have a look at how to use it with our dataset:
sp_sorted = spark_data.sortBy(lambda x: x).collect() sp_sorted
Now, if we want to filter it in the opposite order, we can set the optional “ascending” keyword to false. Let’s have a look at this:
sp_sorted = spark_data.sortBy(lambda x: x, False).collect() sp_sorted
Often, it is necessary to join two different datasets together. This is done by the “Join” function. To use the join, we need to create new datasets first (and we will need them further on as well):
ds_one = sc.parallelize([("Mark", 1984), ("Lisa", 1985)]) ds_two = sc.parallelize([("Mark", 2015), ("Anastasia", 2017)]) sorted(ds_one.join(ds_two).collect())
You can also use the inner or outer join on RDDs.
Today, we looked at some basic data transformation in Spark. Over the next couple of tutorial posts, I will walk you through more of them.
There is of course much more to learn about Spark, so make sure to read the entire Apache Spark Tutorial. I regularly update this tutorial with new content. Also, I created several other tutorials, such as the Machine Learning Tutorial and the Python for Spark Tutorial. The official Apache Spark page can intensify your experience. Your learning journey can still continue.