Illustration Image

Cassandra.Link

The best knowledge base on Apache Cassandra®

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

10/23/2020

Reading time:2 min

DataStax-Examples/iot-demo-java

by DataStax-Examples

There are a large number of devices that are generating, tracking, and sharing data across a variety of networks. This can be overwhelming to most data management solutions. Cassandra is a great fit for consuming lots of time-series data that comes directly from users, devices, sensors, and similar mechanisms that exist in a variety of geographic locations.This is a small demo to show how to insert meter readings for a smart reader. Note: the readings come in through a file with a number of readings per day.Contributor(s): Patrick CallaghanObjectivesTo demonstrate how Cassandra and Datastax can be used to solve IoT data management issues.Project LayoutSchemaSetup.java - Sets up the smart meter reader schema.Main.java - Inserts meter reading in table when run.BillingCycleProcessor.java - Runs a billingCycle, which accummulates usages for a specific time periodAggregate.java - Runs an day aggregation, which sums the usage for a day.How this WorksAfter setting up the smart reader schema, meter reading are inserted into the table from a generated file with number of readings per day, per customer. A billing cycle can be run to determine accumulated usages for a specific time, period. A sum of the usage for a day may also be obtained.Setup and RunningPrerequisitesJava 8A Cassandra, DSE cluster or Astra databaseMaven to compile and run codeRunningSetup the schemaNote : This will drop the keyspace "datastax_iot_demo" and create a new one. All existing data will be lost.To specify contact points use the contactPoints command line parameter e.g.-DcontactPoints=192.168.25.100,192.168.25.101The contact points can take mulitple points in the IP,IP,IP (no spaces).To create the a single node cluster with replication factor of 1 for standard localhost setup, run the followingTo create the schema, run the followingmvn clean compile exec:java -Dexec.mainClass="com.datastax.demo.SchemaSetup" -DcontactPoints=localhost -Dexec.cleanupDaemonThreads=falseInsert meter readingsTo insert some meter readings, run the followingmvn clean compile exec:java -Dexec.mainClass="com.datastax.smartmeter.Main" -DcontactPoints=localhostYou can use -DnoOfCustomers and -DnoOfDays to change the no of customer readings and the no of days (in the past) to be inserted. Defaults are 100 and 180 respectively.To view the data using cqlsh, runselect * from datastax_iot_demo.smart_meter_reading where meter_id = 1;Run a billingCycleTo run a billingCycle, which accummulates usages for a specific time period, runmvn clean compile exec:java -Dexec.mainClass="com.datastax.smartmeter.BillingCycleProcessor" -DcontactPoints=localhostTo specific billing cycle use -DbillingCycle (Default is 7).Run a DAY aggregationTo run a DAY aggregation, which sums the usage for a day, runmvn clean compile exec:java -Dexec.mainClass="com.datastax.smartmeter.Aggregate" -DcontactPoints=localhostYou can use -DnoOfCustomers and -DnoOfDays to change the no of customer readings and the no of days (in the past) to be aggregated. Defaults are 100 and 180 respectively.To view the data using cqlsh, runselect * from datastax_iot_demo.smart_meter_reading_aggregates where meter_id = 1 and aggregatetype ='DAY';Remove tables and schemaTo remove the tables and the schema, run the following.mvn clean compile exec:java -Dexec.mainClass="com.datastax.demo.SchemaTeardown" -DcontactPoints=localhost

Illustration Image

There are a large number of devices that are generating, tracking, and sharing data across a variety of networks. This can be overwhelming to most data management solutions. Cassandra is a great fit for consuming lots of time-series data that comes directly from users, devices, sensors, and similar mechanisms that exist in a variety of geographic locations.

This is a small demo to show how to insert meter readings for a smart reader. Note: the readings come in through a file with a number of readings per day.

Contributor(s): Patrick Callaghan

Objectives

  • To demonstrate how Cassandra and Datastax can be used to solve IoT data management issues.

Project Layout

How this Works

After setting up the smart reader schema, meter reading are inserted into the table from a generated file with number of readings per day, per customer. A billing cycle can be run to determine accumulated usages for a specific time, period. A sum of the usage for a day may also be obtained.

Setup and Running

Prerequisites

  • Java 8
  • A Cassandra, DSE cluster or Astra database
  • Maven to compile and run code

Running

  • Setup the schema

Note : This will drop the keyspace "datastax_iot_demo" and create a new one. All existing data will be lost.

To specify contact points use the contactPoints command line parameter e.g.

-DcontactPoints=192.168.25.100,192.168.25.101

The contact points can take mulitple points in the IP,IP,IP (no spaces).

To create the a single node cluster with replication factor of 1 for standard localhost setup, run the following

To create the schema, run the following

mvn clean compile exec:java -Dexec.mainClass="com.datastax.demo.SchemaSetup" -DcontactPoints=localhost -Dexec.cleanupDaemonThreads=false
  • Insert meter readings

To insert some meter readings, run the following

mvn clean compile exec:java -Dexec.mainClass="com.datastax.smartmeter.Main" -DcontactPoints=localhost

You can use -DnoOfCustomers and -DnoOfDays to change the no of customer readings and the no of days (in the past) to be inserted. Defaults are 100 and 180 respectively.

To view the data using cqlsh, run

select * from datastax_iot_demo.smart_meter_reading where meter_id = 1;
  • Run a billingCycle

To run a billingCycle, which accummulates usages for a specific time period, run

mvn clean compile exec:java -Dexec.mainClass="com.datastax.smartmeter.BillingCycleProcessor" -DcontactPoints=localhost

To specific billing cycle use -DbillingCycle (Default is 7).

  • Run a DAY aggregation

To run a DAY aggregation, which sums the usage for a day, run

mvn clean compile exec:java -Dexec.mainClass="com.datastax.smartmeter.Aggregate" -DcontactPoints=localhost

You can use -DnoOfCustomers and -DnoOfDays to change the no of customer readings and the no of days (in the past) to be aggregated. Defaults are 100 and 180 respectively.

To view the data using cqlsh, run

select * from datastax_iot_demo.smart_meter_reading_aggregates where meter_id = 1 and aggregatetype ='DAY';
  • Remove tables and schema

To remove the tables and the schema, run the following.

mvn clean compile exec:java -Dexec.mainClass="com.datastax.demo.SchemaTeardown" -DcontactPoints=localhost

Related Articles

examples
cassandra
datastax

GitHub - datastaxdevs/workshop-betterreads: Clone of Good Reads using Spring and Cassandra

datastaxdevs

12/2/2023

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

case.study

case.study
cassandra