Illustration Image

Cassandra.Link

The best knowledge base on Apache Cassandra®

Helping platform leaders, architects, engineers, and operators build scalable real time data platforms.

6/1/2020

Reading time:5 min

Setting up your DataStax Astra instance with Cassandra.API - Business Platform Team

by Tanaka Mapondera

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 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.PrerequisitesCreate an account on DataStax AstraCreate a database instanceClone our repository: https://github.com/Anant/cassandra.api.gitDownload 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.Data MigratorFirst 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 RESTToAstra.pyChange the rows variable on line 32 to reflect the number of rows you want copied over, the max you can do 11211 rows.Finally, hit the run button in the upper left corner or enter the following command in the terminal:python3 astra.import/data/RESTToAstra.pyAfterwards, you should get a screen like the one below letting you know that the import is complete.Setting up your REST APIsPythonUse the sidebar to navigate to astra.api/leaves.api.python/src  and openapp.pyHit 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 buttonThen 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.NodeFirst, navigate to astra.api/leaves.api.node  and run the following commands:npm installnpm run devYou should see the following screen if everything worked successfully.Once started, an alert will appear like the image below.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/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.To do the same with cURL and getting by id: run curl http://localhost:8000/api/leaves/:idWe will use the same id we did for Postman.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/13952And to replicate the 404 error and JSON message, we can run that request again:Testing Your APIsWe 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 installOnce 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.NodeOpen a new terminal split to the right and change to the following directory/astra.api/leaves.api.nodeIf you haven’t done so already, runnpm installAfter, your screen should look like this: Then, on the the “leaves.api.node” terminal run:npm run devOnce 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 runnpm testto run the tests. You should see this:If you inserted your credentials correctly, the tests should pass as above.PythonNow 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 ThoughtsSo 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.

Illustration Image

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 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: https://github.com/Anant/cassandra.api.git
  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.

Data Migrator

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 RESTToAstra.py

Change the rows variable on line 32 to reflect the number of rows you want copied over, the max you can do 11211 rows.Finally, hit the run button in the upper left corner or 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

Python

Use the sidebar to navigate to astra.api/leaves.api.python/src  and open

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

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.

Related Articles

cluster
troubleshooting
datastax

GitHub - arodrime/Montecristo: Datastax Cluster Health Check Tooling

arodrime

4/3/2024

Checkout Planet Cassandra

Claim Your Free Planet Cassandra Contributor T-shirt!

Make your contribution and score a FREE Planet Cassandra Contributor T-Shirt! 
We value our incredible Cassandra community, and we want to express our gratitude by sending an exclusive Planet Cassandra Contributor T-Shirt you can wear with pride.

Join Our Newsletter!

Sign up below to receive email updates and see what's going on with our company

Explore Related Topics

AllKafkaSparkScyllaSStableKubernetesApiGithubGraphQl

Explore Further

cassandra