Cassandra Best Practices Series - CQL QuickStart

Simple Single Node C*

This article describes how to simply set up a single node of Cassandra and how to build a multi-node Cassandra cluster. To set up a single node, we need to do the following things:

1. Node deployment JAVA basic environment;
2. Get the Cassandra binary package you need: either compile the source code or get it directly Official Web Download the 3.11.5 bin package;
3.tar xf apache-cassandra-3.11.5-bin.tar.gz apache-cassandra-3.11.5/bin directory;

5. Execute. /cassandra Start Single Node C*;

Next, you execute the. /nodetool status under the bin directory. If you observe the following state, you can verify that the single-node Cassandra has started correctly.

Be careful:

Because we did not modify Cassandra's configuration file, all processes with a single node here use the default configuration, such as using the default 256 vnode, using/var/lib/cassandra/data as the data storage directory, using/var/lib/cassandra/commitlog as the commitlog storage directory, etc., binding localhost by default, no account password authentication by default.

Access C*

Through cqlsh

The cqlsh under the bin directory is similar to a client accessing Cassandra. By default, one level is specified to access Cassandra. Since we set sever binding localhost here, and cqlsh accesses localhost by default, a single-node Cassandra deployed above can be accessed directly under the bin directory. /cqlsh, as shown below:

We can use cqlsh to perform common Cassandra DDL, DML operations, such as building a keyspace ks and a table tb inside a keyspace.Use the following cqls:

CREATE KEYSPACE ks WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};
use ks;
CREATE TABLE tb ( name text PRIMARY KEY ,age int);

We can do this in Cassandra by:


Get all the relevant keyspace, table, index, mv definitions below the keyspace; as follows:

We can also insert and select operations in this table tb, as shown below:

Via java driver

Of course, it is difficult to perform some common dml operations through cql when we are actually developing our business. Here we use datastax's open source java-driver for common cassandra access, as well as other versions and language drivers (see Here).

When using java driver, simply create a new maven project and add the following configuration to pom.xml (we'll use a 3.8.0 Java driver here to demonstrate):


Then our demo code is as follows:

package com.aliyun.cstar.driver.test;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;

public class Test 
    static String[] CONTACT_POINTS = {""};
    static int PORT = 9042;

    public static void main(String[] args) {

      Cluster cluster = null;
      try {
        System.out.println("CLUSTER CONNECT !");
        //cluster operation 
        cluster = Cluster.builder().addContactPoints(CONTACT_POINTS).withPort(PORT).build();
        Session session = cluster.connect();

        System.out.println("CREATE KEYSPACE AND TABLE !");
        //DDL:keyspace and table operation
        session.execute("CREATE KEYSPACE IF NOT EXISTS  newks WITH replication "
                + "= {'class':'SimpleStrategy', 'replication_factor':1};");

        session.execute("CREATE TABLE IF NOT EXISTS  newks.newtb (name text PRIMARY KEY, age int)");
        System.out.println("INSERT INTO TABLE AND SELECT TABLE !");
        //DML:insert and select
        session.execute("INSERT INTO newks.newtb (name, age) VALUES('xla', 22)");
        session.execute("INSERT INTO newks.newtb (name, age) VALUES('xlb', 22)");
        session.execute("INSERT INTO newks.newtb (name, age) VALUES('xlc', 22)");
        ResultSet results = session.execute("SELECT * FROM newks.newtb");
        results = session.execute("SELECT name from newks.newtb");
        results = session.execute("SELECT count(*) from newks.newtb");
        System.out.println("FINISHED OPERATION !");
      } finally {
        if (cluster != null) cluster.close();

The final result is as follows:

CQL use

Here is a brief introduction to the common use of CQL, which can be divided into two main categories: DDL and DML.



CREATE KEYSPACE ksname WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3}; 

If there are multiple DC s, the table can be built as follows to ensure the use of NetworkTopologyStrategy:

    WITH replication = {'class': 'NetworkTopologyStrategy', 'DC1' : 3, 'DC2' : 3};


    WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 4};

Modified the number of copies from 3 to 4.


    pk text,
    v1 int,
    v2 text,
    v3 text ,
    PRIMARY KEY (pk, v1)

primary key must be specified to uniquely determine the unique id of the data in the cluster.Of course, TABLE has many other attributes. Use the default here, others will be described in more detail later.

Here's how we use it most often, as well as drop keyspace, truncate table, and so on. Here.


We list our common SELECT, INSERT, UPDATE, DELETE, BATCH operations:


INSERT INTO t (pk, v1, v2, v3) VALUES ( 'pk1', 1, 'v2', 'v3');

Be sure to provide our PRIMARY KEY data;


SELECT pk, v1, v2, v3 FROM t;


UPDATE t SET v2 = 'vv2' WHERE pk = 'pk1' AND v1 =1;


 DELETE  FROM  t WHERE pk = 'pk1' AND v1 =1;


      ... INSERT INTO t (pk, v1, v2, v3) VALUES ( 'pk1', 1, 'v2', 'vV3');
      ... APPLY BATCH ;

Tags: Java Session Apache Maven

Posted on Mon, 23 Mar 2020 00:52:38 -0400 by Zhadus