繁体   English   中英

如何以编程方式获取Cassandra版本

[英]How to get Cassandra version Programmatically

我在Java 7中使用com.datastax.cassandra cassandra-driver-core version 1.0.0连接到Apache Cassandra(版本1.2.12)。 虽然1.2.12是我今天使用的版本,但该版本可能会发生变化,如果可能的话,我想知道如何以编程方式检索Cassandra的版本(大概是使用驱动程序,尽管我对其他人开放建议)。

我找到了Cluster.getMetadata()Cluster.getMetadata.getKeyspace() ,它们分别返回了一个Metadata对象和一个KeyspaceMetadata对象,但是这些对象似乎都没有任何返回版本的方法。

任何帮助表示赞赏

回答

感谢米哈伊尔和布莱斯,我想出了答案。 此方法返回Cassandra版本,如果群集已关闭,则返回“Offline”。 我已经测试了这段代码,它完美无瑕。

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";
}

再次感谢你们!

除了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中的数据字典

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM