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

时间:2014-09-30 17:06:53

标签: java cassandra datastax-enterprise

我在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个对象,但这些对象似乎没有任何方法可以返回版本

感谢任何帮助

ANSWER

感谢米哈伊尔和布莱斯,我已经想出了答案。此方法返回Cassandra版本,或者#34; 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";
}

再次感谢你们!

2 个答案:

答案 0 :(得分:4)

除了Mikhail非常简洁的答案之外,我还要补充一点,您可以通过release_version列系列查询local以及节点了解自己的其他重要事项。 {1}}键空间:

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)) )...

有关详细信息,请查看此文档:The Data Dictionary in Cassandra 1.2

答案 1 :(得分:3)

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