Introduction
Apache Spark has brought significant innovation to Big Data computing, but its results are even more extraordinary when paired with Alluxio. Alluxio, provides Spark with a reliable data sharing layer, enabling Spark to excel at performing application logic while Alluxio handles storage. Bazaarvoice uses the combination of Spark and Alluxio to provide a real-time big data platform that has the ability to not only handle the intake of 1.5 billion page views during peak events like Black Friday, but also provide real-time analytics against it (read more). At this scale, the gain in speed is an enabler for new workloads. We’ve established a clean and simple way to integrate Alluxio and Spark.
This blog is for those who are new to and interested in how to leverage Alluxio with Spark, and all of the examples will be reproducible on your local machine with simple steps.
Getting Started with Apache Spark Caching
To get started with Alluxio and Spark, you will first need to download a distribution for the two systems, install Java 8 and download sample data to work through the exercises.
Download Alluxio and Spark to a working directory. The sample data files can be downloaded and unpacked there as well or in /tmp
, the full path to the sample files must be referenced from Spark.
Setting up Alluxio
- Turn on remote login service so that
ssh localhost
can succeed.
- To avoid the need to repeatedly input the password, you can add the public SSH key for the host into
~/.ssh/authorized_keys
. See this tutorial for more details.
Download, extract and start a precompiled release of Alluxio from the Alluxio website
$ tar -zxf alluxio-1.8.1-bin.tar.gz
$ cd alluxio-1.8.1
$ bin/alluxio bootstrap-conf localhost
$ bin/alluxio-start.sh local -f
Verify that the Alluxio system is running by visiting the web UI at localhost:19999/home.
Setting up Spark
Extract the precompiled release of Spark
$ tar -zxf spark-2.4.0-bin-hadoop2.7.tgz
$ cd spark-2.4.0-bin-hadoop2.7
Start the spark-shell
program by running the following command from the Spark project directory. In the interactive shell you can manipulate data from various sources, in this case, it will be the local file system.
Integrating Alluxio and Spark for Caching
Spark needs an Alluxio client jar which let’s Spark programs talk with Alluxio; this is the point of integration between the two systems. Specify it with the --driver-class-path
parameter followed by the path to the client jar which is located in the client directory of the Alluxio package.
$ cd spark-2.4.0-bin-hadoop2.7
$ bin/spark-shell --driver-class-path <PATH>/alluxio-1.8.1/client/alluxio-1.8.1-client.jar
Running a Simple Example
As a first example, we will run Alluxio with Spark reading data from local storage to get familiar with the integration between the two systems. The sample data file that was downloaded earlier contains files filled with randomly generated words from an English dictionary 2gb in size.
$ cd /tmp
$ tar -zxf sample-2g.tar.gz
Process the 2gb sample file in Spark and count how many lines are in it. Make sure to specify the correct path.
scala> val file = sc.textFile("/tmp/sample-2g")
scala> file.count()
Alluxio can also be used as a source or sink for your data. You can save the file to Alluxio and similarly run the same operations on the data from Alluxio, as you would for your local file system.
scala> file.saveAsTextFile("alluxio://localhost:19998/sample-2g")
scala> val alluxioFile = sc.textFile("alluxio://localhost:19998/sample-2g")
scala> alluxioFile.count()
Conclusion
This is an introduction to using Alluxio with Spark. Our next blog will go into additional details around the benefits of leveraging Alluxio with Spark:
- Data sharing between multiple jobs: Only one job needs to incur the slow read from cold data
- Resilience against job failures: Data is preserved in Alluxio across job failures or restarts
- Managed storage: Alluxio optimizes the utilization of allocated storage across applications