Setting up your DataStax Astra instance with Cassandra.API

Earlier this year, DataStax announced DataStax Astra: A Cassandra Database SaaS that provides the ability to develop and deploy data-driven applications with a cloud-native service, without the hassles of database and infrastructure administration. We had been looking forward to the launch of this service since we first heard about it, when it was initially called DataStax Constellation. Over the past month and a half, we’ve been working on a program that would allow anyone to get up and running on Astra in as little time as possible. In this blog post, we’re going to go over how our program allows you to import data into Astra, how to set up a Node or Python API that will allow you to communicate with your Astra database and how to run our curl/postman test to ensure that you are connected properly.

Prerequisites

  1. Create an account on DataStax Astra
  2. Create a database instance
  3. Clone our repository
  4. Download your “secure-connection-details” folder from your Astra account and place that in the “astra.credentials” directory.

NOTE: Review each directory readme to make sure you have the appropriate modules installed.

In this post, we’ll be running our code on Gitpod but you can work from your local machine if preferred.

Setup

  1. Before you start using our tools, you’ll need to create an account on DataStax Astra
Astra
  1. You’ll then be directed to this screen where you fill in details to launch new Astra Database.
Astra

NOTE: Be sure to remember the following declarations because you will need those to setup your credentials for the project

database name: 
keyspace name: 
username:
password:

  1. Then, you’ll need to do on Astra is to download your “secure-connection-details-zip”. To do that, click on the actions button in the top left section of the screen then click on “Conection Details”
Astra
  1. After, click on the link to “Download secure connect bundle” and save the zip file to the cassandra.api/astra.credentials directory of this project.
Astra

Fill in the naming conventions you declared earlier in your (cassandra.api/astra.credentials/UserCred.json) file

Data Migrator

NOTE: Check the README to make sure you have the right prerequisites for this section.

First up, we have our migrator that imports data into your Astra database.

To get started, open a fresh Gitpod instance by clicking here 

Use the sidebar to navigate to and open cassandra.api/astra.import/RESTToAstra.py

You can change the rows variable on line 32 to reflect the number of rows you want copied over. The data is being pulled in, from Solr, are links that have been collected through our Wallabag API. Just note, the maximum number you can do right now is 11211 rows. Finally, enter the following command in the terminal:

python3 astra.import/data/RESTToAstra.py

Afterwards, you should get a screen like the one below letting you know that the import is complete.

Setting up your REST APIs

NOTE: Check the Python README and Node README to make sure you have the right prerequisites for this section.

Python

Use the sidebar to navigate to and open the file: cassandra.api/leaves.api.python/app.py

Hit the run button in the upper left or enter the command:

python3 astra.api/leaves.api.python/src/app.py

A popup will open in the lower right, telling you about a service on port 80.

Press the make public button

Then click on “Open Browser”. This will open a new tab and navigate to a 404 page.

Add /api/leaves  to the end of that url and navigate to that page. It should contain a list of all of the rows put into your astra table via the data importer. During this step grab the id of at least one entry. (in this case the id is 13952)

Add that id to then end of the url, so that it looks like this [gitpod generated url]/api/leaves/[chosen id]. In this scenarios that would be  /api/leaves/13952 then navigate to the new url. This will show an individual entry from your Astra database.

Node

First, navigate to astra.api/leaves.api.node  and run the following commands:

npm install
npm run dev

You should see the following screen if everything worked successfully.

ArpImg9

Once started, an alert will appear like the image below.

Alt Img

Add /api/leaves to Gitpod url to view the API.

You can click make public and we will get ready to make cURL requests. To do so, we will need to open a new terminal, while keeping the one running the server open. To make the cURL request to get all, we will run 

curl http://localhost:8000/api/leaves/
ArpImg10

Running that will return the result in the terminal, so we will show the end of the last result as the beginning of the result set won’t be able to be seen.

ArpImg11

To do the same with cURL and getting by id: run 

curl http://localhost:8000/api/leaves/:id

We will use the same id we did for Postman.

ArpImg12

As you can see, the returned result is associated with the item with the id of 13952, and the response code can be seen on the left terminal that is running the server.

To make a DELETE request, we can do use the same id, but add a “DELETE” to the curl command: 

curl -X “DELETE” http://localhost:8000/api/leaves/13952
ArpImg13

And to replicate the 404 error and JSON message, we can run that request again:

ArpImg14

Testing Your APIs

NOTE: Check the README to make sure you have the right prerequisites for this section.

We will discuss how to run the tests for testing the node API and the python API from the same test suite.

First, cd into /astra.api/leaves.api.tests and run:

npm install

Once npm install has run, you can either choose to run the node API or the python API first, but only one can be tested at a time because they both point to localhost:8000.

Node

Open a new terminal split to the right and change to the following directory

/astra.api/leaves.api.node

If you haven’t done so already, run

npm install

After, your screen should look like this: 

Then, on the the “leaves.api.node” terminal run:

npm run dev

Once running, you can click make public and close out the next popup. You should see this:

Now that the node API is running, we can use the “leaves.api.tests” terminal to run

npm test

to run the tests. You should see this:

If you inserted your credentials correctly, the tests should pass as above.

Python

Now to move onto testing the python API, we need to stop the node server and then close out the terminal on the right.

Once done, we can navigate to /workspace/cassandra.api/astra.api/leaves.api.python/app.py and open the file. You should see this:

The nice thing about Gitpod is that the modules are already installed, so we do not need to run pip install. Now, we can click the green play button in the right hand corner to run the python API. It should open a new terminal and you can then split it to the right:

Once the python API is running, we can re-run the tests directory on the left terminal, and if your credentials haven’t changed, the tests should pass: 

And there you have it, both the node and python API’s run and pass the tests with minimal setup on Gitpod.

Final Thoughts

So far we are thoroughly enjoying using DataStax Astra and would definitely recommend it to anyone who is in need or is interested in having Apache Cassandra as a service. They currently offer a free-tier of the service where you can try it out before officially subscribing. The intention of our program is to help developers of all backgrounds get started with Astra in under 30 minutes. Feel free to clone our repository down and try out our code, but keep in mind that this is still a work in progress. If you wish to learn more or have any question please reach out!

On top of helping our enterprise clients with DataStax and Sitecore projects we also provide companies with project planning workshops and Virtual CIOs.

We build and manage business platforms. Is your project going south? Did your vendor screw up again (that never happens)? Let’s talk for 15 minutes.