How to get the Cassandra version Programmatically
I am using com.datastax.cassandra cassandra-driver-core version 1.0.0 in Java 7 to connect to Apache Cassandra (version 1.2.12). While version 1.2.12 is the version I am using today, this version is subject to change and I would like to know, if possible, how to get the software version of Cassandra (presumably using a driver, although I am open to other suggestions).
I found Cluster.getMetadata()
and Cluster.getMetadata.getKeyspace()
, which returns an object Metadata
and an object KeyspaceMetadata
, respectively, but none of them have any methods that will return a version.
Any help is appreciated
ANSWER
Thanks to Michael and Bryce, I came up with an answer. This method returns the Cassandra version, or "Offline" if the cluster is down. I have 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!
source to share
In addition to Michael's very succinct answer, let me add that you can also query release_version
other important elements that node knows about itself from the column family local
in system
:
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))
)...
This column family should have only one row with a key key='local'
.
For more information check this document: Data Dictionary in Cassandra 1.2
source to share