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

DataStax-Examples/forward-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 page 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.Pagination is "forward-only", which means "give me X results, and then the next X results, and then the next X results ..."Project LayoutForwardPagingRestUi.java - The main application which creates and populates a schema and starts the rest service.How this Sample WorksThis application creates a table called forward_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.http://localhost:8080/usersTo explore the paging functionality, use the following and walk from there: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/forward-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/forward-paging-rest-ui-1.0-SNAPSHOT-jar-with-dependencies.jarBy default, it will try 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 page 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.
  • Pagination is "forward-only", which means "give me X results, and then the next X results, and then the next X results ..."

Project Layout

How this Sample Works

This application creates a table called forward_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.

http://localhost:8080/users

To explore the paging functionality, use the following and walk from there:

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/forward-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/forward-paging-rest-ui-1.0-SNAPSHOT-jar-with-dependencies.jar

By default, it will try 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