Use aggregate queries (Count) with Azure CosmosDB Java SDK

I'm addicted to using aggregate queries in CosmosDB from the JavaSDK, so I'll take note of it.

If you do not set the VALUE clause in the query, you will get the following error:

java.lang.IllegalStateException: com.microsoft.azure.documentdb.DocumentClientException: Message: {"Errors":["Cross partition query only supports 'VALUE <AggreateFunc>' for aggregates."]}
Caused by: com.microsoft.azure.documentdb.DocumentClientException: Message: {"Errors":["Cross partition query only supports 'VALUE <AggreateFunc>' for aggregates."]}

Also, if setEnableCrossPartitionQuery is not set to true, the following error will occur.

java.lang.IllegalStateException: com.microsoft.azure.documentdb.DocumentClientException: Cross partition query is required but disabled. Please set x-ms-documentdb-query-enablecrosspartition to true, specify x-ms-documentdb-partitionkey, or revise your query to avoid this exception.
Caused by: com.microsoft.azure.documentdb.DocumentClientException: Cross partition query is required but disabled. Please set x-ms-documentdb-query-enablecrosspartition to true, specify x-ms-documentdb-partitionkey, or revise your query to avoid this exception.

Below is the Java code.


		// Azure Cosmos DB Libraries for Java
		// https://docs.microsoft.com/ja-jp/java/api/overview/azure/cosmosdb?view=azure-java-stable

		String host = "yourhost";
		String container_id = "yourcontainer";
		String database = "yourdatabase";

		// Get key from Azure Web Console
		// read write key
		String key = "yourkey";

		String endPoint = "https://" + host + ".documents.azure.com:443";

		DocumentClient client = new DocumentClient(endPoint, key, //
				new ConnectionPolicy(), ConsistencyLevel.Session);

		String collectionLink = String.format("/dbs/%s/colls/%s", database, container_id);

		String q = String.format("SELECT VALUE count(1) FROM c");
		// IF (without 'VALUE' in query)
//		java.lang.IllegalStateException: com.microsoft.azure.documentdb.DocumentClientException: Message: {"Errors":["Cross partition query only supports 'VALUE <AggreateFunc>' for aggregates."]}
//		Caused by: com.microsoft.azure.documentdb.DocumentClientException: Message: {"Errors":["Cross partition query only supports 'VALUE <AggreateFunc>' for aggregates."]}

		FeedOptions feedOptions = new FeedOptions();
		feedOptions.setEnableCrossPartitionQuery(true);
		// IF (EnableCrossPartitionQuery = false)
//		java.lang.IllegalStateException: com.microsoft.azure.documentdb.DocumentClientException: Cross partition query is required but disabled. Please set x-ms-documentdb-query-enablecrosspartition to true, specify x-ms-documentdb-partitionkey, or revise your query to avoid this exception.
//		Caused by: com.microsoft.azure.documentdb.DocumentClientException: Cross partition query is required but disabled. Please set x-ms-documentdb-query-enablecrosspartition to true, specify x-ms-documentdb-partitionkey, or revise your query to avoid this exception.

		try {

			List<Object> results = client //
					.queryAggregateValues(collectionLink, q, feedOptions);

			System.err.println(results.get(0));

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			client.close();
		}


that's all.

Recommended Posts

Use aggregate queries (Count) with Azure CosmosDB Java SDK
Use Azure Bing SpellCheck with Java
Get Timestamp with Azure BlobStorage Java SDK
Use Lambda Layers with Java
Use SpatiaLite with Java / JDBC
Use java with MSYS and Cygwin
Use JDBC with Java and Scala.
Use Java 11 with Google Cloud Functions
[JaCoCo (Java Code Coverage)] Use with NetBeans
[CircleCI 2.0] [Java] [Maven] [JUnit] Aggregate JUnit test results with CircleCI 2.0
Deploy Java web app to Azure with maven
How to use Java framework with AWS Lambda! ??
I want to use java8 forEach with index
Get block information with Hyperledger Iroha's Java SDK
How to use Java API with lambda expression
Use Matplotlib from Java or Scala with Matplotlib4j
[JAVA] [Spring] [MyBatis] Use IN () with SQL Builder
Select * from Java SDK to Azure Cosmos DB