1

I'm using java-sdk for inserting documents to cosmosdb. From the driver I was not able to find any java api to do asynchronous insert, however I saw options for .Net api,

Is there a way(natively) to insert documents in background without making the client wait.

lambodar
  • 3,495
  • 5
  • 34
  • 58

1 Answers1

2

I reviewed the javadocs & source code of Azure DocumentDB SDK for Java, unfortunately, there is not any native method to support the asynchronous operation. So if you have to need asynchronous feature, there are two workaround ways below.

  1. Using an asynchronous http client to call Cosmos REST APIs, such as OkHttp.
  2. Azure DocumentDB support MongoDB protocol, so I think it's possible to use MongoDB Async Java Driver to satisfy your needs, but I was failed due to the offical MongoDB Async Java Driver seems to not support Azure DocumentDB. However, there is a third party MongoDB Asynchronous Java Driver mongodb-async-driver, which I tried to connect to Azure DocumentDB with MongoDB protocol successfully, but its APIs are different from the MongoDB offical.

    As reference, Here is my sample code using the third party driver which be download here, without maven repo.

    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()));
            }
        }
    
    }
    

Hope it helps.

Peter Pan
  • 23,476
  • 4
  • 25
  • 43
  • thanks much @peter, may be I will give a try to the mongoDB driver, is there any plan for MS to enhance the SDK to support async because it's something everyone want? – lambodar Jun 09 '17 at 13:47
  • @Iswain You can post a feedback to https://feedback.azure.com/forums/34192--general-feedback which help MS to collect customer's suggestions, ideas, etc. – Peter Pan Jun 27 '17 at 12:00