【问题标题】:Test Hector Spring Cassandra connection测试 Hector Spring Cassandra 连接
【发布时间】:2023-04-02 15:48:01
【问题描述】:

在我基于 Java-Spring 的 Web 应用程序中,我使用 Hector and Spring 连接到 Cassandra DB。

连接工作正常,但我希望能够测试连接。

因此,如果我故意向 CassandraHostConfigurator 提供错误的主机,则会收到错误消息:

ERROR connection.HConnectionManager: Could not start connection pool for host <myhost:myport>

当然可以。但是如何测试这个连接呢?

如果我务实地定义连接(而不是通过 spring 上下文)很清楚,但通过 spring 上下文就不清楚如何测试它。

你能想出一个主意吗?

【问题讨论】:

    标签: spring testing cassandra hector


    【解决方案1】:

    由于我找不到满意的答案,我决定务实地定义我的连接并使用简单的查询:

    private ColumnFamilyResult<String, String> readFromDb(Keyspace keyspace) {
    ColumnFamilyTemplate<String, String> template = new ThriftColumnFamilyTemplate<String, String>(keyspace, tableName, StringSerializer.get(),
        StringSerializer.get());
    // It doesn't matter if the column actually exists or not since we only check the
    // connection. In case of connection failure an exception is thrown,
    // else something comes back.
    return template.queryColumns("some_column");
    }
    

    我的测试检查返回的对象不为空。

    另一种效果很好的方法:

    public boolean isConnected() {
    List<KeyspaceDefinition> keyspaces = null;
    try {
        keyspaces = cluster.describeKeyspaces();
    } catch (HectorException e) {
        return false;
    }
    return (!CollectionUtils.isEmpty(keyspaces));
    }
    

    【讨论】:

      猜你喜欢
      • 2012-08-31
      • 1970-01-01
      • 2012-06-21
      • 1970-01-01
      • 2013-08-10
      • 2011-08-06
      • 2011-12-26
      • 2015-06-03
      • 1970-01-01
      相关资源
      最近更新 更多