CommunityData:Hyak Spark

Apache Spark is a powerful system for writing programs that deal with large datasets. The most likely reason for using Spark on Hyak is that you run into memory limitations when building variables. For example suppose you want to compute:


 * The number of prior edits a Wikipedia editor has made, for every editor and every edit.
 * The number of views for every Wikipedia page for every month and every edit.
 * The number of times every Reddit user has commented on every thread on every subreddit for every week.

You might try writing a program to build these variables using a data science tool like pandas in Python or data.table, or plyr in R. These common data science tools are powerful, expressive, and fast, but do not work when data does not fit in memory. When a table does not fit in memory, but the computation you want to do only requires operating on one row at a time (such as in a simple transformation or aggregation), you can often work around this limitation by writing a simple custom program that operates in a streaming fashion. However, when computation cannot be done one row at a time, such as in a sort, group by, or join a streaming solution will not work. In this case your options are limited. One option is writing bespoke code to perform the required operations and building variables. However, this can be technically challenging and time consuming work. Moreover, your eventual solution is likely to be relatively slow and difficult to extend or maintain compared to a solution build using Spark. A number of us (Nate, Jeremy, Kaylea) have all at some point written bespoke code for computing user-level variables on Wikipedia data. The infamous "million file problem" is a result from abusing the filesystem to perform a massive group by.

This page will help you decide if you should use Spark on Hyak for your problem and provide instructions on how to get started.

Pros and Cons of Spark
The main advantages of Spark on Hyak:


 * 1) Work with "big data" without ever running out of memory.
 * 2) You get very good parallelism for free.
 * 3) Distribute computational work across many hyak nodes so your programs run faster.
 * 4) Common database operations (select, join, groupby, filter) are pretty easy.
 * 5) Spark supports common statistical and analytical tasks (stratified sampling, summary and pairwise statistics, common and simple models).
 * 6) Spark is a trendy technology that lots of people know or want to learn.

The main disadvantages of Spark are


 * 1) It takes several steps to get the cluster up and running.
 * 2) The programming paradigm is not super intuitive, especially if you are not familiar with SQL databases or lazy evaluation.
 * 3) Doing more advanced things requires programming in Scala.

= Getting Started with Spark =

Spark on Hyak
If you are already set up on Hyak following the instructions on [CommunityData:Hyak] then you should already have a working spark installation on Hyak. Test this by running pyspark

from a hyak cluster node (directly on the login node will give you an insufficient memory error).

You should see this:

