如何通过编程方式获取Cassandra版本

4
我在Java 7中使用com.datastax.cassandra cassandra-driver-core版本1.0.0连接到Apache Cassandra(版本1.2.12)。虽然我今天使用的是1.2.12版本,但该版本可能会更改,如果可能的话,我想知道如何通过编程方式检索Cassandra的版本(可能使用驱动程序,尽管我也接受其他建议)。
我找到了Cluster.getMetadata()和Cluster.getMetadata.getKeyspace(),它们分别返回Metadata对象和KeyspaceMetadata对象,但这两个对象似乎都没有任何方法可以返回版本。
感谢Mikhail和Bryce,我已经得出答案。此方法返回Cassandra版本,如果群集处于离线状态,则返回“离线”。我已测试此代码,它完美无缺。
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";
}

再次感谢各位!
2个回答

4
您可以从Cassandra查询版本:select release_version from system.local where key = 'local';

4
除了Mikhail非常简明的答案之外,让我补充一下,您可以从“system”键空间上的“local”列族中查询release_version和其他节点自身知道的重要信息。
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))
)...

该列族只应该有一行,以 key='local' 为键。

欲了解更多信息,请查看此文档:Cassandra 1.2中的数据字典


你的回答非常有帮助,如果我可以标记两个正确答案,我会这么做;然而,Mikhail比你更快地给出了答案,并且他的回答也非常有用,所以他得到了大大的绿色勾号,不幸的是,你只能获得+1。再次感谢你的帮助! - Cody S

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接