6

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",
            metadata.getClusterName());
    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() {
    cluster.shutdown();
}

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

    client.createSchema();
    client.close();
}

}

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: /127.0.0.1; 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(NoHostAvailableException.java:61)
    at com.datastax.driver.core.ResultSetFuture.extractCauseFromExecutionException(ResultSetFuture.java:234)
    at com.datastax.driver.core.ResultSetFuture.getUninterruptibly(ResultSetFuture.java:165)
    at com.datastax.driver.core.Session.execute(Session.java:106)
    at com.datastax.driver.core.Session.execute(Session.java:75)
    at com.example.cassandra.AnotherClient.createSchema(AnotherClient.java:30)
    at com.example.cassandra.AnotherClient.main(AnotherClient.java:81)
Caused by: com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: [])
    at com.datastax.driver.core.RequestHandler.sendRequest(RequestHandler.java:93)
    at com.datastax.driver.core.Session$Manager.execute(Session.java:393)
    at com.datastax.driver.core.Session$Manager.executeQuery(Session.java:429)
    at com.datastax.driver.core.Session.executeAsync(Session.java:146)
    ... 4 more
abhi
  • 4,762
  • 4
  • 29
  • 49
Mandrek
  • 1,159
  • 6
  • 25
  • 55
  • What are you supplying as the `String node` parameter? – Lyuben Todorov Jun 05 '13 at 14:17
  • public static void main(String[] args) { AnotherClient client = new AnotherClient(); client.connect("127.0.0.1"); client.createSchema(); client.close(); }i am supplying the host address – Mandrek Jun 06 '13 at 07:18

3 Answers3

1

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

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("127.0.0.1").build();
Lyuben Todorov
  • 13,987
  • 5
  • 50
  • 69
  • i cannot see where i use "/127.0.0.1" as my host address. i am using "127.0.0.1" and passing it from the main – Mandrek Jun 06 '13 at 07:20
  • @Mandrek Then your local cassandra server is not started. Start the task manager / activity monitor and check to see if cassandra is running (will be a java process). If you cant find it just try to start the cassandra server (do it in the foreground so you can see what is happening) – Lyuben Todorov Jun 06 '13 at 08:14
1

Issue is caused by: Caused by: com.datastax.driver.core.exceptions.NoHostAvailableException

@Lyuben Todorov, the problem is not causing by the leading slash in /127.0.0.1. The reason why there is a leading / before the IP is the logger implicitly call toString of InetAddress, according to this question.

I think the reason could be either the Cassandra remote setting or the firewall setting of Cassandra setting.

  1. For Cassandra setting: change in /etc/cassandra/cassandra.yaml and restart Cassandra:

    rpc_address: 0.0.0.0 broadcast_rpc_address: {YOUR_SERVER_ID_HERE}

  2. For Firewall settings:

    ufw allow 9042 ufw status

For more information, please see this question

Ryan
  • 11
  • 1
0

I ran into the same issue as OP. What happens is this, if I run cassandra-stress from command line, I get the output as Datatacenter: datacenter1; Host: localhost/127.0.0.1; Rack: rack1 i.e. you can see the Host is given as localhost or 127.0.0.1. When I run my tests using Eclipse, I see the forward slash appearing. So for now, it might be a better idea to read the node param and truncate the forward slash.

Sumod
  • 3,806
  • 9
  • 50
  • 69