Welcome to         ____              __ / __/__ ___ _____/ /__        _\ \/ _ \/ _ `/ __/  '_/       /__ / .__/\_,_/_/ /_/\_\   version 2.4.4 /_/

If so then you are ready to start running Spark programs on a single node. If you don't see this, then you need to check your,  ,  , and   environment variables.

If you are using the cdsc mox setup then you should have a working spark configuration in your environment already. Otherwise, you'll need to have the following in your .bashrc (remember to source .bashrc or re-login to load up these changes):

You can also run spark programs on many nodes, but this requires additional steps. These are described below.

Spark Walkthrough
Spark programs is somewhat different from normal python programming. This section will walk you through a script to help you learn how to work with Spark. You may find this script useful as a template for building variables on top of wikiq data.

This section presents a pyspark program that
 * 1) Reads wikiq tsvs
 * 2) Computes the nth edit for each editor
 * 3) For edits that were reverted, identify the edit that made the revert.
 * 4) Output tsvs with the new variables.

The script is on ikt here: /com/users/nathante/mediawiki_dump_tools/wikiq_users/wikiq_users_spark.py

This part imports some python utilities that we will use. You can pretty safely treat the, the   and the   imports as magic that creates a spark environment that supports working with Spark's SQL features. is used to create Window functions. We will use a window function to count the nth edit made by each editor. provides built in functions that can be applied to data in spark data frames. are data types that we will use to specify the scheme for reading wikiq files.

Above is just a function to build a command line interface.

Now we are in the main function of the script. The above two lines complete setting up spark. If you are going to run this program on a multi-node cluster, then it would be nice to set the AppName to something friendly. This will be used by the job monitoring tools.

Spark is designed to read and write lists of files. The  uses   to accept wildcards. The above lines build a list of files from the argument.

This creates a reader object that can read files. We are starting to get down to business. Next we will specify the schema for the files that we will read in. This is important so that spark can run efficiently and operate on the correct data types.

This is a little bit tedious, but it is necessary for Spark to work effectively on tsv data. If you are reading binary format such as Parquet (which is recommended, and easy to create using Spark) then you can skip this.

This reads the data into a Spark Dataframe. Spark Dataframes are more like SQL tables than they are like pandas DataFrames. Spark Dataframes are pretty abstract and can live on memory or on disk. Operations on Spark Dataframes are lazily evaluated, Spark will not actually run computations on your data until it has to. Calling  will print the dataframe and trigger execution. stops Spark from giving up if it hits malformed rows.

The first thing to do after reading the data is to  the data. This determines the number of files that spark will output. Choosing the right number of partitions isn't really an exact science. Having more partitions makes some operations more efficient and can make other operations slower. The rule of thumb is that the number of partitions increases linearly with the amount of data. 500 partitions seems pretty good for English Wikipedia. If you are interested this page is good. Now we are ready to build some variables. The first thing we are going to do is to create a new column.

The first argument to select  causes select to return all the columns. Next we call the  function which creates a new column with the value of   if   is not null and the value of   if   is null. The call to  gives the new column a name. If you are familiar with SQL programming, this might seem familiar. You could write it as.

Next we are going to identify the edits that revert each edit. lists the edits that the edit has reverted.

This line creates a new Spark Dataframe out of the rows of the first dataframe that have a value for  with the columns   and.

The above line converts  from a string to an array.

The above two lines remove the old "reverteds" column, which was a string, and replaces it with the array column. This is required because unlike pandas, Spark dataframes do not have a column assignment syntax.

The most important part of the above is the function call to. Explode💥 unfolds the array so that we get one line for each element of the array. Now we can join  with   to put the   column in.

Join the two tables so that each revision that was reverted gets a value for. There are many kinds of joins and there is some detail on this in the Join help section of this page. The join is a  join so we keep all the rows of   even the rows that don't have a value for   in. We remove the redundent  column and are don with building. Next we add a column that counts the number of times a given editor has made a revert (this is called a cumulative count). Since we aren't going to use  again we can call. This tells spark it is free to remove the object from storage and can improve performance.

sorts the dataframe by date.

The above defines a, which is a kind of object that can be used to define rolling aggregations. We are going to use the  function to perform the cumulative count, and   requires a. The WindowSpec that we made says that we are grouping at the level of  and that we want to operate on each row of each group in chronological order.

The above creates a new table that only has reverts.

This applies the  function over the window to perform the cumulative count of the reverts. The  function adds a new column to the dataframe called.

Above we perform the join to add the new column to. We join on all of the columns  so that duplicate columns are not created in the

Using  again to illustrate creating some calendar variables from the.

We can reuse the  to get the cumulative count for all edits as opposed to all reverts.

Instead of writing our output to a single file, we output to a directory. Spark will write 1 file for each partition to the directory.

It is also easy to write to parquet.

Starting a Spark cluster with many nodes on Hyak
It is pretty easy to start up a multiple node cluster on Hyak.

If you have  in your   then you should be able to run:

To checkout 4 nodes that can be used as a Spark cluster. The spark cluster will have 4 worker nodes, one of these is also the "master" node. When you run  you will be routed to the machine that will become the master. If you only want 2 nodes just do

After you get the nodes and have a shell on the master node run

This will setup the cluster. Make sure you start up the cluster from the same session you used to get_spark_nodes -- otherwise, the startup script doesn't have access to the assigned node list and will fail. Take note of the node that is assigned to be the master, and use that information to set your $SPARK_MASTER environment variable, for example. The program  submits your script to the running Spark cluster.

For example, we can submit the script we used in the walkthrough as:

When you have a spark cluster running, it will serve some nice monitoring tools on ports 8989 and 4040 of the master. You can build an ssh tunnel between your laptop and these nodes to monitor the progress of your spark jobs.

When you are done with the cluster, you should shut it down using the script in

Monitoring the cluster
From a login node (hyak):

From your laptop:

Point your browser to localhost:8989 to see the cluster status and to localhost:4040 to monitor jobs.

Setting up Spark on your laptop
You might want to have a working stand alone Spark installation on your laptop. You should develop your Spark code on your laptop before running on hyak. To get spark working on your laptop you need to first install the Oracle Java Development Toolkit (Oracle JDK) and then install Spark.

Installing Java
To install java, all that should be required is to download and unzip the software and then set the  environment variable and add the java programs to your. We have Java 8 on Hyak.


 * 1) Download the java jdk appropriate for your Operating System from here.
 * 2) Unpack the archive where you want, for example.
 * 3) Edit your environment variables (i.e. in your .bashrc) to

Installing Spark
Now we can install spark.


 * 1) Download the latest version of spark 'prebuilt for Apache Hadoop 2.7 or later'. From here
 * 2) Extract the archive. (i.e. to /home/you/spark)
 * 3) Set the   environment variable and update your path. I.e. update your .bashrc to set:.
 * 4) Test your spark install by running

You should see this:

For working with Python you also want to add pyspark (Spark Python bindings) to your $PYTHONPATH.

Add to your .bashrc:

= Tips, Recipes, and Resources =

To gain access to various useful SparkContext functions, you need to instantiate a pointer to the context which encloses your session. It seems to be common for Spark users to call this pointer sc, e.g. after you do

add a line like

and then you can use sc to access the functions described here:.

To create an empty dataframe
One way to create an empty dataframe is to generate a schema as in the example script, and then pass the schema into the create method, with an empty RDD object as data.

Pyspark string slicing seems to be non-pythonic

 * 1) Strings begin at 1, not 0. [0:5] will yield the same results as [1:5].
 * [x:y] will give you "give me y total characters, starting with the char in position x."

So, given a column in articleDF called timestamp, with contents like.... 20150701000000

You can access 2015 with

And to get 07:

When Reading In Multiple Files with Different Schemas
Make sure you re-instantiate your reader object, e.g.

when changing to a new file. The reader may cache the schema of the previous file and fail to detect the new schema. To make sure you have what you're expecting, try a

This will get you the same behavior as pandas print(yourDataset.head) -- 20 rows, nicely formatted in your stdout.

Note that calling show, while filtered to show 20 rows, still causes all steps of the job to execute, and can take a long time.

Getting Help & Useful Links
Getting help with Spark in the usual fora such as Stack Exchange or even a straight-up Google search seems to be a less effective strategy for pyspark than it is for normal python queries and errors. Specifying pyspark in your search terms helps in getting only Python answers, but debugging an error may require looking at Java documentation, and some online recipe blogs speak of writing code "in Spark", so presumably the console language.

The apache spark site at https://spark.apache.org/ is useful but not all of the example code is localized to python.

Join help
There's some good info about joins here: http://www.learnbymarketing.com/1100/pyspark-joins-by-example/

Java Errors and Responses
When I got:

spark java.io.IOException: No subfolder can be created in.

the University of Google told me it was a disk space issue. But shutting down the cluster and restarting it solved the problem -- maybe it hadn't shut down cleanly the last time someone used spark.

Slurm kills my job! Encountering Memory Limits
In theory, Spark enables you to run computations on data of any size without memory limitations. In practice, memory management issues occur. We are trying to understand these issues and to learn how to write Spark scripts that don't overuse memory.

Things to try if you run out of memory

 * 1) The 'out of memory' may be ephemeral or due to memory management issues in some layer other than your code -- try starting up a new cluster and running the same job unchanged.
 * 2) Repartition your data: increasing the number of partitions should make it easier for the Spark scheduler to avoid exceeding its memory limits.
 * 3) Increase the number of nodes. This can solve the problem essentially by giving Spark more Ram to work with.
 * 4) Be careful moving data out of distributed Spark objects into normal python objects. Work your way up from a small sample to a larger sample.
 * 5) You might try tweaking memory management options in   and <$SPARK_HOME/conf/spark-defaults.conf . Decreasing the number of executors, and the total memory allocated to executors should make Spark more resilient at the cost of performance.

Errors While Starting Cluster
Sometimes I get errors like this:

n0650: failed to launch: nice -n 0 /com/local/spark/bin/spark-class org.apache.spark.deploy.worker.Worker --webui-port 8081 spark://n0649:18899

Usually it seems to happen if I relinquish my spark cluster (whether I use the kill script or not) and then immediately restart one. The error goes away if I shut down and wait a minute or two before re-launching; my assumption is that there's some hygienic work being done behind the scenes that the scheduler doesn't know about and I need to let that finish.

And sometimes I get errors like this:

scontrol: error: host list is empty

when trying to launch spark. This means I'm in a session that doesn't know what nodes are assigned to the spark cluster. This will launch a dysfunctional cluster. Stop-all and then start_spark_cluster from the same session you landed in when you ran get_spark_nodes.

When I get errors about full logs, I go in and clean up the temporary files folder it refers to.