我正在尝试使用 Mongo Java Driver 3.0.4 在大集合上运行聚合。在我收藏的小样本上,一切都很好,但是当我尝试在整个收藏中执行它时,我最终得到了MongoCursorNotFoundException. 我发现这是 Cursor 的问题,它超时并被服务器关闭。
但是,我无法理解如何设置此选项。该aggregate() 函数返回一个AggregateIterable,它只有useCursor看起来有点相关的方法。另一方面, find() 函数返回FindIterable,它有一个方便的noCursorTimeout(Boolean)方法。我不明白为什么它在 find 上如此简单,但是这个选项在聚合上没有明显的方法。我应该如何确保 Cursor 在一段时间后不会失败?
到目前为止,我的代码是这样的。
AggregateIterable<Document> iterable = db.getCollection("tweets").aggregate(asList( new Document("$sort", new Document("timestamp_ms", 1)),
new Document("$group", new Document("_id", "$relatedTrend")
.append("count", new Document("$sum", 1))
.append("tweets", new Document("$push", new Document("timestamp_millis", "$timestamp_ms")))))).allowDiskUse(true);
iterable.forEach(new Block<Document>() {
@Override
public void apply(final Document document) {
//parse field "tweets" of document and do a lot of calculations.
}
});
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个 Java 应用程序,它的行为类似于事物,在 AWS 中发布数据。文档有这个代码示例:
String clientEndpoint = "<prefix>.iot.<region>.amazonaws.com"; // replace <prefix> and <region> with your own
String clientId = "<unique client id>"; // replace with your own client ID. Use unique client IDs for concurrent connections.
String certificateFile = "<certificate file>"; // X.509 based certificate file
String privateKeyFile = "<private key file>"; // PKCS#1 or PKCS#8 PEM encoded private key file
// SampleUtil.java and its dependency PrivateKeyReader.java can be copied from the sample source code.
// Alternatively, you could …Run Code Online (Sandbox Code Playgroud)