Getting Started with Spark and Elassandra with Instaclustr

Instaclustr has retired support for Elassandra and now supports Elasticsearch.

Elassandra (Elasticsearch + Cassandra) is a fork of Elasticsearch modified to run on top of Apache Cassandra to provide advanced search features on Cassandra tables. In this tutorial we will walk you through the basic steps of setting up an Instaclustr Elassandra cluster with Spark on Amazon Web Services (AWS) and how to write and query Elassandra from Spark. The high-level steps are:

  1. Provision a cluster with Elassandra and Spark
  2. Set up a Spark client to communicate with Elassandra via the Elasticsearch REST API
  3. Configure client network access
  4. Run basic queries to read data from Elassandra using Spark Shell
  5. Submit a Spark job to write to Elassandra index
  6. Conclusion

This tutorial assumes that you are familiar with launching and connecting to servers in AWS. While this tutorial uses AWS, we also support Spark on Azure, IBM SoftLayer, and Google Cloud Platform. You can follow a similar approach to set up on those platforms or contact if you need more detailed instructions.

Table of Contents

Provision a cluster with Elassandra 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:
    • Elassandra (Cassandra 3.11) (preview)
    • Apache Spark as an Add-on
  4. Under Data Centre section, select:
    • Amazon Web Services as the Infrastructure Provider
    • A minimum node size of m4l-250
  5. 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 your newly created Spark cluster, you will need to set up a client machine 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 Elassandra and Spark cluster
    • VPC: if possible, use a VPC with DNS resolution and DNS hostname enabled (Otherwise, refer step g 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 or t2.micro 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 which matches the cluster created in Step 1. In this case, Spark 1.6.0:
  4. Extract the Spark files:
  5. Download the Spark Elassandra 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. Install curl to run some test REST calls in command line:
  9. To build the final Java example, install maven:

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 AddressesElassandra REST API Allowed AddressesSpark 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)

Open Elassandra REST Port in the Cluster

Elasticsearch by defaults listens to REST API calls on port 9200 and uses 9300 for internal communication between nodes. Elassandra uses the same ports. However, these ports are not open in the cluster for security reasons. Instead, the client machine can talk to Elassandra on 9201 port which is secured by a CA-signed certificate in the cluster. To allow the client to to connect to Elassandra’s REST port (9201) in the cluster:

  • Log into the Instaclustr console and click on the Settings tab inside your cluster panel.
  • In Elassandra REST API Allowed Addresses box, add the global IP address of your client.
  • Finally, save the changes (Refer steps 1 and 2 above in Configure Client Network Access section of this tutorial)

Having these settings in place allows the client to make REST calls to Elassandra on port 9201.

Run a Cluster Health Check

Before running any Spark job either via Spark Shell or Spark Submit command, it is worth checking if the cluster has been properly set up and configured. You can do this by running some basic curl commands to index and read twitter like information (demo from Elasticsearch).

Note: Before running the commands, find the required authentication and URL information from Connection Info page in the Console and set them in the commands accordingly. To find authentication and URL information:

  1. On the Cluster Overview page, click Connection Details from Manage Cluster menu of your cluster.
  2. On the Connection Info page, you can view Elassandra REST API username, password, port number and URL.

Once you have the authentication and URL information, log in to your Spark Client and run the following commands to create a twitter user and two tweets in Elassandra (Replace keywords in <> with corresponding authentication, IP and URL information).

Running the commands above should be followed by messages like the one below that indicates successful creation of a twitter index.

Run basic queries against Elassandra from Spark Shell

We will now connect to the Spark cluster using the Spark Shell and run some queries.

  1. Note the IP addresses 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 commands:
  3. Spark-shell should start without any errors. There will be a lot of log messages. Once fully started you will see a “scala>” prompt as shown below:
  4. Some imports are necessary. For our simple job, enter the following at the prompt:
  5. Now run the following code to read the Elassandra index and create a spark RDD. By default the documents are returned as a Tuple2 with id as first element and the actual value as second element.
  6. You should now see the following output:

Submit a Spark job to write to Elassandra index

In this step of the tutorial we will demonstrate how to build and submit a Spark job written in Java to write to Elassandra index.

  1. Create the following folder structure on the client.
    • Spark_job/src/main/java/
    • Spark_job/pom.xml
  2. Add the following code to the maven pom file (pom.xml):
  3. Create a file called in java directory and add the code below. Replace keywords in <> with the corresponding cluster configs which are visible in the Connection Info page on the console (Refer Run a Cluster Health Check section and Run basic queries against Elassandra from Spark Shell section above in this tutorial to retrieve cluster configs):
  4. Build the job (from spark_job directory):
  5. Submit the job (from spark_job directory):

    You should see a lot of log messages with the row count message towards the end.


In this tutorial you have learned how to:

  • Provision a cluster with Elassandra and Spark
  • Setup and configure Spark client
  • Run basic curl commands from Spark client
  • Run basic queries against Elassandra from Spark Shell and
  • Submit a Spark job to write to Elassandra index

For more information, refer following resources:

Need Support
Learn More

Already have an account?
Login to the Console

Experiencing difficulties on the website or console?
Status page for known incidents

Don’t have an account yet?
Sign up for a free trial

Why sign up?
To experience the ease of creating and managing clusters via the Instaclustr Console.