在Cassandra 4.X中,我可以用什么用作替代结果?
我必须将Cassandra升级到4.x。 这是先前用Cassandra 3.5编写的代码
protected <T> Stream<T> getAll(Stream<Statement> statements, Mapper<T> mapper) {
List<ResultSetFuture> futures = statements
.peek(p -> cassandraReads.inc())
.map(s -> session.session().executeAsync(s))
.collect(Collectors.toList());
return futures.stream()
.map(ResultSetFuture::getUninterruptibly)
.map(mapper::map)
.flatMap(r -> StreamSupport.stream(r.spliterator(), false));
}
,我做了一些更改...
List<CompletionStage<AsyncResultSet>> futures = statements
.peek(p -> cassandraReads.inc())
.map(s -> session.getSession().executeAsync(s))
.collect(Collectors.toList());
但是我应该用什么来代替.map(resultsetFuture :: getunressruption)
,因为它已被删除。 由于我是卡桑德拉(Cassandra)和异步编程的新手,因此将不胜感激。
I have to upgrade Cassandra to 4.x.
This was the code previously written in cassandra 3.5
protected <T> Stream<T> getAll(Stream<Statement> statements, Mapper<T> mapper) {
List<ResultSetFuture> futures = statements
.peek(p -> cassandraReads.inc())
.map(s -> session.session().executeAsync(s))
.collect(Collectors.toList());
return futures.stream()
.map(ResultSetFuture::getUninterruptibly)
.map(mapper::map)
.flatMap(r -> StreamSupport.stream(r.spliterator(), false));
}
I have done some changes...
List<CompletionStage<AsyncResultSet>> futures = statements
.peek(p -> cassandraReads.inc())
.map(s -> session.getSession().executeAsync(s))
.collect(Collectors.toList());
BUT what should I use in place of .map(ResultSetFuture::getUninterruptibly)
since it has been removed now.
Since I am new to Cassandra and asynchronous programming any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您已经在代码中混合异步并阻止了调用,因此我建议您完全阻止并使用
session.execute
而不是session.executeasync
。这将使事情变得更加容易。然后,您可以按照以下方式重写代码:注意:我正在稍微更改您的
mapper
接口,我假设它将是:(您实际上可以替换此
mapper
function&lt; row,t&gt;
btw。)除此之外,如埃里克·拉米雷斯(Erick Ramirez)所说,请查看此页面了解如何使用驱动程序4.x编写正确的async代码。
Since you are already mixing async and blocking calls in your code, I'd suggest that you go fully blocking and use
Session.execute
instead ofSession.executeAsync
. It's going to make things much easier. Then you could rewrite your code as follows:Note: I'm changing slightly your
Mapper
interface, which I assume would be:(You could actually replace this
Mapper
interface with justFunction<Row, T>
btw.)Other than that, as Erick Ramirez said, please have a look at this page to understand how to write proper async code with driver 4.x.
异步编程的API实际上不是由您连接到的Cassandra群集的版本确定的。重要的是您正在使用的Java驱动程序的版本。
如果您仍在使用Java驱动程序3.X,则您的代码应在大部分时间继续工作,直到Java驱动程序3.11。参见 asynchronous在Java驱动程序中的编程3.11 。
直到您升级到Java驱动程序4.x,您才会遇到破坏更改,因为V4.X与Java驱动程序的早期版本不兼容(请参见升级指南有关详细信息)。
如果您升级到Java驱动程序4.x,则可以找到在此处进行异步编程的工作示例。干杯!
The API for asynchronous programming isn't actually determined by the version of the Cassandra cluster you're connecting to. What matters is the version of the Java driver you're using.
If you're still using Java driver 3.x, your code should continue to work for the most part until Java driver 3.11. See Asynchronous programming in Java driver 3.11.
It isn't until you upgrade to the Java driver 4.x that you will run into breaking changes since v4.x is not binary-compatible with earlier versions of the Java driver (see Upgrade guide for details).
If you do upgrade to the Java driver 4.x, you can find working examples for asynchronous programming here. Cheers!