1
votes

I need to retrieve information about cluster's datacenters with their nodes. I need to do that using datastax java driver. So I can not invoke say "nodetool status" and parse its output.

I have tried to query system.peers table (SELECT peer,data_center FROM system.peers), but it doesn't return me actual information about all peers and datacenters. It contains only partial list of nodes belonging to one of the datacenters.

Is any way to do that?

2

2 Answers

4
votes

Check for Metadata class. getAllHosts() returns a Set and each Host contains all the info about that host, including dc.

0
votes

@Horia has given me a good hint how to properly resolve this issue. In fact Metadata.getAllHosts() method returns not a full list of hosts. It returns information only about N hosts, where N is equal to the replication factor. So it not necessary contains hosts from multiple datacenters.

But using this class I found another way to do that. Metadata.getKeyspace(...) method returns a KeyspaceMetadata object which does contain information about all datacenters:

Cluster cluster = ...
Metadata metadata = cluster.getMetadata();
Map<String, String> replication = metadata.getKeyspace("keyspace_name").getReplication();