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);
//If you prepare a suitable class, it will be converted to JSON
Doc d = new Doc("002", new Date(), "Hello", "Hello");
//Whether it is a Name or an ID is not clear even if you look at the document.
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();
}
}
Recommended Posts