Skip to content

Commit 22456ed

Browse files
authored
samples: query profile (#1385)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/java-datastore/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) Fixes #<issue_number_goes_here> ☕️ If you write sample code, please follow the [samples format]( https://github.com/GoogleCloudPlatform/java-docs-samples/blob/main/SAMPLE_FORMAT.md).
1 parent 520ebd4 commit 22456ed

File tree

8 files changed

+400
-20
lines changed

8 files changed

+400
-20
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-datastore/tre
280280
| 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) |
281281
| 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) |
282282
| 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) |
283+
| Query Profile Explain | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/queryprofile/QueryProfileExplain.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/queryprofile/QueryProfileExplain.java) |
284+
| Query Profile Explain Aggregation | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/queryprofile/QueryProfileExplainAggregation.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/queryprofile/QueryProfileExplainAggregation.java) |
285+
| Query Profile Explain Analyze | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/queryprofile/QueryProfileExplainAnalyze.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/queryprofile/QueryProfileExplainAnalyze.java) |
286+
| Query Profile Explain Analyze Aggregation | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/queryprofile/QueryProfileExplainAnalyzeAggregation.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/queryprofile/QueryProfileExplainAnalyzeAggregation.java) |
283287
| 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) |
284288

285289

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.datastore.queryprofile;
18+
19+
// [START datastore_query_explain_entity]
20+
21+
import com.google.cloud.datastore.Datastore;
22+
import com.google.cloud.datastore.DatastoreOptions;
23+
import com.google.cloud.datastore.Entity;
24+
import com.google.cloud.datastore.Query;
25+
import com.google.cloud.datastore.QueryResults;
26+
import com.google.cloud.datastore.models.ExplainMetrics;
27+
import com.google.cloud.datastore.models.ExplainOptions;
28+
import com.google.cloud.datastore.models.PlanSummary;
29+
import java.util.List;
30+
import java.util.Map;
31+
import java.util.Optional;
32+
33+
public class QueryProfileExplain {
34+
public static void invoke() throws Exception {
35+
// Instantiates a client
36+
Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
37+
38+
// Build the query
39+
Query<Entity> query = Query.newEntityQueryBuilder().setKind("Task").build();
40+
41+
// Set the explain options to get back *only* the plan summary
42+
QueryResults<Entity> results = datastore.run(query, ExplainOptions.newBuilder().build());
43+
44+
// Get the explain metrics
45+
Optional<ExplainMetrics> explainMetrics = results.getExplainMetrics();
46+
if (!explainMetrics.isPresent()) {
47+
throw new Exception("No explain metrics returned");
48+
}
49+
PlanSummary planSummary = explainMetrics.get().getPlanSummary();
50+
List<Map<String, Object>> indexesUsed = planSummary.getIndexesUsed();
51+
System.out.println("----- Indexes Used -----");
52+
indexesUsed.forEach(map -> map.forEach((key, val) -> System.out.println(key + ": " + val)));
53+
}
54+
}
55+
// [END datastore_query_explain_entity]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.datastore.queryprofile;
18+
19+
// [START datastore_query_explain_aggregation]
20+
21+
import static com.google.cloud.datastore.aggregation.Aggregation.count;
22+
23+
import com.google.cloud.datastore.AggregationQuery;
24+
import com.google.cloud.datastore.AggregationResults;
25+
import com.google.cloud.datastore.Datastore;
26+
import com.google.cloud.datastore.DatastoreOptions;
27+
import com.google.cloud.datastore.Entity;
28+
import com.google.cloud.datastore.Query;
29+
import com.google.cloud.datastore.StructuredQuery;
30+
import com.google.cloud.datastore.models.ExplainMetrics;
31+
import com.google.cloud.datastore.models.ExplainOptions;
32+
import com.google.cloud.datastore.models.PlanSummary;
33+
import java.util.List;
34+
import java.util.Map;
35+
import java.util.Optional;
36+
37+
public class QueryProfileExplainAggregation {
38+
public static void invoke() throws Exception {
39+
// Instantiates a client
40+
Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
41+
42+
// Build the query
43+
StructuredQuery<Entity> query = Query.newEntityQueryBuilder().setKind("Task").build();
44+
45+
// Build the aggregation query
46+
AggregationQuery aggregationQuery =
47+
Query.newAggregationQueryBuilder().over(query).addAggregation(count()).build();
48+
49+
// Set the explain options to get back *only* the plan summary
50+
AggregationResults results =
51+
datastore.runAggregation(aggregationQuery, ExplainOptions.newBuilder().build());
52+
53+
// Get the explain metrics
54+
Optional<ExplainMetrics> explainMetrics = results.getExplainMetrics();
55+
if (!explainMetrics.isPresent()) {
56+
throw new Exception("No explain metrics returned");
57+
}
58+
PlanSummary planSummary = explainMetrics.get().getPlanSummary();
59+
List<Map<String, Object>> indexesUsed = planSummary.getIndexesUsed();
60+
System.out.println("----- Indexes Used -----");
61+
indexesUsed.forEach(map -> map.forEach((key, val) -> System.out.println(key + ": " + val)));
62+
}
63+
}
64+
// [END datastore_query_explain_aggregation]
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.datastore.queryprofile;
18+
19+
// [START datastore_query_explain_analyze_entity]
20+
import com.google.cloud.datastore.Datastore;
21+
import com.google.cloud.datastore.DatastoreOptions;
22+
import com.google.cloud.datastore.Entity;
23+
import com.google.cloud.datastore.Query;
24+
import com.google.cloud.datastore.QueryResults;
25+
import com.google.cloud.datastore.models.ExecutionStats;
26+
import com.google.cloud.datastore.models.ExplainMetrics;
27+
import com.google.cloud.datastore.models.ExplainOptions;
28+
import com.google.cloud.datastore.models.PlanSummary;
29+
import java.util.List;
30+
import java.util.Map;
31+
32+
public class QueryProfileExplainAnalyze {
33+
public static void invoke() throws Exception {
34+
// Instantiates a client
35+
Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
36+
37+
// Build the query
38+
Query<Entity> query = Query.newEntityQueryBuilder().setKind("Task").build();
39+
40+
// Set explain options with analzye = true to get back the query stats, plan info, and query
41+
// results
42+
QueryResults<Entity> results =
43+
datastore.run(query, ExplainOptions.newBuilder().setAnalyze(true).build());
44+
45+
// Get the result set stats
46+
if (!results.getExplainMetrics().isPresent()) {
47+
throw new Exception("No explain metrics returned");
48+
}
49+
ExplainMetrics explainMetrics = results.getExplainMetrics().get();
50+
51+
// Get the execution stats
52+
if (!explainMetrics.getExecutionStats().isPresent()) {
53+
throw new Exception("No execution stats returned");
54+
}
55+
56+
ExecutionStats executionStats = explainMetrics.getExecutionStats().get();
57+
Map<String, Object> debugStats = executionStats.getDebugStats();
58+
System.out.println("----- Debug Stats -----");
59+
debugStats.forEach((key, val) -> System.out.println(key + ": " + val));
60+
System.out.println("----------");
61+
62+
long resultsReturned = executionStats.getResultsReturned();
63+
System.out.println("Results returned: " + resultsReturned);
64+
65+
// Get the plan summary
66+
PlanSummary planSummary = explainMetrics.getPlanSummary();
67+
List<Map<String, Object>> indexesUsed = planSummary.getIndexesUsed();
68+
System.out.println("----- Indexes Used -----");
69+
indexesUsed.forEach(map -> map.forEach((key, val) -> System.out.println(key + ": " + val)));
70+
71+
if (!results.hasNext()) {
72+
throw new Exception("query yielded no results");
73+
}
74+
75+
// Get the query results
76+
System.out.println("----- Query Results -----");
77+
while (results.hasNext()) {
78+
Entity entity = results.next();
79+
System.out.printf("Entity: %s%n", entity);
80+
}
81+
}
82+
}
83+
// [END datastore_query_explain_analyze_entity]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.datastore.queryprofile;
18+
19+
// [START datastore_query_explain_analyze_aggregation]
20+
21+
import static com.google.cloud.datastore.aggregation.Aggregation.count;
22+
23+
import com.google.cloud.datastore.AggregationQuery;
24+
import com.google.cloud.datastore.AggregationResult;
25+
import com.google.cloud.datastore.AggregationResults;
26+
import com.google.cloud.datastore.Datastore;
27+
import com.google.cloud.datastore.DatastoreOptions;
28+
import com.google.cloud.datastore.Entity;
29+
import com.google.cloud.datastore.Query;
30+
import com.google.cloud.datastore.StructuredQuery;
31+
import com.google.cloud.datastore.models.ExecutionStats;
32+
import com.google.cloud.datastore.models.ExplainMetrics;
33+
import com.google.cloud.datastore.models.ExplainOptions;
34+
import com.google.cloud.datastore.models.PlanSummary;
35+
import com.google.common.collect.Iterables;
36+
import java.util.List;
37+
import java.util.Map;
38+
39+
public class QueryProfileExplainAnalyzeAggregation {
40+
public static void invoke() throws Exception {
41+
// Instantiates a client
42+
Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
43+
44+
// Build the query
45+
StructuredQuery<Entity> query = Query.newEntityQueryBuilder().setKind("Task").build();
46+
47+
// Build the aggregation query
48+
AggregationQuery aggregationQuery =
49+
Query.newAggregationQueryBuilder()
50+
.over(query)
51+
.addAggregation(count().as("countVal"))
52+
.build();
53+
54+
// Set the explain options with analyze = true to get back the query stats, plan info, and
55+
// aggregation results
56+
AggregationResults results =
57+
datastore.runAggregation(
58+
aggregationQuery, ExplainOptions.newBuilder().setAnalyze(true).build());
59+
60+
if (!results.getExplainMetrics().isPresent()) {
61+
throw new Exception("No explain metrics returned");
62+
}
63+
ExplainMetrics explainMetrics = results.getExplainMetrics().get();
64+
65+
if (!explainMetrics.getExecutionStats().isPresent()) {
66+
throw new Exception("No execution stats returned");
67+
}
68+
ExecutionStats executionStats = explainMetrics.getExecutionStats().get();
69+
Map<String, Object> debugStats = executionStats.getDebugStats();
70+
System.out.println("----- Debug Stats -----");
71+
debugStats.forEach((key, val) -> System.out.println(key + ": " + val));
72+
System.out.println("----------");
73+
74+
long resultsReturned = executionStats.getResultsReturned();
75+
System.out.println("Results returned: " + resultsReturned);
76+
77+
// Get the plan summary
78+
PlanSummary planSummary = explainMetrics.getPlanSummary();
79+
List<Map<String, Object>> indexesUsed = planSummary.getIndexesUsed();
80+
System.out.println("----- Indexes Used -----");
81+
indexesUsed.forEach(map -> map.forEach((key, val) -> System.out.println(key + ": " + val)));
82+
83+
System.out.println("----- Aggregation Results -----");
84+
AggregationResult result = Iterables.getOnlyElement(results);
85+
System.out.println("Count: " + result.getLong("countVal"));
86+
}
87+
}
88+
// [END datastore_query_explain_analyze_aggregation]

