Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Cassandra version Programmatically

I'm using com.datastax.cassandra cassandra-driver-core version 1.0.0 in Java 7 to connect to an Apache Cassandra (version 1.2.12). While 1.2.12 is the version I am using today, that version is subject to change, and I would like to know, if possible, how to retrieve the version of Cassandra programmatically (presumably using the driver, though I'm open to other suggestions).

I found Cluster.getMetadata() and Cluster.getMetadata.getKeyspace() which returns a Metadata object and a KeyspaceMetadata object, respectively, but neither of those seem to have any methods the would return the version.

Any help is appreciated

ANSWER

Thanks to both Mikhail and Bryce, I've come up with the answer. This method returns the Cassandra version, or "Offline" if the cluster is down. I've tested this code, and it works flawlessly.

private String getCassandraVersion() {
    String[] cassandraServers = <insert your servers here>;
    String keyspace = "system";
    Session session = null;
    try {
        Cluster cluster = Cluster.builder().addContactPoints(cassandraServers)
                .build();
        session = cluster.connect(keyspace);
        PreparedStatement ps = session.prepare("select release_version from system.local where key = 'local'");
        ResultSet result = session.execute(ps.bind());
        Row versionRow = result.one();
        if (versionRow != null) {
            return versionRow.getString("release_version");
        }
    } catch (Exception ex) {
        _log.error("Failed to connect to '" + keyspace + "' keyspace!");
    } finally {
        if(session != null) {
            try {
                session.shutdown();
            } catch (Exception ex) {
                //NOP
            }
        }
    }
    return "Offline";
}

Thanks again guys!

like image 448
Cody S Avatar asked Feb 11 '23 22:02

Cody S


2 Answers

You can query the version from the Cassandra: select release_version from system.local where key = 'local';

like image 138
Mikhail Stepura Avatar answered Feb 24 '23 03:02

Mikhail Stepura


In addition to Mikhail's very concise answer, let me just add that you can query release_version and other important items that a node knows about itself from the local column family on the system keyspace:

cqlsh> use system;
cqlsh:system> desc table local;

CREATE TABLE local (
  key text,
  bootstrapped text,
  cluster_name text,
  cql_version text,
  data_center text,
  gossip_generation int,
  host_id uuid,
  native_protocol_version text,
  partitioner text,
  rack text,
  release_version text,
  schema_version uuid,
  thrift_version text,
  tokens set<text>,
  truncated_at map<uuid, blob>,
  PRIMARY KEY ((key))
)...

That column family should only have one row, keyed off of key='local'.

For more information, check this doc: The Data Dictionary in Cassandra 1.2

like image 37
Aaron Avatar answered Feb 24 '23 03:02

Aaron