diff --git a/CHANGELOG.md b/CHANGELOG.md
index b036ad58c..9f235cfe8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,18 @@
# Changelog
+## [2.17.1](https://github.com/googleapis/java-datastore/compare/v2.17.0...v2.17.1) (2023-09-11)
+
+
+### Dependencies
+
+* Update dependency com.google.cloud:google-cloud-shared-dependencies to v3.15.0 ([#1184](https://github.com/googleapis/java-datastore/issues/1184)) ([6cbb795](https://github.com/googleapis/java-datastore/commit/6cbb79589e7eb8648c734bd02579a93423aa4e13))
+* Update dependency org.easymock:easymock to v5.2.0 ([#1180](https://github.com/googleapis/java-datastore/issues/1180)) ([3e62380](https://github.com/googleapis/java-datastore/commit/3e62380ae2793437aa9c4d21fa4484a8896cb49e))
+
+
+### Documentation
+
+* Marking AggregationResult#get as Obsolete ([#1185](https://github.com/googleapis/java-datastore/issues/1185)) ([252f854](https://github.com/googleapis/java-datastore/commit/252f8549895250ced3aecf5c082cb0b41ea12472))
+
## [2.17.0](https://github.com/googleapis/java-datastore/compare/v2.16.3...v2.17.0) (2023-08-22)
diff --git a/README.md b/README.md
index 662c0b5e4..fd1b011ed 100644
--- a/README.md
+++ b/README.md
@@ -42,7 +42,7 @@ If you are using Maven without the BOM, add this to your dependencies:
com.google.cloud
google-cloud-datastore
- 2.16.3
+ 2.17.0
```
@@ -57,13 +57,13 @@ implementation 'com.google.cloud:google-cloud-datastore'
If you are using Gradle without BOM, add this to your dependencies:
```Groovy
-implementation 'com.google.cloud:google-cloud-datastore:2.16.3'
+implementation 'com.google.cloud:google-cloud-datastore:2.17.0'
```
If you are using SBT, add this to your dependencies:
```Scala
-libraryDependencies += "com.google.cloud" % "google-cloud-datastore" % "2.16.3"
+libraryDependencies += "com.google.cloud" % "google-cloud-datastore" % "2.17.0"
```
@@ -262,6 +262,10 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-datastore/tre
| --------------------------- | --------------------------------- | ------ |
| Native Image Datastore Sample | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/native-image-sample/src/main/java/com/example/datastore/NativeImageDatastoreSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/native-image-sample/src/main/java/com/example/datastore/NativeImageDatastoreSample.java) |
| Quickstart Sample | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/QuickstartSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/QuickstartSample.java) |
+| Avg Aggregation On Kind | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationOnKind.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationOnKind.java) |
+| Avg Aggregation With Limit | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationWithLimit.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationWithLimit.java) |
+| Avg Aggregation With Order By | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationWithOrderBy.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationWithOrderBy.java) |
+| Avg Aggregation With Property Filter | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationWithPropertyFilter.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationWithPropertyFilter.java) |
| Count Aggregation In Transaction | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/CountAggregationInTransaction.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/aggregation/CountAggregationInTransaction.java) |
| Count Aggregation On Kind | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/CountAggregationOnKind.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/aggregation/CountAggregationOnKind.java) |
| Count Aggregation With Gql Query | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/CountAggregationWithGqlQuery.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/aggregation/CountAggregationWithGqlQuery.java) |
@@ -269,6 +273,12 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-datastore/tre
| Count Aggregation With Order By | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/CountAggregationWithOrderBy.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/aggregation/CountAggregationWithOrderBy.java) |
| Count Aggregation With Property Filter | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/CountAggregationWithPropertyFilter.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/aggregation/CountAggregationWithPropertyFilter.java) |
| Count Aggregation With Stale Read | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/CountAggregationWithStaleRead.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/aggregation/CountAggregationWithStaleRead.java) |
+| Multiple Aggregations In Gql Query | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/MultipleAggregationsInGqlQuery.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/aggregation/MultipleAggregationsInGqlQuery.java) |
+| Multiple Aggregations In Structured Query | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/MultipleAggregationsInStructuredQuery.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/aggregation/MultipleAggregationsInStructuredQuery.java) |
+| Sum Aggregation On Kind | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/SumAggregationOnKind.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/aggregation/SumAggregationOnKind.java) |
+| Sum Aggregation With Limit | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/SumAggregationWithLimit.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/aggregation/SumAggregationWithLimit.java) |
+| Sum Aggregation With Order By | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/SumAggregationWithOrderBy.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/aggregation/SumAggregationWithOrderBy.java) |
+| Sum Aggregation With Property Filter | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/SumAggregationWithPropertyFilter.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/aggregation/SumAggregationWithPropertyFilter.java) |
| Create a union between two filters | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/filters/OrFilterQuery.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/filters/OrFilterQuery.java) |
| Task List | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/google/datastore/snippets/TaskList.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/google/datastore/snippets/TaskList.java) |
@@ -370,7 +380,7 @@ Java is a registered trademark of Oracle and/or its affiliates.
[kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/java-datastore/java11.html
[stability-image]: https://img.shields.io/badge/stability-stable-green
[maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-datastore.svg
-[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-datastore/2.16.3
+[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-datastore/2.17.0
[authentication]: https://github.com/googleapis/google-cloud-java#authentication
[auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes
[predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles
diff --git a/datastore-v1-proto-client/pom.xml b/datastore-v1-proto-client/pom.xml
index 634f8dd6e..a693fcb34 100644
--- a/datastore-v1-proto-client/pom.xml
+++ b/datastore-v1-proto-client/pom.xml
@@ -19,12 +19,12 @@
4.0.0
com.google.cloud.datastore
datastore-v1-proto-client
- 2.17.0
+ 2.17.1
com.google.cloud
google-cloud-datastore-parent
- 2.17.0
+ 2.17.1
jar
diff --git a/google-cloud-datastore-bom/pom.xml b/google-cloud-datastore-bom/pom.xml
index a7b54e305..34a7e2b19 100644
--- a/google-cloud-datastore-bom/pom.xml
+++ b/google-cloud-datastore-bom/pom.xml
@@ -3,7 +3,7 @@
4.0.0
com.google.cloud
google-cloud-datastore-bom
- 2.17.0
+ 2.17.1
pom
com.google.cloud
@@ -52,22 +52,22 @@
com.google.cloud
google-cloud-datastore
- 2.17.0
+ 2.17.1
com.google.api.grpc
grpc-google-cloud-datastore-admin-v1
- 2.17.0
+ 2.17.1
com.google.api.grpc
proto-google-cloud-datastore-v1
- 0.108.0
+ 0.108.1
com.google.api.grpc
proto-google-cloud-datastore-admin-v1
- 2.17.0
+ 2.17.1
diff --git a/google-cloud-datastore/pom.xml b/google-cloud-datastore/pom.xml
index 1d5f83046..a1619a509 100644
--- a/google-cloud-datastore/pom.xml
+++ b/google-cloud-datastore/pom.xml
@@ -2,7 +2,7 @@
4.0.0
google-cloud-datastore
- 2.17.0
+ 2.17.1
jar
Google Cloud Datastore
https://github.com/googleapis/java-datastore
@@ -12,7 +12,7 @@
com.google.cloud
google-cloud-datastore-parent
- 2.17.0
+ 2.17.1
google-cloud-datastore
diff --git a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/AggregationResult.java b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/AggregationResult.java
index 75636c004..a5071d8d8 100644
--- a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/AggregationResult.java
+++ b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/AggregationResult.java
@@ -15,9 +15,7 @@
*/
package com.google.cloud.datastore;
-import static com.google.cloud.datastore.ValueType.DOUBLE;
-import static com.google.cloud.datastore.ValueType.LONG;
-
+import com.google.api.core.ObsoleteApi;
import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import java.util.Map;
@@ -41,6 +39,7 @@ public AggregationResult(Map> properties) {
* 'property_\d'
* @return An aggregation result value for the given alias.
*/
+ @ObsoleteApi("Please use getLong(String) instead, see Github issue #1175 for details.")
public Long get(String alias) {
return getLong(alias);
}
diff --git a/grpc-google-cloud-datastore-admin-v1/pom.xml b/grpc-google-cloud-datastore-admin-v1/pom.xml
index 83076f0de..0d4643da7 100644
--- a/grpc-google-cloud-datastore-admin-v1/pom.xml
+++ b/grpc-google-cloud-datastore-admin-v1/pom.xml
@@ -4,13 +4,13 @@
4.0.0
com.google.api.grpc
grpc-google-cloud-datastore-admin-v1
- 2.17.0
+ 2.17.1
grpc-google-cloud-datastore-admin-v1
GRPC library for google-cloud-datastore
com.google.cloud
google-cloud-datastore-parent
- 2.17.0
+ 2.17.1
diff --git a/pom.xml b/pom.xml
index 8fa45820d..bba6d9fa5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,7 +4,7 @@
com.google.cloud
google-cloud-datastore-parent
pom
- 2.17.0
+ 2.17.1
Google Cloud Datastore Parent
https://github.com/googleapis/java-datastore
@@ -151,7 +151,7 @@
com.google.cloud
google-cloud-shared-dependencies
- 3.14.0
+ 3.15.0
pom
import
@@ -159,27 +159,27 @@
com.google.api.grpc
proto-google-cloud-datastore-admin-v1
- 2.17.0
+ 2.17.1
com.google.api.grpc
grpc-google-cloud-datastore-admin-v1
- 2.17.0
+ 2.17.1
com.google.cloud
google-cloud-datastore
- 2.17.0
+ 2.17.1
com.google.api.grpc
proto-google-cloud-datastore-v1
- 0.108.0
+ 0.108.1
com.google.cloud.datastore
datastore-v1-proto-client
- 2.17.0
+ 2.17.1
com.google.api.grpc
@@ -197,7 +197,7 @@
org.easymock
easymock
- 5.1.0
+ 5.2.0
test
diff --git a/proto-google-cloud-datastore-admin-v1/pom.xml b/proto-google-cloud-datastore-admin-v1/pom.xml
index a0948a03d..55665d8a5 100644
--- a/proto-google-cloud-datastore-admin-v1/pom.xml
+++ b/proto-google-cloud-datastore-admin-v1/pom.xml
@@ -4,13 +4,13 @@
4.0.0
com.google.api.grpc
proto-google-cloud-datastore-admin-v1
- 2.17.0
+ 2.17.1
proto-google-cloud-datastore-admin-v1
Proto library for google-cloud-datastore
com.google.cloud
google-cloud-datastore-parent
- 2.17.0
+ 2.17.1
diff --git a/proto-google-cloud-datastore-v1/pom.xml b/proto-google-cloud-datastore-v1/pom.xml
index cbb5d557e..77305438f 100644
--- a/proto-google-cloud-datastore-v1/pom.xml
+++ b/proto-google-cloud-datastore-v1/pom.xml
@@ -4,13 +4,13 @@
4.0.0
com.google.api.grpc
proto-google-cloud-datastore-v1
- 0.108.0
+ 0.108.1
proto-google-cloud-datastore-v1
PROTO library for proto-google-cloud-datastore-v1
com.google.cloud
google-cloud-datastore-parent
- 2.17.0
+ 2.17.1
diff --git a/samples/install-without-bom/pom.xml b/samples/install-without-bom/pom.xml
index 0097db7b4..d77507645 100644
--- a/samples/install-without-bom/pom.xml
+++ b/samples/install-without-bom/pom.xml
@@ -29,7 +29,7 @@
com.google.cloud
google-cloud-datastore
- 2.16.3
+ 2.17.0
diff --git a/samples/snapshot/pom.xml b/samples/snapshot/pom.xml
index dbdadb49c..81807f24a 100644
--- a/samples/snapshot/pom.xml
+++ b/samples/snapshot/pom.xml
@@ -23,13 +23,14 @@
UTF-8
-
+
com.google.cloud
google-cloud-datastore
- 2.16.3
+ 2.17.1
+
junit
@@ -44,7 +45,6 @@
test
-
diff --git a/samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationOnKind.java b/samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationOnKind.java
new file mode 100644
index 000000000..367837da6
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationOnKind.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2023 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.datastore.aggregation;
+
+// [START datastore_avg_aggregation_query_on_kind]
+
+import static com.google.cloud.datastore.aggregation.Aggregation.avg;
+
+import com.google.cloud.datastore.AggregationQuery;
+import com.google.cloud.datastore.AggregationResult;
+import com.google.cloud.datastore.Datastore;
+import com.google.cloud.datastore.DatastoreOptions;
+import com.google.cloud.datastore.Entity;
+import com.google.cloud.datastore.EntityQuery;
+import com.google.cloud.datastore.Key;
+import com.google.cloud.datastore.Query;
+import com.google.common.collect.Iterables;
+
+public class AvgAggregationOnKind {
+
+ // Instantiates a client.
+ private static final Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
+
+ // The kind for the new entity.
+ private static final String kind = "Sales";
+
+ // Setting up Sales in database
+ private static void setUpSales() {
+ Key sales1Key = datastore.newKeyFactory().setKind(kind).newKey("sales1");
+ Key sales2Key = datastore.newKeyFactory().setKind(kind).newKey("sales2");
+ Key sales3Key = datastore.newKeyFactory().setKind(kind).newKey("sales3");
+
+ // Save all the sales.
+ datastore.put(
+ Entity.newBuilder(sales1Key).set("amount", 89).build(),
+ Entity.newBuilder(sales2Key).set("amount", 95).build(),
+ Entity.newBuilder(sales3Key).set("amount", 55).build());
+ }
+
+ // Accessing aggregation result by the provided custom alias.
+ private static void usageWithCustomAlias() {
+ EntityQuery selectAllSales = Query.newEntityQueryBuilder().setKind(kind).build();
+ // Creating an aggregation query to get the avg of all sales.
+ AggregationQuery avgOfSalesQuery =
+ Query.newAggregationQueryBuilder()
+ .over(selectAllSales)
+ // passing 'avg_sales_amount' as alias in the aggregation query.
+ .addAggregation(avg("amount").as("avg_sales_amount"))
+ .build();
+ // Executing aggregation query.
+ AggregationResult aggregationResult =
+ Iterables.getOnlyElement(datastore.runAggregation(avgOfSalesQuery));
+
+ System.out.printf(
+ "Average sales is %.8f", aggregationResult.getDouble("avg_sales_amount")); // 79.66666667
+ }
+
+ public static void invoke() {
+ setUpSales();
+ usageWithCustomAlias();
+ }
+}
+// [END datastore_avg_aggregation_query_on_kind]
diff --git a/samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationWithLimit.java b/samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationWithLimit.java
new file mode 100644
index 000000000..a96ab9317
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationWithLimit.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2023 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.datastore.aggregation;
+
+// [START datastore_avg_aggregation_query_with_limit]
+
+import static com.google.cloud.datastore.aggregation.Aggregation.avg;
+
+import com.google.cloud.datastore.AggregationQuery;
+import com.google.cloud.datastore.AggregationResult;
+import com.google.cloud.datastore.Datastore;
+import com.google.cloud.datastore.DatastoreOptions;
+import com.google.cloud.datastore.Entity;
+import com.google.cloud.datastore.EntityQuery;
+import com.google.cloud.datastore.Key;
+import com.google.cloud.datastore.Query;
+import com.google.common.collect.Iterables;
+
+public class AvgAggregationWithLimit {
+
+ public static void invoke() {
+ // Instantiates a client.
+ Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
+
+ // The kind for the new entity.
+ String kind = "Sales";
+
+ Key sales1Key = datastore.newKeyFactory().setKind(kind).newKey("sales1");
+ Key sales2Key = datastore.newKeyFactory().setKind(kind).newKey("sales2");
+ Key sales3Key = datastore.newKeyFactory().setKind(kind).newKey("sales3");
+
+ // Save all the tasks.
+ datastore.put(
+ Entity.newBuilder(sales1Key).set("amount", 89).build(),
+ Entity.newBuilder(sales2Key).set("amount", 95).build(),
+ Entity.newBuilder(sales3Key).set("amount", 55).build());
+
+ EntityQuery selectAllSales = Query.newEntityQueryBuilder().setKind(kind).setLimit(2).build();
+ // Creating an aggregation query to get the avg of all sales amount.
+ AggregationQuery avgOfSalesQuery =
+ Query.newAggregationQueryBuilder()
+ .over(selectAllSales)
+ .addAggregation(avg("amount").as("at_least"))
+ .build();
+ // Executing aggregation query.
+ AggregationResult limitQueryResult =
+ Iterables.getOnlyElement(datastore.runAggregation(avgOfSalesQuery));
+
+ System.out.printf("Average with limit 2 is %d.", limitQueryResult.getLong("at_least")); // 72
+ }
+}
+// [END datastore_avg_aggregation_query_with_limit]
diff --git a/samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationWithOrderBy.java b/samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationWithOrderBy.java
new file mode 100644
index 000000000..68875b23f
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationWithOrderBy.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2023 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.datastore.aggregation;
+
+// [START datastore_avg_aggregation_query_with_order_by]
+
+import static com.google.cloud.datastore.StructuredQuery.OrderBy.asc;
+import static com.google.cloud.datastore.aggregation.Aggregation.avg;
+
+import com.google.cloud.datastore.AggregationQuery;
+import com.google.cloud.datastore.AggregationResult;
+import com.google.cloud.datastore.Datastore;
+import com.google.cloud.datastore.DatastoreOptions;
+import com.google.cloud.datastore.Entity;
+import com.google.cloud.datastore.EntityQuery;
+import com.google.cloud.datastore.Key;
+import com.google.cloud.datastore.Query;
+import com.google.common.collect.Iterables;
+
+public class AvgAggregationWithOrderBy {
+
+ public static void invoke() {
+ // Instantiates a client.
+ Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
+
+ // The kind for the new entity.
+ String kind = "Sales";
+
+ Key sales1Key = datastore.newKeyFactory().setKind(kind).newKey("sales1");
+ Key sales2Key = datastore.newKeyFactory().setKind(kind).newKey("sales2");
+ Key sales3Key = datastore.newKeyFactory().setKind(kind).newKey("sales3");
+
+ // Save all the sales.
+ datastore.put(
+ Entity.newBuilder(sales1Key).set("amount", 89).set("customerId", 1).build(),
+ // customerId not specified (orphan record).
+ Entity.newBuilder(sales2Key).set("amount", 95).build(),
+ Entity.newBuilder(sales3Key).set("amount", 55).set("customerId", 2).build());
+
+ // OrderBy acts as an existence filter.
+ EntityQuery selectAllSales =
+ Query.newEntityQueryBuilder().setKind(kind).addOrderBy(asc("customerId")).build();
+
+ // Creating an aggregation query to avg of all sales.
+ AggregationQuery avgOfSalesQuery =
+ Query.newAggregationQueryBuilder()
+ .over(selectAllSales)
+ .addAggregation(avg("amount").as("avg_of_sales"))
+ .build();
+ // Executing aggregation query.
+ AggregationResult limitQueryResult =
+ Iterables.getOnlyElement(datastore.runAggregation(avgOfSalesQuery));
+
+ System.out.printf(
+ "Total avg of %d with valid customerId field",
+ limitQueryResult.getLong("avg_of_sales")); // 72
+ }
+}
+// [END datastore_avg_aggregation_query_with_order_by]
diff --git a/samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationWithPropertyFilter.java b/samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationWithPropertyFilter.java
new file mode 100644
index 000000000..4fe2dd532
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationWithPropertyFilter.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2023 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.datastore.aggregation;
+
+// [START datastore_avg_aggregation_query_with_filters]
+
+import static com.google.cloud.datastore.aggregation.Aggregation.avg;
+
+import com.google.cloud.datastore.AggregationQuery;
+import com.google.cloud.datastore.AggregationResult;
+import com.google.cloud.datastore.Datastore;
+import com.google.cloud.datastore.DatastoreOptions;
+import com.google.cloud.datastore.Entity;
+import com.google.cloud.datastore.EntityQuery;
+import com.google.cloud.datastore.Key;
+import com.google.cloud.datastore.Query;
+import com.google.cloud.datastore.StructuredQuery.PropertyFilter;
+import com.google.common.collect.Iterables;
+
+public class AvgAggregationWithPropertyFilter {
+
+ public static void invoke() {
+ // Instantiates a client.
+ Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
+
+ // The kind for the new entity.
+ String kind = "Sales";
+
+ Key sales1Key = datastore.newKeyFactory().setKind(kind).newKey("sales1");
+ Key sales2Key = datastore.newKeyFactory().setKind(kind).newKey("sales2");
+ Key sales3Key = datastore.newKeyFactory().setKind(kind).newKey("sales3");
+
+ // Save all the tasks.
+ datastore.put(
+ Entity.newBuilder(sales1Key).set("amount", 89).set("customerId", 1).build(),
+ Entity.newBuilder(sales2Key).set("amount", 95).set("customerId", 1).build(),
+ Entity.newBuilder(sales3Key).set("amount", 55).set("customerId", 2).build());
+
+ EntityQuery customer1Sales =
+ Query.newEntityQueryBuilder()
+ .setKind(kind)
+ .setFilter(PropertyFilter.eq("customerId", 1))
+ .build();
+
+ // Creating an aggregation query to get the avg of all sales for customerId 1.
+ AggregationQuery customer1SalesAvg =
+ Query.newAggregationQueryBuilder()
+ .over(customer1Sales)
+ .addAggregation(avg("amount").as("total_sales"))
+ .build();
+
+ // Executing aggregation query.
+ AggregationResult customer1SalesAvgQueryResult =
+ Iterables.getOnlyElement(datastore.runAggregation(customer1SalesAvg));
+
+ System.out.printf(
+ "Customer 1 sales avg is %d", customer1SalesAvgQueryResult.getLong("total_sales")); // 92
+ }
+}
+// [END datastore_avg_aggregation_query_with_filters]
diff --git a/samples/snippets/src/main/java/com/example/datastore/aggregation/MultipleAggregationsInGqlQuery.java b/samples/snippets/src/main/java/com/example/datastore/aggregation/MultipleAggregationsInGqlQuery.java
new file mode 100644
index 000000000..40d152598
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/datastore/aggregation/MultipleAggregationsInGqlQuery.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2023 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.datastore.aggregation;
+
+// [START datastore_multiple_aggregation_in_structured_query]
+
+import com.google.cloud.datastore.AggregationQuery;
+import com.google.cloud.datastore.AggregationResult;
+import com.google.cloud.datastore.Datastore;
+import com.google.cloud.datastore.DatastoreOptions;
+import com.google.cloud.datastore.Entity;
+import com.google.cloud.datastore.GqlQuery;
+import com.google.cloud.datastore.Key;
+import com.google.cloud.datastore.Query;
+import com.google.common.collect.Iterables;
+
+public class MultipleAggregationsInGqlQuery {
+
+ public static void invoke() {
+ // Instantiates a client.
+ Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
+
+ // The kind for the new entity.
+ String kind = "Sales";
+
+ Key sales1Key = datastore.newKeyFactory().setKind(kind).newKey("sales1");
+ Key sales2Key = datastore.newKeyFactory().setKind(kind).newKey("sales2");
+ Key sales3Key = datastore.newKeyFactory().setKind(kind).newKey("sales3");
+
+ // Save all the sales.
+ datastore.put(
+ Entity.newBuilder(sales1Key).set("amount", 89).set("customerId", 1).build(),
+ Entity.newBuilder(sales2Key).set("amount", 95).set("customerId", 1).build(),
+ Entity.newBuilder(sales3Key).set("amount", 55).set("customerId", 2).build());
+
+ GqlQuery> baseQuery =
+ Query.newGqlQueryBuilder(
+ "AGGREGATE COUNT(*) AS total_count,"
+ + " SUM(amount) AS sales_sum,"
+ + " AVG(amount) AS sales_avg "
+ + "OVER (SELECT * FROM Sales)")
+ .build();
+
+ // Creating an aggregation query with COUNT, SUM and AVG aggregations.
+ AggregationQuery aggregationQuery = Query.newAggregationQueryBuilder().over(baseQuery).build();
+
+ // Executing aggregation query.
+ AggregationResult aggregationResult =
+ Iterables.getOnlyElement(datastore.runAggregation(aggregationQuery));
+
+ System.out.printf("Total sales count: %d", aggregationResult.getLong("total_count")); // 3
+ System.out.printf("Sum of sales: %d", aggregationResult.getLong("sales_sum")); // 239
+ System.out.printf(
+ "Avg of sales: %.8f", aggregationResult.getDouble("sales_avg")); // 79.66666667
+ }
+}
+// [END datastore_multiple_aggregation_in_structured_query]
diff --git a/samples/snippets/src/main/java/com/example/datastore/aggregation/MultipleAggregationsInStructuredQuery.java b/samples/snippets/src/main/java/com/example/datastore/aggregation/MultipleAggregationsInStructuredQuery.java
new file mode 100644
index 000000000..1fc0e9eda
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/datastore/aggregation/MultipleAggregationsInStructuredQuery.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2023 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.datastore.aggregation;
+
+// [START datastore_multiple_aggregation_in_structured_query]
+
+import static com.google.cloud.datastore.aggregation.Aggregation.avg;
+import static com.google.cloud.datastore.aggregation.Aggregation.count;
+import static com.google.cloud.datastore.aggregation.Aggregation.sum;
+
+import com.google.cloud.datastore.AggregationQuery;
+import com.google.cloud.datastore.AggregationResult;
+import com.google.cloud.datastore.Datastore;
+import com.google.cloud.datastore.DatastoreOptions;
+import com.google.cloud.datastore.Entity;
+import com.google.cloud.datastore.EntityQuery;
+import com.google.cloud.datastore.Key;
+import com.google.cloud.datastore.Query;
+import com.google.common.collect.Iterables;
+
+public class MultipleAggregationsInStructuredQuery {
+
+ public static void invoke() {
+ // Instantiates a client.
+ Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
+
+ // The kind for the new entity.
+ String kind = "Sales";
+
+ Key sales1Key = datastore.newKeyFactory().setKind(kind).newKey("sales1");
+ Key sales2Key = datastore.newKeyFactory().setKind(kind).newKey("sales2");
+ Key sales3Key = datastore.newKeyFactory().setKind(kind).newKey("sales3");
+
+ // Save all the sales.
+ datastore.put(
+ Entity.newBuilder(sales1Key).set("amount", 89).set("customerId", 1).build(),
+ Entity.newBuilder(sales2Key).set("amount", 95).set("customerId", 1).build(),
+ Entity.newBuilder(sales3Key).set("amount", 55).set("customerId", 2).build());
+
+ EntityQuery baseQuery = Query.newEntityQueryBuilder().setKind(kind).build();
+
+ // Creating an aggregation query with COUNT, SUM and AVG aggregations.
+ AggregationQuery aggregationQuery =
+ Query.newAggregationQueryBuilder()
+ .over(baseQuery)
+ .addAggregation(count().as("total_count"))
+ .addAggregation(sum("amount").as("sales_sum"))
+ .addAggregation(avg("amount").as("sales_avg"))
+ .build();
+
+ // Executing aggregation query.
+ AggregationResult aggregationResult =
+ Iterables.getOnlyElement(datastore.runAggregation(aggregationQuery));
+
+ System.out.printf("Total sales count: %d", aggregationResult.getLong("total_count")); // 3
+ System.out.printf("Sum of sales: %d", aggregationResult.getLong("sales_sum")); // 239
+ System.out.printf(
+ "Avg of sales: %.8f", aggregationResult.getDouble("sales_avg")); // 79.66666667
+ }
+}
+// [END datastore_multiple_aggregation_in_structured_query]
diff --git a/samples/snippets/src/main/java/com/example/datastore/aggregation/SumAggregationOnKind.java b/samples/snippets/src/main/java/com/example/datastore/aggregation/SumAggregationOnKind.java
new file mode 100644
index 000000000..48ca2974d
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/datastore/aggregation/SumAggregationOnKind.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2023 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.datastore.aggregation;
+
+// [START datastore_sum_aggregation_query_on_kind]
+
+import static com.google.cloud.datastore.aggregation.Aggregation.sum;
+
+import com.google.cloud.datastore.AggregationQuery;
+import com.google.cloud.datastore.AggregationResult;
+import com.google.cloud.datastore.Datastore;
+import com.google.cloud.datastore.DatastoreOptions;
+import com.google.cloud.datastore.Entity;
+import com.google.cloud.datastore.EntityQuery;
+import com.google.cloud.datastore.Key;
+import com.google.cloud.datastore.Query;
+import com.google.common.collect.Iterables;
+
+public class SumAggregationOnKind {
+
+ // Instantiates a client.
+ private static final Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
+
+ // The kind for the new entity.
+ private static final String kind = "Sales";
+
+ // Setting up Sales in database
+ private static void setUpSales() {
+ Key sales1Key = datastore.newKeyFactory().setKind(kind).newKey("sales1");
+ Key sales2Key = datastore.newKeyFactory().setKind(kind).newKey("sales2");
+ Key sales3Key = datastore.newKeyFactory().setKind(kind).newKey("sales3");
+
+ // Save all the sales.
+ datastore.put(
+ Entity.newBuilder(sales1Key).set("amount", 89).build(),
+ Entity.newBuilder(sales2Key).set("amount", 95).build(),
+ Entity.newBuilder(sales3Key).set("amount", 55).build());
+ }
+
+ // Accessing aggregation result by the provided custom alias.
+ private static void usageWithCustomAlias() {
+ EntityQuery selectAllSales = Query.newEntityQueryBuilder().setKind(kind).build();
+ // Creating an aggregation query to get the sum of all sales.
+ AggregationQuery sumOfSalesQuery =
+ Query.newAggregationQueryBuilder()
+ .over(selectAllSales)
+ // passing 'total_sales_amount' as alias in the aggregation query.
+ .addAggregation(sum("amount").as("total_sales_amount"))
+ .build();
+ // Executing aggregation query.
+ AggregationResult aggregationResult =
+ Iterables.getOnlyElement(datastore.runAggregation(sumOfSalesQuery));
+
+ System.out.printf("Total sales is %d", aggregationResult.getLong("total_sales_amount")); // 239
+ }
+
+ public static void invoke() {
+ setUpSales();
+ usageWithCustomAlias();
+ }
+}
+// [END datastore_sum_aggregation_query_on_kind]
diff --git a/samples/snippets/src/main/java/com/example/datastore/aggregation/SumAggregationWithLimit.java b/samples/snippets/src/main/java/com/example/datastore/aggregation/SumAggregationWithLimit.java
new file mode 100644
index 000000000..6b94ade92
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/datastore/aggregation/SumAggregationWithLimit.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2023 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.datastore.aggregation;
+
+// [START datastore_sum_aggregation_query_with_limit]
+
+import static com.google.cloud.datastore.aggregation.Aggregation.sum;
+
+import com.google.cloud.datastore.AggregationQuery;
+import com.google.cloud.datastore.AggregationResult;
+import com.google.cloud.datastore.Datastore;
+import com.google.cloud.datastore.DatastoreOptions;
+import com.google.cloud.datastore.Entity;
+import com.google.cloud.datastore.EntityQuery;
+import com.google.cloud.datastore.Key;
+import com.google.cloud.datastore.Query;
+import com.google.common.collect.Iterables;
+
+public class SumAggregationWithLimit {
+
+ public static void invoke() {
+ // Instantiates a client.
+ Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
+
+ // The kind for the new entity.
+ String kind = "Sales";
+
+ Key sales1Key = datastore.newKeyFactory().setKind(kind).newKey("sales1");
+ Key sales2Key = datastore.newKeyFactory().setKind(kind).newKey("sales2");
+ Key sales3Key = datastore.newKeyFactory().setKind(kind).newKey("sales3");
+
+ // Save all the tasks.
+ datastore.put(
+ Entity.newBuilder(sales1Key).set("amount", 89).build(),
+ Entity.newBuilder(sales2Key).set("amount", 95).build(),
+ Entity.newBuilder(sales3Key).set("amount", 55).build());
+
+ EntityQuery selectAllSales = Query.newEntityQueryBuilder().setKind(kind).setLimit(2).build();
+ // Creating an aggregation query to get the sum of all sales amount.
+ AggregationQuery sumOfSalesQuery =
+ Query.newAggregationQueryBuilder()
+ .over(selectAllSales)
+ .addAggregation(sum("amount").as("at_least"))
+ .build();
+ // Executing aggregation query.
+ AggregationResult limitQueryResult =
+ Iterables.getOnlyElement(datastore.runAggregation(sumOfSalesQuery));
+
+ System.out.printf(
+ "We have a minimum sales sum of %d.", limitQueryResult.getLong("at_least")); // 144
+ }
+}
+// [END datastore_sum_aggregation_query_with_limit]
diff --git a/samples/snippets/src/main/java/com/example/datastore/aggregation/SumAggregationWithOrderBy.java b/samples/snippets/src/main/java/com/example/datastore/aggregation/SumAggregationWithOrderBy.java
new file mode 100644
index 000000000..151bdd1c7
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/datastore/aggregation/SumAggregationWithOrderBy.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2023 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.datastore.aggregation;
+
+// [START datastore_sum_aggregation_query_with_order_by]
+
+import static com.google.cloud.datastore.StructuredQuery.OrderBy.asc;
+import static com.google.cloud.datastore.aggregation.Aggregation.sum;
+
+import com.google.cloud.datastore.AggregationQuery;
+import com.google.cloud.datastore.AggregationResult;
+import com.google.cloud.datastore.Datastore;
+import com.google.cloud.datastore.DatastoreOptions;
+import com.google.cloud.datastore.Entity;
+import com.google.cloud.datastore.EntityQuery;
+import com.google.cloud.datastore.Key;
+import com.google.cloud.datastore.Query;
+import com.google.common.collect.Iterables;
+
+public class SumAggregationWithOrderBy {
+
+ public static void invoke() {
+ // Instantiates a client.
+ Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
+
+ // The kind for the new entity.
+ String kind = "Sales";
+
+ Key sales1Key = datastore.newKeyFactory().setKind(kind).newKey("sales1");
+ Key sales2Key = datastore.newKeyFactory().setKind(kind).newKey("sales2");
+ Key sales3Key = datastore.newKeyFactory().setKind(kind).newKey("sales3");
+
+ // Save all the sales.
+ datastore.put(
+ Entity.newBuilder(sales1Key).set("amount", 89).set("customerId", 1).build(),
+ // customerId not specified (orphan record).
+ Entity.newBuilder(sales2Key).set("amount", 95).build(),
+ Entity.newBuilder(sales3Key).set("amount", 55).set("customerId", 2).build());
+
+ // OrderBy acts as an existence filter.
+ EntityQuery selectAllSales =
+ Query.newEntityQueryBuilder().setKind(kind).addOrderBy(asc("customerId")).build();
+
+ // Creating an aggregation query to sum of all sales.
+ AggregationQuery sumOfSalesQuery =
+ Query.newAggregationQueryBuilder()
+ .over(selectAllSales)
+ .addAggregation(sum("amount").as("total_sales"))
+ .build();
+ // Executing aggregation query.
+ AggregationResult limitQueryResult =
+ Iterables.getOnlyElement(datastore.runAggregation(sumOfSalesQuery));
+
+ System.out.printf(
+ "Total sum of %d with valid customerId field",
+ limitQueryResult.getLong("total_sales")); // 144
+ }
+}
+// [END datastore_sum_aggregation_query_with_order_by]
diff --git a/samples/snippets/src/main/java/com/example/datastore/aggregation/SumAggregationWithPropertyFilter.java b/samples/snippets/src/main/java/com/example/datastore/aggregation/SumAggregationWithPropertyFilter.java
new file mode 100644
index 000000000..39ba310ea
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/datastore/aggregation/SumAggregationWithPropertyFilter.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2023 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.datastore.aggregation;
+
+// [START datastore_sum_aggregation_query_with_filters]
+
+import static com.google.cloud.datastore.aggregation.Aggregation.sum;
+
+import com.google.cloud.datastore.AggregationQuery;
+import com.google.cloud.datastore.AggregationResult;
+import com.google.cloud.datastore.Datastore;
+import com.google.cloud.datastore.DatastoreOptions;
+import com.google.cloud.datastore.Entity;
+import com.google.cloud.datastore.EntityQuery;
+import com.google.cloud.datastore.Key;
+import com.google.cloud.datastore.Query;
+import com.google.cloud.datastore.StructuredQuery.PropertyFilter;
+import com.google.common.collect.Iterables;
+
+public class SumAggregationWithPropertyFilter {
+
+ public static void invoke() {
+ // Instantiates a client.
+ Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
+
+ // The kind for the new entity.
+ String kind = "Sales";
+
+ Key sales1Key = datastore.newKeyFactory().setKind(kind).newKey("sales1");
+ Key sales2Key = datastore.newKeyFactory().setKind(kind).newKey("sales2");
+ Key sales3Key = datastore.newKeyFactory().setKind(kind).newKey("sales3");
+
+ // Save all the tasks.
+ datastore.put(
+ Entity.newBuilder(sales1Key).set("amount", 89).set("customerId", 1).build(),
+ Entity.newBuilder(sales2Key).set("amount", 95).set("customerId", 1).build(),
+ Entity.newBuilder(sales3Key).set("amount", 55).set("customerId", 2).build());
+
+ EntityQuery customer1Sales =
+ Query.newEntityQueryBuilder()
+ .setKind(kind)
+ .setFilter(PropertyFilter.eq("customerId", 1))
+ .build();
+
+ // Creating an aggregation query to get the sum of all sales for customerId 1.
+ AggregationQuery customer1SalesSum =
+ Query.newAggregationQueryBuilder()
+ .over(customer1Sales)
+ .addAggregation(sum("amount").as("total_sales"))
+ .build();
+
+ // Executing aggregation query.
+ AggregationResult customer1SalesSumQueryResult =
+ Iterables.getOnlyElement(datastore.runAggregation(customer1SalesSum));
+
+ System.out.printf(
+ "Customer 1 sales sum is %d", customer1SalesSumQueryResult.getLong("total_sales")); // 184
+ }
+}
+// [END datastore_sum_aggregation_query_with_filters]
diff --git a/samples/snippets/src/test/java/com/example/datastore/AggregationQuerySampleTestIT.java b/samples/snippets/src/test/java/com/example/datastore/AggregationQuerySampleTestIT.java
index 662802e51..ab709f575 100644
--- a/samples/snippets/src/test/java/com/example/datastore/AggregationQuerySampleTestIT.java
+++ b/samples/snippets/src/test/java/com/example/datastore/AggregationQuerySampleTestIT.java
@@ -18,6 +18,10 @@
import static org.junit.Assert.assertThrows;
+import com.example.datastore.aggregation.AvgAggregationOnKind;
+import com.example.datastore.aggregation.AvgAggregationWithLimit;
+import com.example.datastore.aggregation.AvgAggregationWithOrderBy;
+import com.example.datastore.aggregation.AvgAggregationWithPropertyFilter;
import com.example.datastore.aggregation.CountAggregationInTransaction;
import com.example.datastore.aggregation.CountAggregationOnKind;
import com.example.datastore.aggregation.CountAggregationWithGqlQuery;
@@ -25,6 +29,12 @@
import com.example.datastore.aggregation.CountAggregationWithOrderBy;
import com.example.datastore.aggregation.CountAggregationWithPropertyFilter;
import com.example.datastore.aggregation.CountAggregationWithStaleRead;
+import com.example.datastore.aggregation.MultipleAggregationsInGqlQuery;
+import com.example.datastore.aggregation.MultipleAggregationsInStructuredQuery;
+import com.example.datastore.aggregation.SumAggregationOnKind;
+import com.example.datastore.aggregation.SumAggregationWithLimit;
+import com.example.datastore.aggregation.SumAggregationWithOrderBy;
+import com.example.datastore.aggregation.SumAggregationWithPropertyFilter;
import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.DatastoreOptions;
import com.google.cloud.datastore.Key;
@@ -98,4 +108,68 @@ public void testAggregationQueryAndCountWithTransaction() throws InterruptedExce
assertThrows(Exception.class, CountAggregationInTransaction::invoke);
systemsOutRule.assertContains("Found existing 2 tasks, rolling back");
}
+
+ @Test
+ public void testSumAggregationWithKind() {
+ SumAggregationOnKind.invoke();
+ systemsOutRule.assertContains("Total sales is 239");
+ }
+
+ @Test
+ public void testAvgAggregationWithKind() {
+ AvgAggregationOnKind.invoke();
+ systemsOutRule.assertContains("Average sales is 79.66666667");
+ }
+
+ @Test
+ public void testSumAggregationWithPropertyFilter() {
+ SumAggregationWithPropertyFilter.invoke();
+ systemsOutRule.assertContains("Customer 1 sales sum is 184");
+ }
+
+ @Test
+ public void testAvgAggregationWithPropertyFilter() {
+ AvgAggregationWithPropertyFilter.invoke();
+ systemsOutRule.assertContains("Customer 1 sales avg is 92");
+ }
+
+ @Test
+ public void testSumAggregationWithLimit() {
+ SumAggregationWithLimit.invoke();
+ systemsOutRule.assertContains("We have a minimum sales sum of 144.");
+ }
+
+ @Test
+ public void testAvgAggregationWithLimit() {
+ AvgAggregationWithLimit.invoke();
+ systemsOutRule.assertContains("Average with limit 2 is 72.");
+ }
+
+ @Test
+ public void testSumAggregationWithOrderBy() {
+ SumAggregationWithOrderBy.invoke();
+ systemsOutRule.assertContains("Total sum of 144 with valid customerId field");
+ }
+
+ @Test
+ public void testAvgAggregationWithOrderBy() {
+ AvgAggregationWithOrderBy.invoke();
+ systemsOutRule.assertContains("Total avg of 72 with valid customerId field");
+ }
+
+ @Test
+ public void testMultipleAggregationsInStructuredQuery() {
+ MultipleAggregationsInStructuredQuery.invoke();
+ systemsOutRule.assertContains("Total sales count: 3");
+ systemsOutRule.assertContains("Sum of sales: 239");
+ systemsOutRule.assertContains("Avg of sales: 79.66666667");
+ }
+
+ @Test
+ public void testMultipleAggregationsInGQLQuery() {
+ MultipleAggregationsInGqlQuery.invoke();
+ systemsOutRule.assertContains("Total sales count: 3");
+ systemsOutRule.assertContains("Sum of sales: 239");
+ systemsOutRule.assertContains("Avg of sales: 79.66666667");
+ }
}
diff --git a/samples/snippets/src/test/resources/index.yaml b/samples/snippets/src/test/resources/index.yaml
new file mode 100644
index 000000000..50ab1ca0f
--- /dev/null
+++ b/samples/snippets/src/test/resources/index.yaml
@@ -0,0 +1,21 @@
+# Copyright 2023 Google LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# one time index creation is required to run AggregationQuerySampleTestIT
+# see https://cloud.google.com/sdk/gcloud/reference/datastore/indexes/create for more details
+indexes:
+- kind: Sales
+ properties:
+ - name: customerId
+ - name: amount
diff --git a/versions.txt b/versions.txt
index eea775b52..fbfda5a27 100644
--- a/versions.txt
+++ b/versions.txt
@@ -1,9 +1,9 @@
# Format:
# module:released-version:current-version
-google-cloud-datastore:2.17.0:2.17.0
-google-cloud-datastore-bom:2.17.0:2.17.0
-proto-google-cloud-datastore-v1:0.108.0:0.108.0
-datastore-v1-proto-client:2.17.0:2.17.0
-proto-google-cloud-datastore-admin-v1:2.17.0:2.17.0
-grpc-google-cloud-datastore-admin-v1:2.17.0:2.17.0
+google-cloud-datastore:2.17.1:2.17.1
+google-cloud-datastore-bom:2.17.1:2.17.1
+proto-google-cloud-datastore-v1:0.108.1:0.108.1
+datastore-v1-proto-client:2.17.1:2.17.1
+proto-google-cloud-datastore-admin-v1:2.17.1:2.17.1
+grpc-google-cloud-datastore-admin-v1:2.17.1:2.17.1