Skip to content

Latest commit

History

24 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

DataStax Java Driver for Apache Cassandra Quickstart

Build Status

A basic CRUD Java application using the DataStax Java Driver for Apache Cassandra. The intent is to help users get up and running quickly with the driver. If you are having trouble, the complete code solution for GettingStarted.java can be found here.

Prerequisites

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'};
CREATETABLEdemo.users (
lastname textPRIMARY 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 centertry (CqlSessionsession = CqlSession.builder()
.addContactPoint(newInetSocketAddress("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

privatestaticvoidsetUser(CqlSessionsession, Stringlastname, intage, Stringcity, Stringemail, Stringfirstname) {
//TO DO: execute SimpleStatement that inserts one user into the tablesession.execute(
SimpleStatement.builder( "INSERT INTO users (lastname, age, city, email, firstname) VALUES (?,?,?,?,?)")
.addPositionalValues(lastname, age, city, email, firstname)
.build());
}

SELECT a user

privatestaticvoidgetUser(CqlSessionsession, Stringlastname) {
//TO DO: execute SimpleStatement that retrieves one user from the table//TO DO: print firstname and age of userResultSetrs = session.execute(
SimpleStatement.builder("SELECT * FROM users WHERE lastname=?")
.addPositionalValue(lastname)
.build());
Rowrow = rs.one();
System.out.format("%s %d\n", row.getString("firstname"), row.getInt("age"));
}

UPDATE a user's age

privatestaticvoidupdateUser(CqlSessionsession, intage, Stringlastname) {
//TO DO: execute SimpleStatement that updates the age of one usersession.execute(
SimpleStatement.builder("UPDATE users SET age =? WHERE lastname =? ")
.addPositionalValues(age, lastname)
.build());
}

DELETE a user

privatestaticvoiddeleteUser(CqlSessionsession, Stringlastname) {
//TO DO: execute SimpleStatement that deletes one user from the tablesession.execute(
SimpleStatement.builder("DELETE FROM users WHERE lastname=?")
.addPositionalValue(lastname)
.build());
}

About

A basic demo CRUD application to help users get up and running with the DataStax Java Driver.

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages