简体   繁体   English

Mongodb 异步与同步 Java 驱动程序

[英]Mongodb Async vs Sync Java driver

I´m quite confused about java drivers for Mongodb. Reading the official documentation it seems that you can use the normal MondoDB Driver or the MongoDB Async Driver.我对 Mongodb 的 java 驱动程序感到很困惑。阅读官方文档,您似乎可以使用普通的 MondoDB 驱动程序或 MongoDB 异步驱动程序。

The first question is: Can I use both in the same application or I have to choose one?第一个问题是:我可以在同一个应用程序中同时使用两者还是必须选择一个?

Trying to use the Async driver I found things that I used to do (with the normal driver) in which I get a bit lost now.尝试使用 Async 驱动程序时,我发现我以前(使用普通驱动程序)做的事情现在有点迷路了。 For example, I used to do this:例如,我曾经这样做:

FindIterable<Document> iterable = db.getCollection("my_coll").find(query);
String json = JSON.serialize(iterable);

And now I really don´t know how to convert the result into a json string since they have not included the JSON class from the Async driver.现在我真的不知道如何将结果转换为 json 字符串,因为它们没有包含异步驱动程序中的JSON class。 Second question: If I cannot use both drivers at the same time, how can I then serialize a FindIterable<Document> ?第二个问题:如果我不能同时使用两个驱动程序,我该如何序列化FindIterable<Document>

The answers are: 答案是:

  • Yes of course you can use both drivers. 是的,你当然可以使用这两种驱动程序。 In fact, if you really care about performance in your application you should use the Sync driver for those actions that you need a response from MongoDB (like find()). 实际上,如果您真的关心应用程序的性能,那么您应该使用Sync驱动程序来处理那些需要MongoDB响应的操作(比如find())。 And you will use the Async driver for the ones that you don't really need it, for "fire and forget" actions (like insert or update). 并且您将使用Async驱动程序来处理那些您并不真正需要的驱动程序,以用于“即发即忘”操作(例如插入或更新)。
  • So the serialization question gets answered from the above. 因此,序列化问题得到了上述答案。 If you get a response you are using the sync driver, therefore you can keep using JSON class: 如果您收到响应,那么您正在使用同步驱动程序,因此您可以继续使用JSON类:

JSON.serialize(iterable);

One thing to be conscious of is that the two drivers share some dependencies.需要注意的一件事是这两个驱动程序共享一些依赖项。 Try to align the versions of the drivers such that they are both expecting the same version of their common dependencies otherwise you can end up with some 'Class not found' type issues resulting from the class loader picking an incompatible version of a class for one or other of the drivers.尝试对齐驱动程序的版本,以便它们都期望相同版本的共同依赖项,否则您可能会遇到一些“未找到类”类型的问题,这是由于 class 加载程序为一个或一个选择了 class 的不兼容版本而导致的其他司机。

I'd add that the use-case I found the async driver great for was a large export style query, where I had to return a large amount of data from a web service.我要补充一点,我发现异步驱动程序非常适合的用例是大型导出样式查询,我必须从 web 服务返回大量数据。 In this case I had to return a large CSV. Previously this method would use up a lot of RAM building up the whole set of CSV data up before writing it to the client.在这种情况下,我必须返回一个大的 CSV。以前,此方法会占用大量 RAM,在将其写入客户端之前构建整套 CSV 数据。 Using the async driver I could read small batches (500 records at a time seemed optimal) from MongoDB and write them to the browser client in a chunked response.使用异步驱动程序,我可以从 MongoDB 读取小批量(一次 500 条记录似乎是最佳的),并将它们以分块响应的形式写入浏览器客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM