我想知道是否有办法限制 cassandra java 驱动程序同时执行的查询数量?
目前,我执行了很多查询,如下所示:
...
PreparedStatement stmt = session.prepare("SELECT * FROM users WHERE id = ?");
BoundStatement boundStatement = new BoundStatement(stmt);
List<ResultSetFuture> futures = Lists.newArrayListWithExpectedSize(list.length);
for(String id : list ) {
futures.add(session.executeAsync(boundStatement.bind(id)));
}
for (ListenableFuture<ResultSet> future : futures) {
ResultSet rs = future.get();
... // do some stuff
}
不幸的是,这可能会导致 NoHostAvailableException。
感谢您。
您可以使用信号量来限制并发查询的数量:
final Semaphore semaphore = new Semaphore(numberOfConcurrentQueries);
...
semaphore.acquire();
try {
ResultSetFuture future = session.executeAsync("...");
Futures.addCallback(future, new FutureCallback<ResultSet>() {
@Override
public void onSuccess(ResultSet result) {
semaphore.release();
}
@Override
public void onFailure(Throwable t) {
semaphore.release();
}
});
} catch (Exception e) {
semaphore.release();
}
但归根结底,情况并没有那么不同:而不是得到一个NoHostAvailableException
当超过容量时,信号量将阻塞(或者如果您使用定时版本的获取则抛出)。因此,您可能还想对触发这些查询的组件应用背压。
您可能还想调整连接池以调整容量,请参阅our docs https://github.com/datastax/java-driver(这是针对 2.1 的,如果您使用的是 2.0,请使用页面顶部的下拉菜单)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)