Since the big bang in the data technology landscape happened a decade and a half ago, giving rise to technologies like Hadoop, which cater to the four ‘V’s. — volume, variety, velocity, and veracity there has been an uptick in the use of databases with specialized capabilities to cater to different types of data and usage patterns. You can now see companies using graph databases, time-series databases, document databases, and others for different customer and internal workloads.
Apache Cassandra™ is a wide-column, NoSQL database that is ideal when used for append-only, write-intensive workloads that capture data from IoT sensors, GPS devices, transaction logs, any time-series applications, and more. A lot of these applications need to be coupled with visualization engines for creating reports and dashboards. As most of the visualization libraries are written in JavaScript, using Node.js to interact with the database and the visualization engine is a good idea.
In this tutorial, you will learn how to use a small Node.js application to get cryptocurrency data from an API and continuously ingest that into Cassandra for downstream consumption. You can feed data from Cassandra to a wide variety of applications such as a cryptocurrency trading platform, cryptocurrency price monitoring and alerting, and so on.
Without further ado, let’s get going.
Before getting started with the tutorial, you’ll need the following:
1. Node.js — a local Node.js installation that comes with npm — the default package manager.
2. Git — to clone the GitHub repository that contains the code for this tutorial.
3. DataStax Astra — for spinning up a serverless, Cassandra database to use with an API or driver.
4. CoinGecko-API — a Node.js library to access CoinGecko cryptocurrency data.
You can clone the GitHub repository for Cassandra Node.js Cryptotracker using git clone command:
git clone [email protected]:kovid-r/cassandra-nodejs-cryptotracker.git
After cloning the repository, the first order of business is to install the Node.js dependencies specified in the package.json and package-lock.json files. npm will take care of installing the dependencies using the following command in the repository directory:
npm install
These instructions are also present in the README file in the repository.
Before running the application, you need to sign up for DataStax Astra, which is powered by Apache Cassandra and spin up a Cassandra database.
Although the price information changes as you change cloud providers and regions, for a Proof of Concept, or even small production applications, you won’t need any more than the free $25 credit per month that Astra already offers its users. This gets you roughly 40 GB of free storage, 40 million reads, and 5 million writes. No credit card is required to sign up, just login with Google or GitHub.
Click on the Create Database option, and it will lead you to the following page where you can create a database on any of the three leading cloud providers globally — Google Cloud, AWS, and Azure. You will need to provide the database name (nodeapp) and the keyspace name (coingecko). Please use the specified names.
To access the database for the first time, use the CQLSH in-browser console, as shown in the image below.
Use this console to run the two commands to create a keyspace if it doesn't exist in the database, and to create a table in the specified keyspace. The structure of the table is predefined in the initialize.sql file in the repository. The data from the CoinGecko API calls will land in this table.
You need to download the DataStax Secure Bundle zip file to your Node.js application directory. Make sure that your browser doesn’t unzip the file while downloading it.
You will need to provide the path to this file to the configuration in a later step; for more information about the Secure Bundle, head over to DataStax Astra’s official documentation.
The next step is to generate an authentication token. To do that, click the Billing option on the top menubar. You will land on a screen shown in the image below:
Go to the Token Management option in the left menubar and choose the R/W User role in the Select Role drop-down menu and press Generate Token.
You will see the following screen with your authentication token. Download the Client ID, Client Secret, and Token in a CSV, or copy them directly.
Now, we are good to go ahead and run our Node.js Cryptotracker application. In this tutorial, we are using the config package to avoid storing token information in our code. We have included a file in the repository config/default.json which looks something like this:
To authenticate, replace the values for secureConnectBundle, username, and password in this JSON file as instructed, but make sure you DO NOT commit this to your codebase.
If you have configured everything correctly, you should be able to run the following command from your Node.js application directory now:
npm start
The Node.js application will run for 40 seconds and hit the CoinGecko API. You can change that by going to these two lines in the index.js file and reconfigure the main function call.
After running npm start a couple of times, you can head over to the Astra website to see how much of your quota is used, as shown in the image below:
You can also log into your CQL console and write a CQL query to fetch some data from the coingecko.coin_prices table as shown in the image below:
If you want to capture the price for other coins, you can pass different parameters to the getCoinPrice method arguments — coinName and coinSymbol:
Other applications can consume data from Cassandra. DataStax Astra supports drivers for C#, Python, Node.js, Java, and C++. You can seamlessly integrate popular visualization engines like Grafana with Astra.
In this tutorial, you learned how to use Cassandra as a time-series database for capturing cryptocurrency price movements using a Node.js application. You also learned how to set up a DataStax Astra database in the cloud using your preferred cloud provider.
Please reach out if you have any questions or feedback at [email protected] or connect with me on LinkedIn.
Learn More:
1. Git
2. Node.js
4. CoinGecko
5. CoinGeckoAPI