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/random-paging-restui-java

by DataStax-Examples

Working on a backend REST service that handles access to Cassandra while passing results back to a web front-end for display? You've come to the right place if you have any paging functionality in that web application. This example shows how to jump to a given page of the results returned by Cassandra in a web application via a backend REST service.Contributor(s): Tomasz Lelek, Carlos Diaz - derived from hereObjectivesDemonstrate how to use the paging state returned by Cassandra and encode it in HTTP URLs for a REST application.Jump to a random page number by modifying the url.Project LayoutRandomPagingRestUi.java - The main application which creates and populates a schema and starts the rest service.How this WorksThis application creates a table called random_paging_rest_ui in the examples keyspace. It then populates the table with 3 users and 49 videos for each user. It then starts a REST service for that data, accessible via the following endpoint, the port can be modified by changing HTTP_PORT in RandomPagingRestUi.java.http://localhost:8080/usersCassandra does not support offset queries ( CASSANDRA-6511 ), so we emulate it by restarting from the beginning each time, and iterating through the results until we reach the requested page.This is fundamentally inefficient (O(n) in the number of rows skipped), but the tradeoff might beacceptable for some use cases; for example, if you show 10 results per page and you think usersare unlikely to browse past page 10, you only need to retrieve at most 100 rows.To explore the paging functionality, use the following and modify the integer value to randomly page:curl -i http://localhost:8080/users/1/videosSetup and RunningPrerequisitesJava 8A Cassandra, DDAC, DSE cluster or Apollo database ( docker is a nice option for local install - see docs )RunningThis first step in the process is to build and package the application. This can be done using the following command from within the root directory of this repo:mvn packageThis will compile the code and package it as a fat JAR file (located in target/random-paging-rest-ui-1.0-SNAPSHOT-jar-with-dependencies.jar), which contains all the dependencies needed to run the application.Once you have compiled the application, you can run it with:java -jar target/random-paging-rest-ui-1.0-SNAPSHOT-jar-with-dependencies.jarBy default, it will try to connect to your cluster at 127.0.0.1:9042, however you can change the contact points by adding a file called application.conf to your classpath with the following contents:datastax-java-driver { basic { contact-points = [ "1.2.3.4:9042", "5.6.7.8:9042" ] load-balancing-policy.local-datacenter = datacenter1 } }If you would like to connect to an Apollo cluster instead, simply follow the Switch connection between on-prem and cloud example

Illustration Image

Working on a backend REST service that handles access to Cassandra while passing results back to a web front-end for display? You've come to the right place if you have any paging functionality in that web application. This example shows how to jump to a given page of the results returned by Cassandra in a web application via a backend REST service.

Contributor(s): Tomasz Lelek, Carlos Diaz - derived from here

Objectives

  • Demonstrate how to use the paging state returned by Cassandra and encode it in HTTP URLs for a REST application.
  • Jump to a random page number by modifying the url.

Project Layout

How this Works

This application creates a table called random_paging_rest_ui in the examples keyspace. It then populates the table with 3 users and 49 videos for each user. It then starts a REST service for that data, accessible via the following endpoint, the port can be modified by changing HTTP_PORT in RandomPagingRestUi.java.

http://localhost:8080/users

Cassandra does not support offset queries ( CASSANDRA-6511 ), so we emulate it by restarting from the beginning each time, and iterating through the results until we reach the requested page.

This is fundamentally inefficient (O(n) in the number of rows skipped), but the tradeoff might be acceptable for some use cases; for example, if you show 10 results per page and you think users are unlikely to browse past page 10, you only need to retrieve at most 100 rows.

To explore the paging functionality, use the following and modify the integer value to randomly page:

curl -i http://localhost:8080/users/1/videos

Setup and Running

Prerequisites

  • Java 8
  • A Cassandra, DDAC, DSE cluster or Apollo database ( docker is a nice option for local install - see docs )

Running

This first step in the process is to build and package the application. This can be done using the following command from within the root directory of this repo:

mvn package

This will compile the code and package it as a fat JAR file (located in target/random-paging-rest-ui-1.0-SNAPSHOT-jar-with-dependencies.jar), which contains all the dependencies needed to run the application.

Once you have compiled the application, you can run it with:

java -jar target/random-paging-rest-ui-1.0-SNAPSHOT-jar-with-dependencies.jar

By default, it will try to connect to your cluster at 127.0.0.1:9042, however you can change the contact points by adding a file called application.conf to your classpath with the following contents:

datastax-java-driver {
   basic {
     contact-points = [ "1.2.3.4:9042", "5.6.7.8:9042" ]
     load-balancing-policy.local-datacenter = datacenter1
   }
 }

If you would like to connect to an Apollo cluster instead, simply follow the Switch connection between on-prem and cloud example

Related Articles

spring
angular
rest

GitHub - jhipster/jhipster-sample-app-cassandra: This is a sample application created with JHipster, with the Cassandra option

jhipster

3/7/2024

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

rest