Skip to content

DataStax-Examples/java-cassandra-driver-from3x-to4x

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

32 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Java Driver Code Samples

  • Latest V3 Driver: Maven Central

  • Latest V4 Driver: Maven Central

This repository contains a list of standalone classes illustrating each a dedicated feature of the DataStax java driver. The purpose is to provide you an extended list of code samples with explicit names to speed up you developments (with copy-paste). We implemented those for both driver 3.x (previous oss) and driver 4.x (latest)

πŸ“‹ Table of content

  1. Prerequisites
  2. Start Local Cluster

1. Prerequisites

πŸ“¦ Java Development Kit (JDK) 8

java --version

πŸ“¦ Apache Maven

mvn -version

πŸ“¦ Docker (local Installation)

Docker is an open-source project that automates the deployment of software applications inside containers by providing an additional layer of abstraction and automation of OS-level virtualization on Linux.

To install on windows Docker Desktop for Windows Installer
To install on MAC Docker Desktop for MAC Installer or Homebrew
# Fetch latest version of homebrew and formula.
brew update              
# Tap the Caskroom/Cask repository from Github using HTTPS.
brew tap caskroom/cask                
# Searches all known Casks for a partial or exact match.
brew search docker                    
# Displays information about the given Cask
brew cask info docker
# Install the given cask.
brew cask install docker              
# Remove any older versions from the cellar.
brew cleanup
# Validate installation
docker -v
To install on linux (centOS) you can use the following commands
# Remove if already install
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
# Utils
sudo yum install -y yum-utils

Add docker-ce repo

sudo dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo

Install

sudo dnf -y install docker-ce --nobest

Enable service

sudo systemctl enable --now docker

Get Status

systemctl status docker

Logout....Lougin

exit

Create user

sudo usermod -aG docker $USER newgrp docker

Validation

docker images docker run hello-world docker -v

2. Start Local Cluster

  • Start Cassandra

After cloning this repository you can start either you local instance of Cassandra with $Cassandra_HOME/bin/cassandra or with docker-compose.

docker-compose up -d
  • Run Samples

You can execute each class with maven and or your favorite IDE. Each class will create everything needed each time keyspace and tables. The working tables will be empty in the beginning for not dropped.

cd example-3x
mvn exec:java -D"exec.mainClass"="com.datastax.samples.SampleCode3x_CONNECT_ClusterShowMetaData"
  • Data displayed with CQL Shell

If cassandra is running as a docker container and you want to have a cqlsh shell please execute:

docker exec -it `docker ps | grep cassandra:5 | cut -b 1-12` cqlsh

Working with Schema

3x 4x Description
ShowMetaData3x ShowMetaData4x Connect to cluster then show keyspaces and metadata
CreateKeyspace3x CreateKeyspace4x Create the killrvideo keyspace using SchemaBuilder if not exist
CreateSchema3x CreateSchema4x Create table and type in killrvideo keyspace if they don't exist
DropKeyspace3x DropKeyspace4x Drop the killrvideo keyspace if existis using SchemaBuilder
DropSchema3x DropSchema4x Drop all table and type in killrvideo keyspace if they exist
--- ConfigurationFile4x Setup the driver using custom conf file and not default application.conf
--- ProgrammaticConfig4x Setup the driver in a programmatic way

Executing Queries

3x 4x Description
GettingStarted3x GettingStarted4x First touch with executing queries
Simple3x Simple4x Read, update, insert, delete operations using QueryBuilder
Paging3x Paging4x Illustrating FetchSize and how to retrieve page by page
Batches3x Batches4x Group statements within batches
ListSetMapUdt3x ListSetMapUdt4x Advanced types insertions with list, set, map but also User Defined Type
Json3x Json4x Work with columns or full record with JSON
Async3x Async4x Sample operations as Simple in Asynchronous way
ObjectMapping3x ObjectMapping4x Map table record to Java POJO at driver level
Counter3x Counter4x Working with counters increment/decrement
Lwt3x Lwt4x Working for Lightweight transactions read-before-write
BlobAndCodec3x BlobAndCodec4x Working with BLOB and binary data but also how to create your own CustomCodec
CloudAstra3x CloudAstra4x Working with BLOB and binary data but also how to create your own CustomCodec
--- Reactive4x Working with the Reactive API introduce in driver 4.x
--- Vector4x Working with the Reactive API introduce in driver 4.x

For reference this is the working schema we used for queries

// ----------------------------------------
// Sample Keyspace (to be used locally)
// 
// Here a sample if you want to create on multiple node DC
// CREATE KEYSPACE IF NOT EXISTS killrvideo 
// WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'datacenter1' : 3 }
// AND DURABLE_WRITES = true;
// ----------------------------------------

CREATE KEYSPACE IF NOT EXISTS killrvideo 
WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }
AND DURABLE_WRITES = true;

// ----------------------------------------
// Basic table for basic operations
// ----------------------------------------
// Used by : SampleCodeXx_CRUD_01_Simple
// Used by : SampleCodeXx_CRUD_02_Paging
// Used by : SampleCodeXx_CRUD_06_Async
// Used by : SampleCodeXx_CRUD_09_LightweightTransactions
// ----------------------------------------

CREATE TABLE IF NOT EXISTS users (
 email      text,
 firstname  text,
 lastname   text,
 PRIMARY KEY (email)
);


// ----------------------------------------
// Table to show MAP, LIST, SET, UDT, JSON
// ----------------------------------------
// Used by : SampleCodeXx_CRUD_04_ListSetMapAndUdt
// Used by : SampleCodeXx_CRUD_06_Json
// ----------------------------------------

CREATE TYPE IF NOT EXISTS video_format (
  width   int,
  height  int
);

CREATE TABLE IF NOT EXISTS videos (
  videoid    uuid,
  title      text,
  upload     timestamp,
  email      text,
  url        text,
  tags       set <text>,
  frames     list<int>,
  formats    map <text,frozen<video_format>>,
  PRIMARY KEY (videoid)
);


// ----------------------------------------
// Table to show Batches, ObjectMapping
// ----------------------------------------
// Used by : SampleCodeXx_CRUD_03_Bacthes
// Used by : SampleCodeXx_CRUD_07_ObjectMapping
// ----------------------------------------

CREATE TABLE IF NOT EXISTS comments_by_video (
    videoid uuid,
    commentid timeuuid,
    userid uuid,
    comment text,
    PRIMARY KEY (videoid, commentid)
) WITH CLUSTERING ORDER BY (commentid DESC);

CREATE TABLE IF NOT EXISTS comments_by_user (
    userid uuid,
    commentid timeuuid,
    videoid uuid,
    comment text,
    PRIMARY KEY (userid, commentid)
) WITH CLUSTERING ORDER BY (commentid DESC);


// ----------------------------------------
// Table to show Counters
// ----------------------------------------
// Used by : SampleCodeXx_CRUD_08_Counters
// ----------------------------------------

CREATE TABLE IF NOT EXISTS videos_views (
    videoid     uuid,
    views       counter,
    PRIMARY KEY (videoid)
);


// ----------------------------------------
// Table to show Binary DATA
// ----------------------------------------
// Used by : SampleCodeXx_CRUD_10_Blob
// ----------------------------------------

CREATE TABLE IF NOT EXISTS files (
   filename  text,
   upload    timestamp,
   extension text static,
   binary    blob,
   PRIMARY KEY((filename), upload)
) WITH CLUSTERING ORDER BY (upload DESC);

About

StandAlone classes illustrating portion of DataStax Java Driver in version 3.x then 4.x

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages