cassandradatastaxdatastax-java-driver

Datastax Java driver 4.x : How to get cluster name?


After upgrading java driver for cassandra from 3.7 to 4.0 (or above) - I am unable to resolve the cluster name.

I need the name of the cassandra cluster to which the my application is connected using java driver. Earlier it was available as "Cluster.getMetadata().getClusterName()". But after upgrading to datastax-driver-core-4.0 or above- I am unable to resolve the Cluster name from CqlSession.getMetadata()..

This is very important because I have segregated operations based on different cluster.


Solution

  • I believe cluster name is no longer provided by the java api. Instead just query it from the system.local :

    SimpleStatement statement =SimpleStatement.newInstance("SELECT cluster_name FROM system.local");
    ResultSet resultSet = session.execute(statement);
    Row row = resultSet.one();
    System.out.println(row.getString("cluster_name"));