Illustration Image

Cassandra.Link

The best knowledge base on Apache Cassandra®

Helping platform leaders, architects, engineers, and operators build scalable real time data platforms.

5/12/2020

Reading time:2 min

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

by John Doe

ObjectivesThis 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)ContributorsSetup and RunningPrerequisitesJava 11+Cassandra installed locally OR Docker (we provide you a working docker-compose.yaml)Maven to compile and eventually run the samples (OR your IDE)RunningStart CassandraAfter cloning this repository you can start either you local instance of Cassandra with $Cassandra_HOME/bin/cassandra or with docker-compose.docker-compose up -dRun SamplesYou 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-3xmvn exec:java -D"exec.mainClass"="com.datastax.samples.SampleCode3x_CONNECT_ClusterShowMetaData"Data displayed with CQL ShellIf cassandra is running as a docker container and you want to have a cqlsh shell please execute:docker exec -it `docker ps | grep cassandra:3.11.5 | cut -b 1-12` cqlshWorking with SchemaExecuting QueriesFor 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);

Illustration Image

image

Objectives

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)

Contributors

Setup and Running

Prerequisites

  • Java 11+
  • Cassandra installed locally OR Docker (we provide you a working docker-compose.yaml)
  • Maven to compile and eventually run the samples (OR your IDE)

Running

  • 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:3.11.5 | cut -b 1-12` cqlsh

Working with Schema

Executing Queries

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

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

cassandra.driver