Coder Social home page Coder Social logo

quickstart-java's Introduction

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'};

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());

}

quickstart-java's People

Contributors

beccam avatar

Stargazers

Kyle Alford avatar Paul Baur avatar Gary Sharpe avatar Guru Prasath Anandapadmanaban avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.