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)
- Use the reference documentation to install a Java Development Kit
- Validate your installation with
java --version
- Use the reference documentation to install Apache Maven
- Validate your installation with
mvn -version
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 InstallerTo 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-utilssudo dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo
sudo dnf -y install docker-ce --nobest
sudo systemctl enable --now docker
systemctl status docker
exit
sudo usermod -aG docker $USER newgrp docker
docker images docker run hello-world docker -v
- 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
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 |
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);