Skip to content

Commit 16a720a

Browse files
authored
chore: java21 upgrade for bigquery folder (#10158)
* chore: java21 upgrade for bigquery folder * updating header for BigQueryRun.java
1 parent 5ea7fc0 commit 16a720a

9 files changed

Lines changed: 841 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=appengine-java21/bigquery/README.md">
2+
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>
3+
4+
# Google Cloud API Showcase: BigQuery & Cloud Monitoring in App Engine standard environment for Java 21
5+
6+
This API Showcase demonstrates how to run an App Engine standard environment application with dependencies on both
7+
[Google BigQuery][bigquery] and [Cloud Monitoring][monitoring].
8+
9+
[bigquery]: https://cloud.google.com/bigquery/docs
10+
[monitoring]: https://cloud.google.com/monitoring/docs
11+
12+
The home page of this application provides a form to initiate a query of public data, in this case StackOverflow
13+
questions tagged with `google-bigquery`.
14+
15+
The home page also provides a summary view of the metrics that have been logged in the past 30 days.
16+
17+
## Clone the sample app
18+
19+
Copy the sample apps to your local machine, and cd to the `appengine-java21/bigquery` directory:
20+
21+
```
22+
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
23+
cd appengine-java21/bigquery
24+
```
25+
26+
## Setup
27+
28+
- Make sure [`gcloud`](https://cloud.google.com/sdk/docs/) is installed and initialized:
29+
```
30+
gcloud init
31+
```
32+
- If this is the first time you are creating an App Engine project
33+
```
34+
gcloud app create
35+
```
36+
- For local development, [set up][set-up] authentication
37+
- Enable [BigQuery][bigquery-api] and [Monitoring][monitoring-api] APIs
38+
39+
[set-up]: https://cloud.google.com/docs/authentication/getting-started
40+
[bigquery-api]: https://console.cloud.google.com/launcher/details/google/bigquery-json.googleapis.com
41+
[monitoring-api]: https://console.cloud.google.com/launcher/details/google/monitoring.googleapis.com
42+
43+
## Run locally
44+
Run using shown Maven command. You can then direct your browser to `http://localhost:8080/` to see the most recent query
45+
run (since the app started) and the metrics from the past 30 days.
46+
47+
```
48+
mvn appengine:run
49+
```
50+
51+
Note: The first time the app is run (or after any metrics definitions have been deleted) it may take up to 5 minutes for
52+
the MetricDescriptors to sync with StackDriver before any results are shown. If you do not see results, please wait a
53+
few moments and try again.
54+
55+
## Deploy
56+
57+
- Deploy to App Engine standard environment using the following Maven command.
58+
```
59+
mvn clean package appengine:deploy
60+
```
61+
- Direct your browser to `https://<your-project-id>.appspot.com`.
62+
- View more in-depth metrics data on the [Cloud Monitoring Dashboard][dashboard]
63+
64+
[dashboard]: https://pantheon.corp.google.com/monitoring
65+
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<!--
2+
Copyright 2018 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+
<project xmlns="http://maven.apache.org/POM/4.0.0"
17+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
19+
<modelVersion>4.0.0</modelVersion>
20+
<packaging>war</packaging>
21+
<version>1.0-SNAPSHOT</version>
22+
<groupId>com.example.appengine</groupId>
23+
<artifactId>appengine-bigquery-monitoring-j21</artifactId>
24+
25+
<!--
26+
The parent pom defines common style checks and testing strategies for our samples.
27+
Removing or replacing it should not effect the execution of the samples in anyway.
28+
-->
29+
<parent>
30+
<groupId>com.google.cloud.samples</groupId>
31+
<artifactId>shared-configuration</artifactId>
32+
<version>1.2.0</version>
33+
</parent>
34+
35+
<properties>
36+
<maven.compiler.target>1.8</maven.compiler.target>
37+
<maven.compiler.source>1.8</maven.compiler.source>
38+
</properties>
39+
40+
<dependencyManagement>
41+
<dependencies>
42+
<dependency>
43+
<artifactId>libraries-bom</artifactId>
44+
<groupId>com.google.cloud</groupId>
45+
<scope>import</scope>
46+
<type>pom</type>
47+
<version>26.28.0</version>
48+
</dependency>
49+
</dependencies>
50+
</dependencyManagement>
51+
52+
<dependencies>
53+
<dependency>
54+
<groupId>com.google.appengine</groupId>
55+
<artifactId>appengine-api-1.0-sdk</artifactId>
56+
<version>2.0.23</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>javax.servlet</groupId>
60+
<artifactId>javax.servlet-api</artifactId>
61+
<version>3.1.0</version>
62+
<type>jar</type>
63+
<scope>provided</scope>
64+
</dependency>
65+
66+
<dependency>
67+
<groupId>com.google.cloud</groupId>
68+
<artifactId>google-cloud-bigquery</artifactId>
69+
</dependency>
70+
<dependency>
71+
<groupId>com.google.cloud</groupId>
72+
<artifactId>google-cloud-monitoring</artifactId>
73+
</dependency>
74+
75+
<!-- Test Dependencies -->
76+
<dependency>
77+
<groupId>com.google.appengine</groupId>
78+
<artifactId>appengine-api-stubs</artifactId>
79+
<version>2.0.23</version>
80+
<scope>test</scope>
81+
</dependency>
82+
<dependency>
83+
<groupId>com.google.appengine</groupId>
84+
<artifactId>appengine-tools-sdk</artifactId>
85+
<version>2.0.23</version>
86+
<scope>test</scope>
87+
</dependency>
88+
89+
<dependency>
90+
<groupId>junit</groupId>
91+
<artifactId>junit</artifactId>
92+
<version>4.13.2</version>
93+
<scope>test</scope>
94+
</dependency>
95+
<dependency>
96+
<groupId>org.mockito</groupId>
97+
<artifactId>mockito-core</artifactId>
98+
<version>4.11.0</version>
99+
<scope>test</scope>
100+
</dependency>
101+
<dependency>
102+
<groupId>com.google.appengine</groupId>
103+
<artifactId>appengine-testing</artifactId>
104+
<version>2.0.23</version>
105+
<scope>test</scope>
106+
</dependency>
107+
<dependency>
108+
<groupId>com.google.truth</groupId>
109+
<artifactId>truth</artifactId>
110+
<version>1.1.5</version>
111+
<scope>test</scope>
112+
</dependency>
113+
</dependencies>
114+
<build>
115+
<!-- for hot reload of the web application -->
116+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
117+
<plugins>
118+
<plugin>
119+
<groupId>com.google.cloud.tools</groupId>
120+
<artifactId>appengine-maven-plugin</artifactId>
121+
<version>2.5.0</version>
122+
<configuration>
123+
<projectId>GCLOUD_CONFIG</projectId>
124+
<version>GCLOUD_CONFIG</version>
125+
<deploy.promote>true</deploy.promote>
126+
<deploy.stopPreviousVersion>true</deploy.stopPreviousVersion>
127+
</configuration>
128+
</plugin>
129+
130+
<plugin>
131+
<groupId>org.apache.maven.plugins</groupId>
132+
<artifactId>maven-war-plugin</artifactId>
133+
<version>3.4.0</version>
134+
</plugin>
135+
136+
</plugins>
137+
</build>
138+
</project>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2018 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.appengine.bigquerylogging;
18+
19+
import com.google.cloud.bigquery.FieldValueList;
20+
import com.google.cloud.bigquery.TableResult;
21+
import java.io.IOException;
22+
import java.util.List;
23+
24+
public class BigQueryHome {
25+
private static BigQueryRunner queryRunner;
26+
27+
private static BigQueryRunner getQueryRunner() throws IOException {
28+
if (queryRunner == null) {
29+
queryRunner = BigQueryRunner.getInstance();
30+
}
31+
return queryRunner;
32+
}
33+
34+
public static String getMostRecentRun() throws IOException {
35+
return convertRunToHtmlTable(BigQueryRunner.getMostRecentRunResult());
36+
}
37+
38+
public static String getMetricAverages() throws IOException {
39+
return convertAveragesToHtmlTable(getQueryRunner().getTimeSeriesValues());
40+
}
41+
42+
private static String convertRunToHtmlTable(TableResult result) {
43+
if (result == null) {
44+
return "";
45+
}
46+
47+
StringBuilder sb = new StringBuilder();
48+
for (FieldValueList row : result.iterateAll()) {
49+
sb.append("<tr>");
50+
String url = row.get("url").getStringValue();
51+
addColumn(sb, String.format("<a href=\"%s\">%s</a>", url, url));
52+
addColumn(sb, row.get("view_count").getLongValue());
53+
sb.append("</tr>");
54+
}
55+
return sb.toString();
56+
}
57+
58+
private static String convertAveragesToHtmlTable(List<TimeSeriesSummary<?>> values) {
59+
60+
StringBuilder sb = new StringBuilder();
61+
for (TimeSeriesSummary<?> metric : values) {
62+
sb.append("<tr>");
63+
addColumn(sb, metric.getName());
64+
addColumn(sb, metric.getValues().size());
65+
addColumn(sb, metric.getMostRecentRunTime());
66+
addColumn(sb, metric.getMostRecentValue());
67+
addColumn(sb, metric.getAverage());
68+
sb.append("</tr>");
69+
}
70+
return sb.toString();
71+
}
72+
73+
private static <T> void addColumn(StringBuilder sb, T content) {
74+
sb.append("<td>").append(content.toString()).append("</td>");
75+
}
76+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2018 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.appengine.bigquerylogging;
18+
19+
import java.io.IOException;
20+
import javax.servlet.annotation.WebServlet;
21+
import javax.servlet.http.HttpServlet;
22+
import javax.servlet.http.HttpServletRequest;
23+
import javax.servlet.http.HttpServletResponse;
24+
25+
@WebServlet(name = "runQuery BigQuery", value = "/bigquery/run")
26+
public class BigQueryRun extends HttpServlet {
27+
private BigQueryRunner queryRunner;
28+
29+
public BigQueryRun() throws IOException {
30+
this.queryRunner = BigQueryRunner.getInstance();
31+
}
32+
33+
@Override
34+
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
35+
try {
36+
queryRunner.runQuery();
37+
} catch (InterruptedException e) {
38+
resp.sendError(
39+
HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Interrupted while running BigQuery job.");
40+
}
41+
// redirect to home page
42+
resp.sendRedirect("/");
43+
}
44+
}

0 commit comments

Comments
 (0)