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!
You can query the version from the Cassandra: select release_version from system.local where key = 'local';