Getting Started with Instaclustr Spark & Cassandra

Menu

The rest of this tutorial will walk your through options to submit jobs to your Spark cluster. If you choose to provision your cluster with Zeppelin, you will be able to quickly write some Spark job via Zeppelin interface, available through Instaclustr console. You will find more details in this article.

The high-level steps in this tutorial are:

  1. Provision a cluster with Cassandra and Spark
  2. Set up a Spark Client
  3. Configure Client Network Access
  4. Basic Interaction with Spark Shell
  5. Using Spark SQL from Spark Shell
  6. Creating and Submitting a Scala Job
  7. Conclusion

This tutorial assumes that you are familiar with launching and connecting to servers in AWS.

While this tutorial is specific to AWS, we also support Spark on Azure and IBM SoftLayer. You can follow a similar approach to set up on those platforms or contact support@instaclustr.com if you need more detailed instructions.

Provision a cluster with Cassandra and Spark

  1. If you haven’t already signed up for an Instaclustr account, refer our support article to sign up and create an account.
  2. Once you have signed up for Instaclustr and verified your email, log in to the Instaclustr console and click the Create Cluster button.
  3. On the Create Cluster page, enter an appropriate name for your cluster. Under Applications section, select:
    • Apache Cassandra 3.11.2.2
    • Apache Spark as an Add-on (Apache Spark 2.1.1 – Hadoop 2.6)
  4. Under Data Centre section, select:
    • Amazon Web Services as the Infrastructure Provider
    • A minimum node size of t2.medium

  5. Under Cassandra Options section, select:
    • Use Private IP Addresses for node broadcast
    • Do not enable client encryption for Cassandra (see this article if you want to use Spark with Cassandra client to server encryption).

  6. Leave the other options as default. Accept the terms and conditions and click Create Cluster button. The cluster will automatically provision and will be available for use once all nodes are in the running state.

Set Up a Spark Client

To use our Spark cluster, you will need a client machine setup to submit jobs. Use the following steps to set up a client in AWS:

  1. Provision a new AWS server with the following configuration:
    • Region: same as your newly created Cassandra and Spark cluster
    • VPC: if possible, use a VPC with DNS resolution and DNS hostname enabled (Otherwise, refer step 7 below). The VPC network range should not overlap with the network range of your instaclustr cluster.
    • AMI: Ubuntu Server 14.04 LTS (HVM), SSD Volume Type as the AMI
    • Size: t2.small is sufficient for this tutorial and sufficient for many use-cases ongoing
  2. ssh to the newly launched server with ubuntu as username.
  3. Download the spark version matching your instaclustr version. In this case, Spark 2.1.1:
  4. Extract the Spark files:
  5. Download the Spark Cassandra assembly Jar (this is a fat Jar built by Instaclustr to include all required dependencies, to be used for spark shell). The latest version available for your spark version should be accessible via the Connection Info page of Instaclustr console.
  6. Install the Java Development Kit:
  7. If you are not using a VPC with DNS resolution and DNS hostname enabled, you will need to change the hostname of the client to the IP so that it resolves when used by Spark (a bit of a hack – the right way is to edit /etc/hosts but this is quicker):
  8. If you will be building the final scala example, then install sbt:

Configure Client Network Access

As Spark has minimal security, we recommend that you access Spark from a peered VPC in AWS to increase the security of network-based access rules. To set up the peered VPC and allow connections from your VPC to the cluster, follow our support article on Using VPC Peering AWS.

Note: When following the VPC Peering instructions, you must add your VPC network range to the Spark Allowed Addresses and the Cassandra Allowed Addresses. The Spark driver on your client machine needs to be able to connect to Cassandra as well as the Spark workers (to establish partition ranges).

To add your VPC network range to Cassandra and Spark allowed addresses:

  1. On the Cluster Overview page, click Firewall Rules from Manage Cluster menu of your cluster.
  2. On the Firewall Rules page, add your VPC network range to Cassandra Allowed Addresses, Spark Masters Allowed Addresses and Spark Jobserver Allowed Addresses. After adding VPC network address range, click Save Cluster Settings button.

In addition to connections from the Spark Client to the cluster, the architecture of Spark means that the Spark Cluster needs to be able to connect to the clients. Enable this in AWS by editing the security group associated with your Spark Client to add an Inbound rule with the following values:

  • Type: Custom TCP Rule
  • Protocol: TCP
  • Port Range: 1024-65535
  • Source: Custom IP, <your cluster network range> (viewable from the cluster details page in the Instaclustr console)

Basic Interaction with Spark Shell

We will now connect to the Spark cluster using the Spark Shell and run an analytic job. (Note: sometimes the log messages from Spark shell overwrite the shell prompt. If processing seems to have stopped with no prompt then hit the enter key to get a prompt.)

  1. Note the IP of the three Spark Masters in your cluster – this is viewable on the Spark tab on the Instaclustr console for your cluster.
  2. Log in to your Spark Client and run the following command (adjust keywords in <> to specify your spark master IPs, one of cassandra IP, and the cassandra password if you enabled authentication)
  3. Spark-shell should start without any errors. There will be a lot of log message. Once fully started you will see a prompt: “scala>”.
  4. Some imports are necessary. For this simple job, enter the following at the prompt:
  5. Now we can create an rdd and execute an action on it. Only the action (rdd.count) will trigger the calculation. In this case, we use the “system_schema” keyspace that is used by cassandra to keep tracks of internals, such as the list of keyspaces.
  6. You should see a lot of log messages followed by the row count message.

Using Spark SQL from Spark Shell

Spark SQL allows you to run complex SQL queries against Cassandra data. The following step demonstrate how to execute a Spark SQL query against Cassandra using the Spark SQL connector. Execute these steps in the same Spark shell session where you executed the previous example:

  1. Import the required libraries:
  2. Create a temporary view to access datasets using Spark SQL:
  3. Run queries on the temporary view:

Creating and Submitting a Scala Job

In this step of the tutorial we will demonstrate how to build and submit a Scala job. This is useful where you wish to create a job and submit it multiple times.

  1. Log in to your Spark client machine.
  2. Create required directories for your project:
  3. Create a file called build.sbt in the cassandra-count directory with the following contents (note: the blank lines are important):
  4. Create a file called assembly.sbt in the cassandra-count/project directory with the following contents (this will include required dependencies in the output jars):
  5. Create a file called cassandra-count.scala in the cassandra-count/src/main/scala directory with the following contents:
  6. Create a file called cassandra-count.conf in the cassandra-count directory (this file contains the configuration that will be used when we submit the job):
  7. Build the job (from cassandra-count directory):
  8. Submit the job (from cassandra-count directory):
  9. You should see a lot of log messages with the row count message about 15 messages from the end.

Conclusion

In this tutorial you have learned how to:

  • Provision a cluster with Cassandra and Spark
  • Setup and configure Spark client
  • Use Spark SQL from spark shell and
  • Create a submit a scala job

For more information, refer the following resources:

Site by Swell Design Group