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 [email protected] if you need more detailed instructions.
Table of Contents
Provision a cluster with Cassandra and Spark
If you haven’t already signed up for an Instaclustr account, refer our support article to sign up and create an account.
Once you have signed up for Instaclustr and verified your email, log in to the Instaclustr console and click the Create Cluster button to create a new cluster.
Under the Application section, enter an appropriate name for your cluster. Select Apache Cassandra as the base application and Amazon Web Services as the infrastructure provider.
Under Cassandra Setup, select Apache Spark as an add-on. Notethat we recommend that you enable Use private IPs to broadcast for auto-discovery. Do not enable Client to Cluster Encryption. If you wish to configure Apache Spark with an SSL configured Cassandra Cluster, you may refer to our support article.
Under the Data Centre section, select a Data Centre Region, a Cluster Network address block, the number of nodes for your application, and your desired node size. If the data centre for your cluster is running on Amazon’s EBS infrastructure, you can choose to encrypt your data with an AWS KMS key.Please refer to our support article on Network Address Allocation to understand how we divide up the specified network range to determine the node IP addresses.
Accept the terms and conditions and click the 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:
Provision a new Amazon EC2 instance (a virtual AWS server) with the following configuration:
AMI: Ubuntu Server 16.04 LTS (HVM), SSD Volume Type
Instance Type: t2.small is sufficient for this tutorial and sufficient for many use-cases ongoing
VPC: if possible, use a VPC with DNS resolution and DNS hostname enabled (Otherwise, refer to step 7 below). The VPC network range should not overlap with the network range of your instaclustr cluster. Also, ensure that the region of your VPC is the same as that of your newly created Cassandra and Spark cluster.
ssh to the newly launched server with ubuntu as username.
Download the spark version matching your instaclustr version. In this case, Spark 2.1.1:
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.
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):If your spark version is equal or above 2.3.2 you will have to add SPARK_PUBLIC_DNS=<your_ip> and SPARK_LOCAL_IP=<your_ip> to the spark-env.sh in the conf directory of your spark installation.
If you will be building the final scala example, then install sbt:
<span style="font-weight: 400;">echo"deb https://repo.scala-sbt.org/scalasbt/debian all main"|sudo tee/etc/apt/sources.list.d/sbt.list</span>
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).
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
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.)
Find the IP addresses of the three Spark Masters in your cluster – this is viewable on the Apache Spark tab on the Connection Info page for your cluster.
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).
For Spark 3, include the key value pair spark.sql.extensions=com.datastax.spark.connector.CassandraSparkExtensions with –conf. This option will enable Cassandra Specific Catalyst optimizations and functions.
Spark-shell should start without any errors. There will be a lot of log message. Once fully started you will see a prompt: “scala>”.
Some imports are necessary. For this simple job, enter the following at the prompt:
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.
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:
Import the required libraries:
Create a temporary view to access datasets using Spark SQL:
valcreateDDL="""CREATE TEMPORARY VIEW keyspaces
Run queries on the temporary view:
spark.sql("SELECT * FROM keyspaces").show
valrdd1=spark.sql("SELECT count(*) from keyspaces")
println("Row count: "+rdd1.first()(0))
Using Spark SQL from Spark Shell for Spark 3
Create a Catalog Reference to your Cassandra Cluster