Leveraging the Stargate Document API for Time Series Data in Cassandra

Introduction

Time Series Data with the Cassandra Database

Building a Weather Monitoring App with Cassandra and Stargate

What Purpose Does the Stargate Document API Serve?

Weather Application Architecture

API Specification

curl --location --request POST 'localhost:3000/api/cities' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "Austin"
}'
curl --location --request POST 'localhost:3000/api/fetcher' \
--header 'Content-Type: application/json' \
--data-raw '{
"city": "Seattle",
"numDays": 7,
"forecastDate": "2022-10-01"
}'
curl --location --request POST 'localhost:3000/api/report1' \
--header 'Content-Type: application/json' \
--data-raw '{
"city": "London",
"date": "2022-11-01"
}'
curl --location --request POST 'localhost:3000/api/report2' \
--header 'Content-Type: application/json' \
--data-raw '{
"city1": "London",
"city2": "Seattle",
"date": "2022-11-01"
}'

Application Components

Figure 1: High-level data flow of the weather application

(1) Data Source

WeatherAPI.com API Keys and Authentication

Figure 2: Weather API API key
Figure 3: Weather API interactive API explorer
Figure 4: An example of Weather API JSON forecast response

(2) Application Configuration

*Instructions on how to generate an application token here.
ASTRA_DB_ID=<ASTRA_DB_ID>
ASTRA_DB_REGION=<ASTRA_DB_REGION>
ASTRA_DB_APPLICATION_TOKEN=<ASTRA_DB_APPLICATION_TOKEN>
ASTRA_DB_KEYSPACE=<ASTRA_DB_KEYSPACE>
WEATHER_API_KEY=<WEATHER_API_KEY>

(3) Weather Data Extractor (/fetch API)

Figure 5: An example of JSON response stored in Stargate Collection
{
"documentId": "Boston_26", ←— documentKey aka primary key
"data": {
"city": "Boston",
"week": 26,
"forecast": [
{
"date": "2022-10-24",
"date_epoch": 1666569600,
"stats": {
"day": {
"mintemp_f": 50,
"maxtemp_f": 68,
"humidity": 85
}
}
}
]
}
}

(4) TimeSeries Queries (/report1, /report2 APIs)

Report 1: Weekly temperature report by day for a given location and a given week

curl --location --request POST 'localhost:3000/api/report1' \
--header 'Content-Type: application/json' \
--data-raw '{
"city": "Seattle",
"date": "2022-11-01"
}'
{
"forecast": [
{
"date": "2022-10-30",
"stats": {
"day": {
"maxtemp_f": 53.2,
"mintemp_f": 47.8
}
}
},
{
"date": "2022-10-31",
"stats": {
"day": {
"maxtemp_f": 55.2,
"mintemp_f": 45.7
}
}
},...
]
}
Figure 6: A graph showing a weekly temperature report

Report 2: Weekly temperature comparison for two given locations and a given week

curl --location --request POST 'localhost:3000/api/report2' \
--header 'Content-Type: application/json' \
--data-raw '{
"city1": "London",
"city2": "Seattle",
"date": "2022-11-01"
}'
{
"forecast": [
{
"date": "2022-10-30",
"stats": {
"city1":
{
"city": "London",
"day": {
"maxtemp_f": 53.2,
"mintemp_f": 47.8
}},
"city2":
{
"city": "Seattle",
"day": {
"maxtemp_f": 43.1,
"mintemp_f": 37.8
}}
}
},
{
"date": "2022-10-31",
"stats": {
"city1":
{
"city": "London",
"day": {
"maxtemp_f": 52.2,
"mintemp_f": 45.8
}},
"city2":
{
"city": "Seattle",
"day": {
"maxtemp_f": 47.1,
"mintemp_f": 39.8
}}
}
},...
]
}

Report 3: Min and max temperatures for a location and a week

Time Series Related Improvements for the Stargate Document API

Conclusion

Resources

--

--

We’re huge believers in modern, cloud native technologies like Kubernetes; we are making Cassandra ready for millions of developers through simple APIs; and we are committed to delivering the industry’s first and only open, multi-cloud serverless database: DataStax Astra DB.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
DataStax

DataStax is the company behind the massively scalable, highly available, cloud-native NoSQL data platform built on Apache Cassandra®.