我正在编写针对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
构建密钥空间时,代码可以正常工作。
答案 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());