upsert from Java SDK

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"); d.setItem(new DocItem("DOC_ID_XXX", 1)); { DocHistory[] histories = new DocHistory[1]; histories[0] = new DocHistory(new Date(), "Hiroki Oya", "Init from Java"); d.setHistories(histories); } // 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(); }}