LoginSignup
1
1

More than 5 years have passed since last update.

Azure Cosmos DB に Java SDK からUpsert

Last updated at Posted at 2019-06-27

Maven

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-documentdb</artifactId>
    <version>2.4.0</version>
</dependency>

Java



import java.util.Date;
import java.util.List;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;
import com.microsoft.azure.documentdb.ConnectionPolicy;
import com.microsoft.azure.documentdb.ConsistencyLevel;
import com.microsoft.azure.documentdb.Document;
import com.microsoft.azure.documentdb.DocumentClient;
import com.microsoft.azure.documentdb.FeedOptions;

public class HelloDocumentDb001UpsertDocument {

    static String DATABASE_ID = "sandbox1";
    static String COLLECTION_ID = "container1";

    public static void main(String[] args) throws Exception {

        // Azure Cosmos DB Libraries for Java
        // https://docs.microsoft.com/ja-jp/java/api/overview/azure/cosmosdb?view=azure-java-stable

        FeedOptions queryOptions = new FeedOptions();
        queryOptions.setEnableCrossPartitionQuery(true);

        String host = "yourhost";

        // Get key from Azure Web Console
        // read write key
        String key = "xxx";

        DocumentClient client = new DocumentClient("https://" //
                + host //
                + ".documents.azure.com:443", key, //
                new ConnectionPolicy(), ConsistencyLevel.Eventual);

        // 適当なクラスを用意するとJSONに変換してくれる
        Doc d = new Doc("002", new Date(), "Hello", "Hello");

        // Nameなのか、IDなのか、ドキュメントをみてもはっきりしない。
        String collectionLink = String.format("/dbs/%s/colls/%s", DATABASE_ID, COLLECTION_ID);

        int response = client.upsertDocument(collectionLink, d, null, true).getStatusCode();
        System.err.println(response);

        client.close();
    }
}


1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1