diff --git a/README.md b/README.md index 56cc0d604..fd1b011ed 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/samples/snapshot/pom.xml b/samples/snapshot/pom.xml index 47fb6b3d5..689b85ccd 100644 --- a/samples/snapshot/pom.xml +++ b/samples/snapshot/pom.xml @@ -23,13 +23,14 @@ UTF-8 - + com.google.cloud google-cloud-datastore - 2.17.0 + 2.17.1-SNAPSHOT + 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..f41b193bc --- /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")); // 184 + } +} +// [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