Azure 使用java驱动程序异步写入documentdb
我正在使用JavaSDK向cosmosdb插入文档。从驱动程序中,我找不到任何java api来执行异步插入,但是我看到了Azure 使用java驱动程序异步写入documentdb,azure,asynchronous,azure-cosmosdb,azure-java-sdk,Azure,Asynchronous,Azure Cosmosdb,Azure Java Sdk,我正在使用JavaSDK向cosmosdb插入文档。从驱动程序中,我找不到任何java api来执行异步插入,但是我看到了 是否有一种方法(本机)可以在后台插入文档而不让客户端等待。我查看了Azure DocumentDB SDK for Java的javadocs和源代码,不幸的是,没有任何本机方法支持异步操作。所以,如果您需要异步特性,下面有两种解决方法 使用异步http客户端进行调用,例如 Azure DocumentDB支持MongoDB协议,因此我认为可以使用它来满足您的需求,但我失败
是否有一种方法(本机)可以在后台插入文档而不让客户端等待。我查看了Azure DocumentDB SDK for Java的javadocs和源代码,不幸的是,没有任何本机方法支持异步操作。所以,如果您需要异步特性,下面有两种解决方法
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import com.allanbank.mongodb.MongoClient;
import com.allanbank.mongodb.MongoCollection;
import com.allanbank.mongodb.MongoDatabase;
import com.allanbank.mongodb.MongoFactory;
import com.allanbank.mongodb.MongoIterator;
import com.allanbank.mongodb.bson.Document;
import com.allanbank.mongodb.builder.Find;
public class Test {
public static void main(String[] args) throws InterruptedException, ExecutionException {
String connectionString = "mongodb://<user>:<password>@<documentdb-name>.documents.azure.com:10255/?ssl=true&replicaSet=globaldb";
MongoClient mongo = MongoFactory.createClient(connectionString);
String dbName = "testdb";
MongoDatabase database = mongo.getDatabase(dbName);
String collName = "test";
MongoCollection collection = database.getCollection(collName);
Future<Long> future = collection.countAsync();
System.out.printf("There are %,d documents in the collection.%n", future.get());
MongoIterator iter = collection.find(Find.builder().build());
while (iter.hasNext()) {
System.out.println(((Document) iter.next()));
}
}
}
import java.util.concurrent.ExecutionException;
导入java.util.concurrent.Future;
导入com.allanbank.mongodb.MongoClient;
import com.allanbank.mongodb.MongoCollection;
导入com.allanbank.mongodb.MongoDatabase;
import com.allanbank.mongodb.MongoFactory;
进口com.allanbank.mongodb.MongoIterator;
导入com.allanbank.mongodb.bson.Document;
导入com.allanbank.mongodb.builder.Find;
公开课考试{
公共静态void main(字符串[]args)引发InterruptedException、ExecutionException{
String connectionString=“mongodb://:@.documents.azure.com:10255/?ssl=true&replicaSet=globaldb”;
MongoClient mongo=MongoFactory.createClient(connectionString);
字符串dbName=“testdb”;
MongoDatabase database=mongo.getDatabase(dbName);
字符串collName=“test”;
MongoCollection collection=database.getCollection(collName);
Future=collection.countAsync();
System.out.printf(“集合中有%d个文档。%n”,future.get());
MongoIterator iter=collection.find(find.builder().build());
while(iter.hasNext()){
System.out.println(((文档)iter.next());
}
}
}
希望能有所帮助。非常感谢@peter,也许我会尝试一下mongoDB驱动程序,MS是否有计划增强SDK以支持异步,因为它是每个人都想要的?@Iswain您可以发布反馈,帮助MS收集客户的建议、想法等。