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.

So far only Ikt is supported.

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.3.1 /_/

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.

You should have the following in your .bashrc:

export JAVA_HOME='/com/local/java/' export PATH="$JAVA_HOME/bin:$PATH" export SPARK_HOME='/com/local/spark' export PATH="$SPARK_HOME/bin":$PATH export PYTHONPATH="$SPARK_HOME/python:"$PYTHONPATH

export TMPDIR="/com/users/[YOU]/tmpdir"

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 itk here: /com/users/nathante/mediawiki_dump_tools/wikiq_users/wikiq_users_spark.py

#!/usr/bin/env python3 import sys from pyspark import SparkConf from pyspark.sql import SparkSession, SQLContext from pyspark.sql import Window import pyspark.sql.functions as f   from pyspark.sql import types import argparse import glob from os import mkdir from os import path

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.

def parse_args: parser = argparse.ArgumentParser(description='Create a dataset of edits by user.') parser.add_argument('-i', '--input-file', help='Tsv file of wiki edits. Supports wildcards ', required=True, type=str) parser.add_argument('-o', '--output-dir', help='Output directory', default='./output', type=str) parser.add_argument('--output-format', help = "[csv, parquet] format to output",type=str) parser.add_argument('--num-partitions', help = "number of partitions to output",type=int, default=1) args = parser.parse_args return(args)

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

if __name__ == "__main__": conf = SparkConf.setAppName("Wiki Users Spark") spark = SparkSession.builder.getOrCreate

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.

args = parse_args files = glob.glob(args.input_file) files = [path.abspath(p) for p in files] 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.

reader = spark.read

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.

# build a schema struct = types.StructType.add("anon",types.StringType,True) struct = struct.add("articleid",types.LongType,True) struct = struct.add("date_time",types.TimestampType, True) struct = struct.add("deleted",types.BooleanType, True) struct = struct.add("editor",types.StringType,True) struct = struct.add("editor_id",types.LongType, True) struct = struct.add("minor", types.BooleanType, True) struct = struct.add("namespace", types.LongType, True) struct = struct.add("revert", types.BooleanType, True) struct = struct.add("reverteds", types.StringType, True) struct = struct.add("revid", types.LongType, True) struct = struct.add("sha1", types.StringType, True) struct = struct.add("text_chars", types.LongType, True) struct = struct.add("title",types.StringType, True)

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.

df = reader.csv(files,                   sep='\t',                    inferSchema=False,                    header=True,                    mode="PERMISSIVE",                    schema = struct)

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.

df = df.repartition(args.num_partitions)

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.

# replace na editor ids df = df.select('*',f.coalesce(df['editor_id'],df['editor']).alias('editor_id_or_ip'))

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.

# assign which edit reverted what edit reverteds_df = df.filter(~ df.reverteds.isNull).select(['revid','reverteds'])

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

reverteds_df = reverteds_df.select("*", f.split(reverteds_df.reverteds,',').alias("reverteds_new"))

The above line converts  from a string to an array.

reverteds_df = reverteds_df.drop("reverteds") reverteds_df = reverteds_df.withColumnRenamed("reverteds_new", "reverteds")

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.

reverteds_df = reverteds_df.select(reverteds_df.revid.alias('reverted_by'),                                      f.explode(reverteds_df.reverteds).alias('reverted_id'))

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.

df = df.join(reverteds_df, df.revid == reverteds_df.reverted_id, how='left_outer') df.drop("reverted_id") del(reverteds_df)

Join the two tables so that each revision that was reverted gets a value for. 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.

# sort by datetime df = df.orderBy(df.date_time.asc)

sorts the dataframe by date.

win = Window.orderBy('date_time').partitionBy('editor_id_or_ip')

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.

# count reverts reverts_df = df.filter(df.revert==True).select(['revid','editor_id_or_ip','date_time','revert'])

The above creates a new table that only has reverts.

reverts_df = reverts_df.withColumn('editor_nth_revert',f.rank.over(win))

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.

df = df.join(reverts_df, ["revid",'editor_id_or_ip','date_time','revert'], how='left_outer') del(reverts_df)

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

# count edits df = df.withColumn('year', f.year(df.date_time)) df = df.withColumn('month',f.month(df.date_time))

Using  again to illustrate creating some calendar variables from the.

df = df.withColumn('editor_nth_edit',f.rank.over(win))

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

# output if not path.exists(args.output_dir): mkdir(args.output_dir) if args.output_format == "csv" or args.output_format == "tsv": df.write.csv(args.output_dir, sep='\t', mode='overwrite',header=True,timestampFormat="yyyy-MM-dd HH:mm:ss")

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.

# format == "parquet" else: df.write.parquet(args.output_dir, mode='overwrite')

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: get_spark_nodes.sh 4

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

get_spark_nodes.sh 2

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

This will setup the cluster. 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.

spark-submit --master spark://$SPARK_MASTER:18899 your_script.py [Arguments to your script here].

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

spark-submit --master spark://$SPARK_MASTER:18899 wikiq_users_spark.py --output-format tsv  -i "/com/output/wikiq-enwiki-20180301/enwiki-20180301-pages-meta-history*.tsv" -o  "/com/output/wikiq-users-enwiki-20180301-tsv/" --num-partitions 500

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

$SPARK_HOME/sbin/stop-all.sh

Monitoring the cluster
From a login node (itk): ssh -L localhost:8989:localhost:8989 $SPARK_MASTER -N -f && ssh -L localhost:4040:localhost:4040 $SPARK_MASTER -n -F

From your laptop: ssh -L localhost:8989:localhost:8989 itk -N -f && ssh -L localhost:4040:localhost:4040 itk -n -F

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

JAVA_HOME=/home/you/Oracle_JDK/ PATH=$JAVA_HOME/bin:$PATH

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:

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

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

Add to your .bashrc: export PYTHONPATH=$SPARK_HOME/python/:$PYTHONPATH