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/object-mapper-java

by DataStax-Examples

The Java DataStax Driver comes with an object mapper that removes boilerplate of writing queriesand lets you focus on your application objects. This example shows how to use mapper to build Data Access Objects ( DAOs ) to access Apache Cassandra™ in a Javaapplication.Contributor(s): Olivier Michallat - derived fromhereObjectivesDemonstrate how to use the Java Driver object mapper to replace the tedious work of DAO recreation in Java. Reference thedocumentation for details about the object mapper.Project LayoutMapperApp.java - The main application file that uses thevideo and user DAOs.How this Sample WorksThe driver provides a simple object mapper which allows us to avoid writing much of the boilerplate coderequired to map query results to and from POJOs.To use the mapper requires that several components be annotated in such a way as to allow them towork together:Mapper - This is the entry point for this mapper which wraps the core driver session and acts as a factory forcreating the DAO objects. In this example the mapper is KillrVideoMapper.DAO - Acts as the interface which defines the set of operations which can be performed on an entity. In this exampleVideoDao andUserDao are DAO classes.Entity - This is a class which will be mapped to a Cassandra table or UDT. In this exampleVideo,VideoByTag, andUser are Entity classesQuery Provider - These provide a method to define queries which cannont be expressed as static strings.In this exampleCreateVideoQueryProvider,LoginQueryProvider andCreateUserQueryProvider are Query Provider classes.For additional information and details on how to use the Mapper classes please refer to the documentation availablehere.Setup and RunningPrerequisitesJDK 14A Cassandra cluster is running and accessible through the contacts points and data center identified in application.confBuildingAt the project root levelmvn clean packageThis builds the JAR file located at target/object-mapper-1.0.jarRun the programTo run this application, use the following command:java -jar target/object-mapper-1.0.jarThis will produce results similar to those below.Created User[userid=ce662229-e371-4469-843a-b3e16d9fb8fa, firstname='test', lastname='user', email='testuser@example.com', createdDate=2020-04-16T20:37:17.596601Z]Logging in with testuser@example.com/password123: SuccessLogging in with testuser@example.com/secret123: FailureCreated video [d40e409e-4949-4728-89dc-a4374ee78b17] Accelerate: A NoSQL Original Series (TRAILER)Videos for test user: [d40e409e-4949-4728-89dc-a4374ee78b17] Accelerate: A NoSQL Original Series (TRAILER)Latest videos: [d40e409e-4949-4728-89dc-a4374ee78b17] Accelerate: A NoSQL Original Series (TRAILER)Videos tagged with apachecassandra: [d40e409e-4949-4728-89dc-a4374ee78b17] Accelerate: A NoSQL Original Series (TRAILER)Updated name for video d40e409e-4949-4728-89dc-a4374ee78b17: Accelerate: A NoSQL Original Series - join us online!

Illustration Image

The Java DataStax Driver comes with an object mapper that removes boilerplate of writing queries and lets you focus on your application objects. This example shows how to use mapper to build Data Access Objects ( DAOs ) to access Apache Cassandra™ in a Java application.

Contributor(s): Olivier Michallat - derived from here

Objectives

  • Demonstrate how to use the Java Driver object mapper to replace the tedious work of DAO recreation in Java. Reference the documentation for details about the object mapper.

Project Layout

How this Sample Works

The driver provides a simple object mapper which allows us to avoid writing much of the boilerplate code required to map query results to and from POJOs.

To use the mapper requires that several components be annotated in such a way as to allow them to work together:

  • Mapper - This is the entry point for this mapper which wraps the core driver session and acts as a factory for creating the DAO objects. In this example the mapper is KillrVideoMapper.
  • DAO - Acts as the interface which defines the set of operations which can be performed on an entity. In this example VideoDao and UserDao are DAO classes.
  • Entity - This is a class which will be mapped to a Cassandra table or UDT. In this example Video,
    VideoByTag, and User are Entity classes
  • Query Provider - These provide a method to define queries which cannont be expressed as static strings.
    In this example CreateVideoQueryProvider,
    LoginQueryProvider and CreateUserQueryProvider are Query Provider classes.

For additional information and details on how to use the Mapper classes please refer to the documentation available here.

Setup and Running

Prerequisites

  • JDK 14
  • A Cassandra cluster is running and accessible through the contacts points and data center identified in application.conf

Building

At the project root level

mvn clean package

This builds the JAR file located at target/object-mapper-1.0.jar

Run the program

To run this application, use the following command:

java -jar target/object-mapper-1.0.jar

This will produce results similar to those below.

Created User[userid=ce662229-e371-4469-843a-b3e16d9fb8fa, firstname='test', lastname='user', email='testuser@example.com', createdDate=2020-04-16T20:37:17.596601Z]
Logging in with testuser@example.com/password123: Success
Logging in with testuser@example.com/secret123: Failure
Created video [d40e409e-4949-4728-89dc-a4374ee78b17] Accelerate: A NoSQL Original Series (TRAILER)
Videos for test user:
  [d40e409e-4949-4728-89dc-a4374ee78b17] Accelerate: A NoSQL Original Series (TRAILER)
Latest videos:
  [d40e409e-4949-4728-89dc-a4374ee78b17] Accelerate: A NoSQL Original Series (TRAILER)
Videos tagged with apachecassandra:
  [d40e409e-4949-4728-89dc-a4374ee78b17] Accelerate: A NoSQL Original Series (TRAILER)
Updated name for video d40e409e-4949-4728-89dc-a4374ee78b17: Accelerate: A NoSQL Original Series - join us online!

Related Articles

spring
rest
api

GitHub - DataStax-Examples/spring-data-starter: ⚡️ A sample Spring Data Cassandra REST API

John Doe

2/14/2024

cassandra
java

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

cassandra