Reputation: 2888
I'm attempting to create a simple Java app that can connect to Cassandra running inside of a Docker container running on my local machine. For the life of me I cannot get it to connect to the container!
I'm starting my container like this.
docker run -p 9042:9042 cassandra:latest
My Java code isn't super fancy..
private Cluster cluster;
private Session session;
String serverIP = "localhost";
String username = "cassandra";
String password = "cassandra";
String keyspace = "DefaultKeyspace";
public static void main(String[] args) {
CassandraTest cassandraTest = new CassandraTest();
cassandraTest.connect("localhost");
}
public void connect(String node) {
try {
String username = "cassandra";
String password = "cassandra";
String keyspace = "DefaultKeyspace";
cluster = Cluster.builder()
.addContactPoints(serverIP).withCredentials(username.trim(), password.trim())
.build();
session = cluster.connect(keyspace);
// createSchema();
Metadata metadata = cluster.getMetadata();
System.out.println("Connected to cluster:" + metadata.getClusterName());
for (Host host : metadata.getAllHosts()) {
System.out.println("Datatacenter: " + host.getDatacenter()
+ "; Host: " + host.getAddress() + "; Rack: "
+ host.getRack());
}
} catch (Exception e) {
e.printStackTrace();
}
getSession();
createSchema();
}
But no what what I do I keep getting this..
com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: localhost/127.0.0.1:9042 (com.datastax.driver.core.exceptions.InvalidQueryException: unconfigured table schema_keyspaces))
at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:220)
at com.datastax.driver.core.ControlConnection.connect(ControlConnection.java:78)
at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1231)
at com.datastax.driver.core.Cluster.init(Cluster.java:158)
at com.datastax.driver.core.Cluster.connect(Cluster.java:246)
at com.datastax.driver.core.Cluster.connect(Cluster.java:279)
at CassandraTest.connect(CassandraTest.java:37)
at CassandraTest.main(CassandraTest.java:27)
Exception in thread "main" java.lang.IllegalStateException: Can't use this Cluster instance because it was previously closed
at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1213)
at com.datastax.driver.core.Cluster.init(Cluster.java:158)
at com.datastax.driver.core.Cluster.connect(Cluster.java:246)
at CassandraTest.getSession(CassandraTest.java:56)
at CassandraTest.connect(CassandraTest.java:51)
at CassandraTest.main(CassandraTest.java:27)
I've tried to use the docker host IP as well. How do I get it to connect??
Upvotes: 1
Views: 977
Reputation: 2301
You need to use the ip address of your docker-machine as cassandra host. If cassandra has come up clean, you should be able cqlsh into it using docker-machine ip address.
$ docker-machine ip
123.456.78.99
$ export CQLSH_HOST=123.456.78.99
$ cqlsh -u cassandra
Password:
Connected to Test Cluster at 123.456.78.99:9042.
[cqlsh 5.0.1 | Cassandra 3.11.6 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cassandra@cqlsh>
So in your code, instead of using localhost, try using the docker-machine ip
Upvotes: 1