Upsert from Java SDK to Azure Cosmos DB

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

Upsert from Java SDK to Azure Cosmos DB
Select * from Java SDK to Azure Cosmos DB
Delete All from Java SDK in Azure Cosmos DB
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Delete documents that don't have PK in Azure Cosmos DB (Java SDK)
From Java to Ruby !!
Migration from Cobol to JAVA
New features from Java7 to Java8
Connect from Java to PostgreSQL
Connect to DB with Java
From Ineffective Java to Effective Java
Append text to BlobItem in Azure BlobStorage SDK Java V8
Java to be involved from today
From Java to VB.NET-Writing Contrast Memo-
Java, interface to start from beginner
Change DB from SQLite to MySQL
The road from JavaScript to Java
[Java] Conversion from array to List
Convert from java UTC time to JST time
Connect from Java to MySQL using Eclipse
Try using Microsoft Azure SDK For SQL API of Azure Cosmos DB Service 4.6
From installing Eclipse to executing Java (PHP)
Add Document to Azure Search Service (Java)
Post to Slack from Play Framework 2.8 (Java)
Java: How to send values from Servlet to Servlet
[Java] Flow from source code to execution
Introduction to monitoring from Java Touching Prometheus
Precautions when migrating from VB6.0 to JAVA
Creating Java Web Applications to Azure Web Apps
Get Timestamp with Azure BlobStorage Java SDK
Memo for migration from java to kotlin
Type conversion from java BigDecimal type to String type
Update if the document already exists in Azure Cosmos DB Java SDK, create new if it does not exist
Try calling Watson NLU that seems to support Japanese from the Java SDK
[Java] From two Lists to one array list
Run R from Java I want to run rJava
Connect to Aurora (MySQL) from a Java application
Deploy Java web app to Azure with maven
To become a VB.net programmer from a Java shop
How to dump from database (DB) to seeds file
Migrate from Java to Server Side Kotlin + Spring-boot
Java upload and download notes to Azure storage
[Java] Convert DB code to code value using enum
How to get Class from Element in Java
[Java] How to switch from open jdk to oracle jdk
I want to write quickly from java to sqlite
Minecraft BE server development from PHP to Java
[Java] Introduction to Java
Introduction to java
Launch Docker from Java to convert Office documents to PDF
Convert Java enum enums and JSON to and from Jackson
[Java] I want to calculate the difference from the date
How to jump from Eclipse Java to a SQL file
How to write Scala from the perspective of Java
I tried to operate SQS using AWS Java SDK
[Java] How to extract the file name from the path
Use aggregate queries (Count) with Azure CosmosDB Java SDK
6 features I missed after returning to Java from Scala
Try calling IBM Watson Assistant 2018-07-10 from the Java SDK.
Java development for beginners to start from 1-Vol.1-eclipse setup