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/taxi-iot-demo

by DataStax-Examples

This demo traces moving vehicles as they pass through geohash tiles. It also keeps track of a vehicle movements on a day to day basis. Similar to a vessel tracking or taxi application.Contributor(s): Patrick CallaghanObjectivesTo demonstrate how Cassandra and Datastax can be used to solve IoT data management issues.Project LayoutSchemaSetup.java - Sets up the current_location and vechile tables.Main.java - Continuously updates the locations of the vehicle.How this WorksThis application:Allows the user to track a vehicles movements per day. The location is stored in a Cassandra table.Find all vehicles per tile. Tiles have 2 sizes. Tile1 is large, Tile2 is small.Find all vehicles within a given radius of any vehicle.Setup and RunningPrerequisitesJava 8A running DSE clusterMaven to compile and run codeRunningSetup the schemaTo 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 multiple points in the IP,IP,IP (no spaces).To create the schema, run the followingmvn clean compile exec:java -Dexec.mainClass="com.datastax.demo.SchemaSetup" -DcontactPoints=localhostCreate the Solr coreTo create the solr core, rundsetool create_core datastax_taxi_app.current_location reindex=true schema=src/main/resources/solr/geo.xml solrconfig=src/main/resources/solr/solrconfig.xmlUpdate vehicle locationsTo continuously update the locations of the vehicles runmvn clean compile exec:java -Dexec.mainClass="com.datastax.taxi.Main" -DcontactPoints=localhostRun the webserviceTo start the web server, in another terminal runmvn jetty:run Sample queriesTo find all movements of a vehicle use http://localhost:8080/datastax-taxi-app/rest/getmovements/{vehicle}/{date} e.g.http://localhost:8080/datastax-taxi-app/rest/getmovements/207/20200424Alternatively, with CQL:select * from vehicle where vehicle = '207' and day='20200424';To find all vehicle movement, use the rest command http://localhost:8080/datastax-taxi-app/rest/getvehicles/{tile} e.g.http://localhost:8080/datastax-taxi-app/rest/getvehicles/gcpmAlternatively, with CQL: select * from current_location where solr_query = '{"q": "tile1:gcpm"}' limit 1000;To find all vehicles within a certain distance of a latitude and longitude, http://localhost:8080/datastax-taxi-app/rest/search/{lat}/{long}/{distance}http://localhost:8080/datastax-taxi-app/rest/search/52.53956077140064/-0.20225833920426117/5Alternatively, with CQL:select * from current_location where solr_query = '{"q": "*:*", "fq": "{!geofilt sfield=lat_long pt=52.53956077140064,-0.20225833920426117 d=5}"}' limit 1000;Remove the schemaTo remove the tables and the schema, run the following:mvn clean compile exec:java -Dexec.mainClass="com.datastax.demo.SchemaTeardown"

Illustration Image

This demo traces moving vehicles as they pass through geohash tiles. It also keeps track of a vehicle movements on a day to day basis. Similar to a vessel tracking or taxi application.

Contributor(s): Patrick Callaghan

Objectives

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

Project Layout

  • SchemaSetup.java - Sets up the current_location and vechile tables.
  • Main.java - Continuously updates the locations of the vehicle.

How this Works

This application:

  1. Allows the user to track a vehicles movements per day. The location is stored in a Cassandra table.

  2. Find all vehicles per tile. Tiles have 2 sizes. Tile1 is large, Tile2 is small.

  3. Find all vehicles within a given radius of any vehicle.

Setup and Running

Prerequisites

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

Running

  • Setup the schema

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 multiple points in the IP,IP,IP (no spaces).

To create the schema, run the following

mvn clean compile exec:java -Dexec.mainClass="com.datastax.demo.SchemaSetup" -DcontactPoints=localhost
  • Create the Solr core

To create the solr core, run

dsetool create_core datastax_taxi_app.current_location reindex=true schema=src/main/resources/solr/geo.xml solrconfig=src/main/resources/solr/solrconfig.xml
  • Update vehicle locations

To continuously update the locations of the vehicles run

mvn clean compile exec:java -Dexec.mainClass="com.datastax.taxi.Main" -DcontactPoints=localhost
  • Run the webservice

To start the web server, in another terminal run

mvn jetty:run  
  • Sample queries

To find all movements of a vehicle use http://localhost:8080/datastax-taxi-app/rest/getmovements/{vehicle}/{date} e.g.

http://localhost:8080/datastax-taxi-app/rest/getmovements/207/20200424

Alternatively, with CQL:

select * from vehicle where vehicle = '207' and day='20200424';

To find all vehicle movement, use the rest command http://localhost:8080/datastax-taxi-app/rest/getvehicles/{tile} e.g.

http://localhost:8080/datastax-taxi-app/rest/getvehicles/gcpm

Alternatively, with CQL:

 select * from current_location where solr_query = '{"q": "tile1:gcpm"}' limit 1000;

To find all vehicles within a certain distance of a latitude and longitude, http://localhost:8080/datastax-taxi-app/rest/search/{lat}/{long}/{distance}

http://localhost:8080/datastax-taxi-app/rest/search/52.53956077140064/-0.20225833920426117/5

Alternatively, with CQL:

select * from current_location where solr_query = '{"q": "*:*", "fq": "{!geofilt sfield=lat_long pt=52.53956077140064,-0.20225833920426117 d=5}"}' limit 1000;
  • 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