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:3 min

DataStax-Examples/banking-iot-demo

by DataStax-Examples

A bank wants to help locate and tag all their expenses/transactions in their bank account to help them categorize their spending. The users will be able to tag any expense/transaction to allow for efficient retrieval and reporting. There will be 10 millions customers with on average 500 transactions a year. Some business customers may have up to 10,000 transactions a year. The client wants the tagged items to show up in searches in less than a second to give users a seamless experience between devices.This requires DataStax Enterprise running in Search mode. See background for use case and requirements.Contributor(s): Patrick CallaghanObjectivesTo demonstrate how Cassandra and Datastax can be used to solve IoT data management issues.Project LayoutSchemaSetup.java - Sets up the banking IoT schema with transaction tables.Main.java - Creates transactions.How this WorksAfter setting up the transaction schema, the transaction data is inserted and a Solr core is created. The transactions can then be searched on the basis of tags.Setup and RunningPrerequisitesJava 8A DSE clusterMaven to compile and run codeRunningSetup the schemaTo create the schema, run the followingmvn clean compile exec:java -Dexec.mainClass="com.datastax.demo.SchemaSetup" -DcontactPoints=localhostInsert transactionsTo create some transactions, run the followingmvn clean compile exec:java -Dexec.mainClass="com.datastax.banking.Main" -DcontactPoints=localhostYou can use the following parameters to change the default no of transactions and credit cards-DnoOfTransactions=10000000 -DnoOfCreditCards=1000000Create the Solr coreTo create the solr core, runbin/dsetool create_core datastax_banking_iot.latest_transactions generateResources=true reindex=true coreOptions=rt.yamlSample queriesFor the latest transaction table we can run the following types of queriesuse datastax_banking_iot;select * from latest_transactions where cc_no = '1234123412341234';select * from latest_transactions where cc_no = '1234123412341234' and transaction_time > '2020-01-01';select * from latest_transactions where cc_no = '1234123412341234' and transaction_time > '2020-01-01' and transaction_time < '2020-01-31';For the (historic) transaction table we need to add the year into our queries.select * from transactions where cc_no = '1234123412341234' and year = 2020;select * from transactions where cc_no = '1234123412341234' and year = 2020 and transaction_time > '2019-12-31';select * from transactions where cc_no = '1234123412341234' and year = 2020 and transaction_time > '2019-12-31' and transaction_time < '2020-01-27';Using the solr_queryGet all the latest transactions from PC World in London (This is accross all credit cards and users)select * from latest_transactions where solr_query = 'merchant:PC+World location:London' limit 100;Get all the latest transactions for credit card '1' that have a tag of Work.select * from latest_transactions where solr_query = '{"q":"cc_no:1234123412341234", "fq":"tags:Work"}' limit 1000;Gell all the transaction for credit card '1' that have a tag of Work and are within the last monthselect * from latest_transactions where solr_query = '{"q":"cc_no:1234123412341234", "fq":"tags:Work", "fq":"transaction_time:[NOW-30DAY TO *]"}' limit 1000;Run the webserviceTo use the webservice, start the web server usingmvn jetty:runOpen a browser and use a url likehttp://{servername}:8080/datastax-banking-iot/rest/gettransactions/{creditcardno}/{from}/{to}Note : the from and to are dates in the format yyyyMMdd hh:mm:ss - eghttp://localhost:8080/datastax-banking-iot/rest/gettransactions/1234123412341234/20200101/20200302/Run requestsTo run the requests run the followingmvn clean compile exec:java -Dexec.mainClass="com.datastax.banking.RunRequests" -DcontactPoints=localhostTo change the no of requests and no of credit cards add the following-DnoOfRequests=100000 -DnoOfCreditCards=1000000Remove the schemaTo remove the tables and the schema, run the following.mvn clean compile exec:java -Dexec.mainClass="com.datastax.demo.SchemaTeardown"

Illustration Image

A bank wants to help locate and tag all their expenses/transactions in their bank account to help them categorize their spending. The users will be able to tag any expense/transaction to allow for efficient retrieval and reporting. There will be 10 millions customers with on average 500 transactions a year. Some business customers may have up to 10,000 transactions a year. The client wants the tagged items to show up in searches in less than a second to give users a seamless experience between devices.

This requires DataStax Enterprise running in Search mode. See background for use case and requirements.

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 transaction schema, the transaction data is inserted and a Solr core is created. The transactions can then be searched on the basis of tags.

Setup and Running

Prerequisites

  • Java 8
  • A DSE cluster
  • Maven to compile and run code

Running

  • Setup the schema

To create the schema, run the following

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

To create some transactions, run the following

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

You can use the following parameters to change the default no of transactions and credit cards

-DnoOfTransactions=10000000 -DnoOfCreditCards=1000000
  • Create the Solr core

To create the solr core, run

bin/dsetool create_core datastax_banking_iot.latest_transactions generateResources=true reindex=true coreOptions=rt.yaml
  • Sample queries

For the latest transaction table we can run the following types of queries

use datastax_banking_iot;
select * from latest_transactions where cc_no = '1234123412341234';
select * from latest_transactions where cc_no = '1234123412341234' and transaction_time > '2020-01-01';
select * from latest_transactions where cc_no = '1234123412341234' and transaction_time > '2020-01-01' and transaction_time < '2020-01-31';

For the (historic) transaction table we need to add the year into our queries.

select * from transactions where cc_no = '1234123412341234' and year = 2020;
select * from transactions where cc_no = '1234123412341234' and year = 2020 and transaction_time > '2019-12-31';
select * from transactions where cc_no = '1234123412341234' and year = 2020 and transaction_time > '2019-12-31' and transaction_time < '2020-01-27';
  • Using the solr_query

Get all the latest transactions from PC World in London (This is accross all credit cards and users)

select * from latest_transactions where solr_query = 'merchant:PC+World location:London' limit  100;

Get all the latest transactions for credit card '1' that have a tag of Work.

select * from latest_transactions where solr_query = '{"q":"cc_no:1234123412341234", "fq":"tags:Work"}' limit  1000;

Gell all the transaction for credit card '1' that have a tag of Work and are within the last month

select * from latest_transactions where solr_query = '{"q":"cc_no:1234123412341234", "fq":"tags:Work", "fq":"transaction_time:[NOW-30DAY TO *]"}' limit  1000;
  • Run the webservice

To use the webservice, start the web server using

mvn jetty:run

Open a browser and use a url like

http://{servername}:8080/datastax-banking-iot/rest/gettransactions/{creditcardno}/{from}/{to}

Note : the from and to are dates in the format yyyyMMdd hh:mm:ss - eg

http://localhost:8080/datastax-banking-iot/rest/gettransactions/1234123412341234/20200101/20200302/
  • Run requests

To run the requests run the following

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

To change the no of requests and no of credit cards add the following

-DnoOfRequests=100000  -DnoOfCreditCards=1000000
  • Remove the schema

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

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

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