samples/snippets/src/test/java/com/example/datastore/AggregationQuerySampleTestIT.java renamed to samples/snippets/src/test/java/com/example/datastore/aggregation/AggregationQuerySampleTestIT.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example.datastore;
17+
package com.example.datastore.aggregation;
1818

1919
import static org.junit.Assert.assertThrows;
2020

21-
import com.example.datastore.aggregation.AvgAggregationOnKind;
22-
import com.example.datastore.aggregation.AvgAggregationWithLimit;
23-
import com.example.datastore.aggregation.AvgAggregationWithOrderBy;
24-
import com.example.datastore.aggregation.AvgAggregationWithPropertyFilter;
25-
import com.example.datastore.aggregation.CountAggregationInTransaction;
26-
import com.example.datastore.aggregation.CountAggregationOnKind;
27-
import com.example.datastore.aggregation.CountAggregationWithGqlQuery;
28-
import com.example.datastore.aggregation.CountAggregationWithLimit;
29-
import com.example.datastore.aggregation.CountAggregationWithOrderBy;
30-
import com.example.datastore.aggregation.CountAggregationWithPropertyFilter;
31-
import com.example.datastore.aggregation.CountAggregationWithStaleRead;
32-
import com.example.datastore.aggregation.MultipleAggregationsInGqlQuery;
33-
import com.example.datastore.aggregation.MultipleAggregationsInStructuredQuery;
34-
import com.example.datastore.aggregation.SumAggregationOnKind;
35-
import com.example.datastore.aggregation.SumAggregationWithLimit;
36-
import com.example.datastore.aggregation.SumAggregationWithOrderBy;
37-
import com.example.datastore.aggregation.SumAggregationWithPropertyFilter;
3821
import com.google.cloud.datastore.Datastore;
3922
import com.google.cloud.datastore.DatastoreOptions;
4023
import com.google.cloud.datastore.Key;

samples/snippets/src/test/java/com/example/datastore/OrFilterQuerySampleIT.java renamed to samples/snippets/src/test/java/com/example/datastore/filters/OrFilterQuerySampleIT.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example.datastore;
17+
package com.example.datastore.filters;
1818

19-
import com.example.datastore.filters.OrFilterQuery;
2019
import com.google.cloud.datastore.Datastore;
2120
import com.google.cloud.datastore.DatastoreOptions;
2221
import com.google.cloud.datastore.Entity;

0 commit comments

Comments
 (0)