Java – How to create KEYSPACE in Cassandra using java Class


I am new in Cassandra i want to do a CRUD operation in Cassandra.I am able to connect Cassandra from java class. But now when i doing CRUD its not working.This is my code..

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Host;
import com.datastax.driver.core.Metadata;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;

public class AnotherClient {

private Session session;

private Cluster cluster;

public void connect(String node) {
    cluster = Cluster.builder().addContactPoint(node).build();
    Metadata metadata = cluster.getMetadata();
    System.out.println("Cassandra connection established");
    System.out.printf("Connected to cluster: %s\n",
    for (Host host : metadata.getAllHosts()) {
        System.out.printf("Datatacenter: %s; Host: %s; Rack: %s \n",
                host.getDatacenter(), host.getAddress(), host.getRack());
        session = cluster.connect();


public void createSchema() {
    session.execute("CREATE KEYSPACE simplex WITH replication "
            + "= {'class':'SimpleStrategy', 'replication_factor':3};");

    session.execute("CREATE TABLE simplex.songs (" + "id uuid PRIMARY KEY,"
            + "title text," + "album text," + "artist text,"
            + "tags set<text>," + "data blob" + ");");
    session.execute("CREATE TABLE simplex.playlists (" + "id uuid,"
            + "title text," + "album text, " + "artist text,"
            + "song_id uuid," + "PRIMARY KEY (id, title, album, artist)"
            + ");");


public void loadData() {
    session.execute("INSERT INTO simplex.songs (id, title, album, artist, tags) "
            + "VALUES ("
            + "756716f7-2e54-4715-9f00-91dcbea6cf50,"
            + "'La Petite Tonkinoise',"
            + "'Bye Bye Blackbird',"
            + "'Jos├ęphine Baker'," + "{'jazz', '2013'})" + ";");
    session.execute("INSERT INTO simplex.playlists (id, song_id, title, album, artist) "
            + "VALUES ("
            + "2cc9ccb7-6221-4ccb-8387-f22b6a1b354d,"
            + "756716f7-2e54-4715-9f00-91dcbea6cf50,"
            + "'La Petite Tonkinoise',"
            + "'Bye Bye Blackbird',"
            + "'Jos├ęphine Baker'" + ");");

public void close() {

public static void main(String[] args) {
    AnotherClient client = new AnotherClient();



The connection is properly established.but the method createSchema() is not working properly.this is my error log.

Cassandra connection established
Connected to cluster: Test Cluster
Datatacenter: datacenter1; Host: /; Rack: rack1 
Exception in thread "main" com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: [])
    at com.datastax.driver.core.exceptions.NoHostAvailableException.copy(
    at com.datastax.driver.core.ResultSetFuture.extractCauseFromExecutionException(
    at com.datastax.driver.core.ResultSetFuture.getUninterruptibly(
    at com.datastax.driver.core.Session.execute(
    at com.datastax.driver.core.Session.execute(
    at com.example.cassandra.AnotherClient.createSchema(
    at com.example.cassandra.AnotherClient.main(
Caused by: com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: [])
    at com.datastax.driver.core.RequestHandler.sendRequest(
    at com.datastax.driver.core.Session$Manager.execute(
    at com.datastax.driver.core.Session$Manager.executeQuery(
    at com.datastax.driver.core.Session.executeAsync(
    ... 4 more

Best Solution

Why is your host "/" and not just "" ?

Exception in thread "main" com.datastax.driver.core.exceptions.NoHostAvailableException

The error is telling you that the connection is not properly established because the driver failed to connect to any of the supplied nodes, which you supply as a parameter (String node).

To make it really simple... try:

cluster = Cluster.builder().addContactPoint("").build();