1

Here is a simple program for test connection to cassandra:

package testJava.db.cassandra;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;

public class ConnectionTester {
    public static void main (String[] args) {
        Session session = null;
        try {
            Cluster cluster = Cluster.builder().addContactPoint("192.168.1.2").build();
            session = cluster.connect("myspace");
            System.out.println(session.getState());
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }
}

When I tun it I get:

com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: /192.168.1.2:9042 (com.datastax.driver.core.TransportException: [/192.168.1.2:9042] Cannot connect))
    at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:199)
    at com.datastax.driver.core.ControlConnection.connect(ControlConnection.java:80)
    at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1199)
    at com.datastax.driver.core.Cluster.init(Cluster.java:154)
    at com.datastax.driver.core.Cluster.connect(Cluster.java:230)
    at com.datastax.driver.core.Cluster.connect(Cluster.java:263)
    at testJava.db.cassandra.ConnectionTester.main(ConnectionTester.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

When I change localhost to 192.168.1.2 every thing works. Also ipconfig windows command returns:

Ethernet adapter Local Area Connection:

   Description . . . . . . . . . . . . . : Realtek PCIe GBE Family Controller
   Physical Address. . . . . . . . . . . : 40-61-86-7C-4B-0F
   DHCP Enabled. . . . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . . . : Yes
   IPv4-address. . . . . . . . . . . . . : 192.168.1.2(Preffered)
   Subnet Mask . . . . . . . . . . . . . : 255.255.255.0
   Default gateway . . . . . . . . . . . : 192.168.1.1
   DHCP-Server . . . . . . . . . . . . . : 192.168.1.1
   DNS-Servers . . . . . . . . . . . . . : 192.168.1.1

So it is clear that my IP address is 192.168.1.2

Important

Yes I can stay with with localhost or 127.0.0.1 but I wana know why my example above do not work. Also when I specify 192.168.1.2 address and run application on virtual machine it not work too, even when ping command works.

Cherry
  • 31,309
  • 66
  • 224
  • 364

1 Answers1

2

Configure your rpc_address in cassandra.yaml to point to the address your client will use to connect to Cassandra.

These are the 3 addresses configurable in cassandra.yaml.

Listen Address - This is the ip address other Cassandra nodes will use to talk to this node. If you are on the cloud, an internal IP address is good here for performance.

RPC Address - This is the address your client connects to, probably the one you want to configure to be an IP that is accessible from your client machine.

Broadcast Address - If you are using multiple data centers or AWS Regions, where not all the nodes have access to each other via internal IP. You can specify the external IP address for the nodes in different data centers can still talk to each other. In many cases you don't need this setting at all, it will default to your Listen Address.

Community
  • 1
  • 1
phact
  • 7,305
  • 23
  • 27