Cassandra Java驱动程序的最佳设置,仅写入本地数据中心

AKI*_*WEB 10 java cassandra datastax-java-driver

我最近开始在我们的Cassandra用例中使用Datastax Java驱动程序......我们将使用Datastax Java驱动程序读取/写入Cassandra ...

我成功地能够使用Datastax Java驱动程序创建Cassandra连接......但是我想知道,在生成环境中是否还有其他设置可以使用Datastax Java驱动程序在与Cassandra建立连接时获得更好的性能?

/**
 * Creating Cassandra connection using Datastax driver
 *
 */
private DatastaxConnection() {

    try{
        builder = Cluster.builder();
        builder.addContactPoint("some-node");

        // Can anybody explain me what does below piece of code do?

        builder.poolingOptions().setCoreConnectionsPerHost(
                HostDistance.LOCAL,
                builder.poolingOptions().getMaxConnectionsPerHost(HostDistance.LOCAL));

        // And also what does below piece of code is doing?       
        cluster = builder
                .withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE)
                .withReconnectionPolicy(new ConstantReconnectionPolicy(100L))
                .build();

        StringBuilder s = new StringBuilder();
        Set<Host> allHosts = cluster.getMetadata().getAllHosts();
        for (Host h : allHosts) {
            s.append("[");
            s.append(h.getDatacenter());
            s.append("-");
            s.append(h.getRack());
            s.append("-");
            s.append(h.getAddress());
            s.append("]");
        }
        System.out.println("Cassandra Cluster: " + s.toString());

        session = cluster.connect("testdatastaxks");
    } catch (NoHostAvailableException e) {

    } catch (Exception e) {

    }
}
Run Code Online (Sandbox Code Playgroud)

我的首要任务是: -

  • 在本地数据中心过滤出Cassandra节点.因此,在连接池中,它只有本地数据中心Cassandra节点.
  • 并且在使用具有某些特定设置的Datastax java驱动程序时获得最佳性能.

我知道某些设置可能会在不同的环境中有所不同,但可能会有一些设置,每个人都必须遵循这些设置,以便在使用Datastax Java驱动程序进行Cassandra连接时获得最佳性能.

就像我之前使用的Astyanax中的一个例子一样,你需要使用TOKEN_AWARE ......

因此在使用Datastax java驱动程序时,应该有一些最佳设置或推荐?

小智 4

根据本地数据中心过滤掉 Cassandra 节点。因此在连接池中将只有本地数据中心的 Cassandra 节点

然后您需要使用DCAwareRoundRobinPolicy

就像我之前使用的 Astyanax 中的例子一样,你需要使用 TOKEN_AWARE...

对于 DataStax Java 驱动程序也是如此,它称为TokenAwarePolicy,可以在上面引用的 DCAwareRoundRobinPolicy 之上使用。

我知道某些设置在不同的环境中可能会有所不同,但在使用 Datastax Java 驱动程序建立 Cassandra 连接时,每个人都必须遵循一些设置才能获得最佳性能。

我不能代表“所有人”,但除了如上所述正确选择负载平衡策略之外,其余的很可能取决于环境。但当然,如果您非常关心性能,那么最好尝试一下配置中的各种设置和一些实际的工作负载,看看是否有帮助。