Delete All from Java SDK in Azure Cosmos DB

Overview

After getting the document, specify the document ID and PartitionKey and delete it.

code


		// 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 = "yourkey";

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

		String collectionLink = String.format("/dbs/%s/colls/%s", DATABASE_ID, COLLECTION_ID); //Specify appropriately

		String q = "SELECT * FROM container1";
		//All documents
		List<Document> results = client //
				.queryDocuments(collectionLink, q, queryOptions).getQueryIterable().toList();

		for (Document doc : results) {
			System.err.println(doc);
			String documentLink = doc.getSelfLink();
			RequestOptions options = new RequestOptions();
			// check your configuration of cosmos db container
			String partitionKey = ((org.json.JSONObject) doc.get("item")).getString("xxx");
			options.setPartitionKey(new PartitionKey(partitionKey));

			client.deleteDocument(documentLink, options);
			System.err.println("deleted: " + documentLink);
		}
		client.close();


Impressions

You must specify the value, not the Patition key. Since "SELECT * FROM c" can be done, it seems that "DELETE * FROM c" can be done, but it seems that it cannot be done.

Recommended Posts

Delete All from Java SDK in Azure Cosmos DB
Upsert from Java SDK to Azure Cosmos DB
Select * from Java SDK to Azure Cosmos DB
Delete documents that don't have PK in Azure Cosmos DB (Java SDK)
Azure functions in java
Update if the document already exists in Azure Cosmos DB Java SDK, create new if it does not exist
Create Azure Functions in Java
Append text to BlobItem in Azure BlobStorage SDK Java V8
Run Java application in Azure Batch
[Java] Get data from DB using singleton service in Spring (Boot)
[Java] Reflash all items in BeanClass
Try using Microsoft Azure SDK For SQL API of Azure Cosmos DB Service 4.6
Study Deep Learning from scratch in Java.
Call Java method from JavaScript executed in Java
OCR in Java (character recognition from images)
HA/DR support in Couchbase (Java SDK commentary)
Reverse Key from Value in Java Map
Using JavaScript from Java in Rhino 2021 version
ERRORCODE = -4471 occurs in Java application using Db2.
All same hash code string in Java
Delete blank rows / columns in Java Excel
Get history from Zabbix server in Java
Call Visual Recognition in Watson Java SDK
Get Timestamp with Azure BlobStorage Java SDK
GetInstance () from a @Singleton class in Groovy from Java
Java method call from RPG (method call in own class)
How to get Class from Element in Java
Text extraction in Java from PDF with pdfbox-2.0.8
Delete all records in a MySQL database table
Capture and save from selenium installation in Java
Get unixtime (seconds) from ZonedDateTime in Scala / Java
Add, read, and delete Excel comments in Java
[Deep Learning from scratch] in Java 3. Neural network
Generate OffsetDateTime from Clock and LocalDateTime in Java
[Java] Get KFunction from Method / Constructor in Java [Kotlin]