使用使用CqlFamilyFactory构建的键空间时,Astyanax会话未初始化

时间:2014-08-19 21:30:36

标签: cassandra astyanax

我正在编写针对Astyanax EmbeddedCassandra的测试用例。 我尝试使用CqlFamilyFactory来构建上下文,并在尝试执行dropKeyspace时获取NPE:

这是初始化代码:

    ConnectionPoolConfiguration cpConfig = new ConnectionPoolConfigurationImpl("cassandra connection pool")
        .setPort(9171)
        .setSeeds("127.0.0.1:9171")
        .setMaxConnsPerHost(4);

    m_context = new AstyanaxContext.Builder()
        .forCluster("test-cluster")
        .forKeyspace("testkeyspace")
        .withAstyanaxConfiguration(new AstyanaxConfigurationImpl().setDiscoveryType(NodeDiscoveryType.NONE))
        .withConnectionPoolConfiguration(cpConfig)
        .withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
        .buildKeyspace(CqlFamilyFactory.getInstance());

    m_context.start();

    m_context.getClient().dropKeyspace();

由于会话CqlKeyspaceImpl:276 (astyanax-cql-2.0.1)处为空,这将导致NPE:

return new CqlOperationResultImpl<SchemaChangeResult>(session.execute("DROP KEYSPACE " + keyspaceName), null);

我猜我没有指定上下文创建会话所需的内容,但我找不到很多使用CqlFamilyFactory的例子。

使用ThriftFamilyFactory构建密钥空间时,代码可以正常工作。

1 个答案:

答案 0 :(得分:1)

要使CqlFamilyFactory初始化会话,需要调用ConnectionPoolProxy.setHosts,NodeDiscoveryImpl.update可以调用它。它需要hostSupplier才能使用。

以下示例代码适用于TOKEN_AWARE或DISCOVERY_SERVICE:

Supplier<List<Host>> hostSupplier = new Supplier<List<Host>>() {
            @Override
            public List<Host> get() {
                List<Host> hosts = new ArrayList<>();
                for (HostAndPort hostAndPort : properties.getSeeds()) {
                    hosts.add(new Host(hostAndPort.toString(), 9160));
                }
                return hosts;
            }
        };
AstyanaxConfiguration astyanaxConfiguration = new AstyanaxConfigurationImpl()
                .setDiscoveryType(NodeDiscoveryType.TOKEN_AWARE)   
                .setCqlVersion(properties.getCQLVersion())
                .setTargetCassandraVersion(properties.getVersion());
ConnectionPoolConfiguration poolConfiguration = new ConnectionPoolConfigurationImpl("MY CONNECTION POOL")
                .setSeeds(Joiner.on(",").join(properties.getSeeds()))
                .setPort(properties.getPort())
                .setMaxConnsPerHost(3)
                .setAuthenticationCredentials(new SimpleAuthenticationCredentials(properties.getUsername(), properties.getPassword()));
context = new AstyanaxContext.Builder()
                .forCluster(properties.getClusterName())
                .forKeyspace(properties.getKeyspace())
                .withHostSupplier(hostSupplier)
                .withAstyanaxConfiguration(astyanaxConfiguration)
                .withConnectionPoolConfiguration(poolConfiguration)
                .withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
                .buildKeyspace(CqlFamilyFactory.getInstance());

可以找到更多示例代码https://github.com/Netflix/astyanax/blob/master/astyanax-test/src/main/java/com/netflix/astyanax/cql/test/utils/AstyanaxContextFactory.java