How to create KEYSPACE in Kassandra using Java class

I am new to Cassandra, I want to do a CRUD operation in Cassandra. I can connect Cassandra from java class. But now when I do CRUD, it does not work. 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 established correctly. but the createSchema () method does not work 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 
+6
source share
2 answers

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

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

The error tells you that the connection was not established correctly because the driver could not connect to any of the supplied nodes that you supply as a parameter (String node).

To make this really easy ... try:

 cluster = Cluster.builder().addContactPoint("127.0.0.1").build(); 
+2
source

I came across the same issue as the OP. What happens is if I run cassandra-stress from the command line, I get the output as Datatacenter: datacenter1; Host: localhost / 127.0.0.1; Rack: rack1 that is, you can see that the host is listed as localhost or 127.0.0.1. When I run my tests using Eclipse, I see that a slash appears. So, for now, it would be better to read the node parameter and truncate the slash.

0
source

Source: https://habr.com/ru/post/946592/


All Articles