0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Azure CosmosDB でJavaSDKから Delete All (全削除)

Last updated at Posted at 2019-07-01

概要

文書を取得してから、文書のIDとPartitionKeyを指定して削除する。

コード


		// 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); // 適当に指定する

		String q = "SELECT * FROM container1";
		// 全文書
		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();


所感

Patitionのキーを指定するのではなく、値を指定する必要があります。
「SELECT * FROM c」ができるのだから「DELETE * FROM c」ができてもよさそうなのだが、できないらしい。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?