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/quickstart-java

by DataStax-Examples

A basic CRUD Java application using the DataStax Java Driver for Apache Cassandra. Run the GettingStartedComplete.java file if you want to skip the exercise and run the application with the complete code.Contributors: Rebecca MillsObjectivesTo demonstrate how to perform basic CRUD operations with the DataStax Java Driver.The intent is to help users get up and running quickly with the driver.How this Sample WorksThis project walks through basic CRUD operations using Cassandra. The demo application will first insert a row of user data, select that same row back out, update the row and finally delete the user. The README includes the code snippets to be filled in to the main application code to complete the functionality.PrerequisitesA running instance of Apache Cassandra® 2.1+Maven build automation toolJava 8Project LayoutGettingStarted.java - Main application file with space to fill in CRUD operation codeusers.cql - Use this file to create the schemaCreate the keyspace and tableThe resources/users.cql file provides the schema used for this project:CREATE KEYSPACE demo WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'};CREATE TABLE demo.users ( lastname text PRIMARY KEY, age int, city text, email text, firstname text);Connect to your clusterAll of our code is contained in the GettingStarted class.Note how the main method creates a session to connect to our cluster and runs the CRUD operations against it.Replace the default parameters in CqlSession.builder() with your own hostname, port and datacenter.// TO DO: Fill in your own host, port, and data centertry (CqlSession session = CqlSession.builder() .addContactPoint(new InetSocketAddress("127.0.0.1", 9042)) .withKeyspace("demo") .withLocalDatacenter("datacenter1") .build()) CRUD OperationsFill the code in the methods that will add a user, get a user, update a user and delete a user from the table with the driver.INSERT a userprivate static void setUser(CqlSession session, String lastname, int age, String city, String email, String firstname) { //TO DO: execute SimpleStatement that inserts one user into the table session.execute( SimpleStatement.builder( "INSERT INTO users (lastname, age, city, email, firstname) VALUES (?,?,?,?,?)") .addPositionalValues(lastname, age, city, email, firstname) .build());}SELECT a userprivate static void getUser(CqlSession session, String lastname) { //TO DO: execute SimpleStatement that retrieves one user from the table //TO DO: print firstname and age of user ResultSet rs = session.execute( SimpleStatement.builder("SELECT * FROM users WHERE lastname=?") .addPositionalValue(lastname) .build()); Row row = rs.one(); System.out.format("%s %d\n", row.getString("firstname"), row.getInt("age"));}UPDATE a user's ageprivate static void updateUser(CqlSession session, int age, String lastname) { //TO DO: execute SimpleStatement that updates the age of one user session.execute( SimpleStatement.builder("UPDATE users SET age =? WHERE lastname =? ") .addPositionalValues(age, lastname) .build());}DELETE a userprivate static void deleteUser(CqlSession session, String lastname) { //TO DO: execute SimpleStatement that deletes one user from the table session.execute( SimpleStatement.builder("DELETE FROM users WHERE lastname=?") .addPositionalValue(lastname) .build());}

Illustration Image

Build Status

A basic CRUD Java application using the DataStax Java Driver for Apache Cassandra. Run the GettingStartedComplete.java file if you want to skip the exercise and run the application with the complete code.

Contributors: Rebecca Mills

Objectives

  • To demonstrate how to perform basic CRUD operations with the DataStax Java Driver.
  • The intent is to help users get up and running quickly with the driver.

How this Sample Works

This project walks through basic CRUD operations using Cassandra. The demo application will first insert a row of user data, select that same row back out, update the row and finally delete the user. The README includes the code snippets to be filled in to the main application code to complete the functionality.

Prerequisites

Project Layout

Create the keyspace and table

The resources/users.cql file provides the schema used for this project:

CREATE KEYSPACE demo
    WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'};
CREATE TABLE demo.users (
    lastname text PRIMARY KEY,
    age int,
    city text,
    email text,
    firstname text);

Connect to your cluster

All of our code is contained in the GettingStarted class. Note how the main method creates a session to connect to our cluster and runs the CRUD operations against it. Replace the default parameters in CqlSession.builder() with your own hostname, port and datacenter.

// TO DO: Fill in your own host, port, and data center
try (CqlSession session = CqlSession.builder()
                .addContactPoint(new InetSocketAddress("127.0.0.1", 9042))
                .withKeyspace("demo")
                .withLocalDatacenter("datacenter1")
                .build()) 

CRUD Operations

Fill the code in the methods that will add a user, get a user, update a user and delete a user from the table with the driver.

INSERT a user

private static void setUser(CqlSession session, String lastname, int age, String city, String email, String firstname) {
    
    //TO DO: execute SimpleStatement that inserts one user into the table
    session.execute(
            SimpleStatement.builder( "INSERT INTO users (lastname, age, city, email, firstname) VALUES (?,?,?,?,?)")
            .addPositionalValues(lastname, age, city, email, firstname)
            .build());
}

SELECT a user

private static void getUser(CqlSession session, String lastname) {
    //TO DO: execute SimpleStatement that retrieves one user from the table
    //TO DO: print firstname and age of user
    ResultSet rs = session.execute(
    SimpleStatement.builder("SELECT * FROM users WHERE lastname=?")
            .addPositionalValue(lastname)
            .build());
    Row row = rs.one();
    System.out.format("%s %d\n", row.getString("firstname"), row.getInt("age"));
}

UPDATE a user's age

private static void updateUser(CqlSession session, int age, String lastname) {
    //TO DO: execute SimpleStatement that updates the age of one user
    session.execute(
            SimpleStatement.builder("UPDATE users SET age =?  WHERE lastname =? ")
            .addPositionalValues(age, lastname)
            .build());
}

DELETE a user

private static void deleteUser(CqlSession session, String lastname) {
   //TO DO: execute SimpleStatement that deletes one user from the table
    session.execute(
            SimpleStatement.builder("DELETE FROM users WHERE lastname=?")
                    .addPositionalValue(lastname)
                    .build());
}

Related Articles

spring
rest
api

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

John Doe

2/14/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

examples