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
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With