RandomUser
RandomUser

Reputation: 4220

How to connect my Java based Cassandra client to a Cassandra cluster, rather than a specific node?

I am trying to learn Cassandra and have setup a 4 node Cassandra cluster. I have written a client in Java using Hector, which currently connects to a hard coded single node in the cluster. Ideally, I would like my client to connect to the "cluster" rather then a specific node....so if any of the 4 nodes are down, the client will still connect to something. From the client application perspective how does this work exactly? I can't seem to find a good explanation.

My Hector connection string currently, I need to specify a specific node here:

Cluster c = getOrCreateCluster("Test Cluster", cassandraNode1:9160);

My Cassandra nodes are all configured with my rpc_address: 0.0.0.0

Upvotes: 3

Views: 2912

Answers (2)

DNA
DNA

Reputation: 42597

If you pass a CassandraHostConfigurator to getOrCreateCluster(), you can specify multiple nodes as a comma-separated string:

  public CassandraHostConfigurator(String hosts) {
    this.hosts = hosts;
  }

  ...

  String[] hostVals = hosts.split(",");

  CassandraHost[] cassandraHosts = new CassandraHost[hostVals.length];
  ...

Upvotes: 5

zznate
zznate

Reputation: 1908

You can also toggle CassandraHostConfigurator#setAutoDiscoverHosts and #setUseAutoDiscoverAtStartup to use your initial host(s) to automatically add all hosts found on via the Thrift API method describe_keyspaces. This makes configuration a little bit easier in that you only need to reference a single host.

Keeping autoDiscover enabled (it is off by default) makes it a bit easier to scale out as new nodes will be added as they are discovered. The ability to add nodes is also available via JMX as well so adding nodes can be done manually at any time, though you would have to do it once per Hector instance.

Upvotes: 3

Related Questions