Skip to content

Commit 5bfacb8

Browse files
dzlier-gcpkurtisvg
authored andcommitted
Adding BigQuery + StackDriver Monitoring sample. (#1001)
1 parent 41df694 commit 5bfacb8

File tree

10 files changed

+867
-0
lines changed

10 files changed

+867
-0
lines changed

appengine-java8/bigquery/README.md

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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-java8/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: Cloud BigQuery & StackDriver Monitoring in App Engine Standard Java 8 Environment
5+
6+
This API Showcase demonstrates how to run an AppEngine Standard application with dependencies on both
7+
[Google BigQuery][bigquery] and [StackDriver Monitoring][stackdriver].
8+
9+
[bigquery]: https://cloud.google.com/bigquery/docs
10+
[stackdriver]: 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-java8/bigquery directory:
20+
21+
```
22+
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
23+
cd appengine-java8/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+
- If you have not already enabled your project for StackDriver, do so by following [these instructions][stackdriver-setup].
39+
40+
[set-up]: https://cloud.google.com/docs/authentication/getting-started
41+
[bigquery-api]: https://console.cloud.google.com/launcher/details/google/bigquery-json.googleapis.com
42+
[monitoring-api]: https://console.cloud.google.com/launcher/details/google/monitoring.googleapis.com
43+
[stackdriver-setup]: https://cloud.google.com/monitoring/accounts/tiers#not-enabled
44+
45+
## Run locally
46+
Run using shown Maven command. You can then direct your browser to `http://localhost:8080/` to see the most recent query
47+
run (since the app started) and the metrics from the past 30 days.
48+
49+
```
50+
mvn appengine:run
51+
```
52+
53+
Note: The first time the app is run (or after any metrics definitions have been deleted) it may take up to 5 minutes for
54+
the MetricDescriptors to sync with StackDriver before any results are shown. If you do not see results, please wait a
55+
few moments and try again.
56+
57+
## Deploy
58+
59+
- Deploy to AppEngine Standard using the following Maven command.
60+
```
61+
mvn appengine:deploy
62+
```
63+
- Direct your browser to `https://<your-project-id>.appspot.com`.
64+
- View more in-depth metrics data on the [StackDriver Monitoring Dashboard][dashboard]
65+
66+
[dashboard]: https://pantheon.corp.google.com/monitoring
67+

appengine-java8/bigquery/pom.xml

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<!--
2+
Copyright 2018 Google Inc.
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>
17+
<modelVersion>4.0.0</modelVersion>
18+
<packaging>war</packaging>
19+
<version>1.0-SNAPSHOT</version>
20+
<groupId>com.example.appengine</groupId>
21+
<artifactId>appengine-bigquery-monitoring-j8</artifactId>
22+
23+
<!--
24+
The parent pom defines common style checks and testing strategies for our samples.
25+
Removing or replacing it should not effect the execution of the samples in anyway.
26+
-->
27+
<parent>
28+
<groupId>com.google.cloud.samples</groupId>
29+
<artifactId>shared-configuration</artifactId>
30+
<version>1.0.8</version>
31+
</parent>
32+
33+
<properties>
34+
<maven.compiler.target>1.8</maven.compiler.target>
35+
<maven.compiler.source>1.8</maven.compiler.source>
36+
</properties>
37+
38+
<dependencies>
39+
<dependency>
40+
<groupId>com.google.appengine</groupId>
41+
<artifactId>appengine-api-1.0-sdk</artifactId>
42+
<version>1.9.60</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>javax.servlet</groupId>
46+
<artifactId>javax.servlet-api</artifactId>
47+
<version>3.1.0</version>
48+
<type>jar</type>
49+
<scope>provided</scope>
50+
</dependency>
51+
52+
<!-- [START dependencies] -->
53+
<dependency>
54+
<groupId>com.google.cloud</groupId>
55+
<artifactId>google-cloud-bigquery</artifactId>
56+
<version>0.33.0-beta</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>com.google.cloud</groupId>
60+
<artifactId>google-cloud-monitoring</artifactId>
61+
<version>0.33.0-beta</version>
62+
</dependency>
63+
<!-- [END dependencies ] -->
64+
65+
<dependency>
66+
<groupId>commons-cli</groupId>
67+
<artifactId>commons-cli</artifactId>
68+
<version>1.4</version>
69+
</dependency>
70+
<dependency>
71+
<groupId>joda-time</groupId>
72+
<artifactId>joda-time</artifactId>
73+
<version>2.9.9</version>
74+
</dependency>
75+
76+
<!-- Test Dependencies -->
77+
<dependency>
78+
<groupId>com.google.appengine</groupId>
79+
<artifactId>appengine-api-stubs</artifactId>
80+
<version>1.9.60</version>
81+
<scope>test</scope>
82+
</dependency>
83+
<dependency>
84+
<groupId>com.google.appengine</groupId>
85+
<artifactId>appengine-tools-sdk</artifactId>
86+
<version>1.9.60</version>
87+
<scope>test</scope>
88+
</dependency>
89+
90+
<dependency>
91+
<groupId>junit</groupId>
92+
<artifactId>junit</artifactId>
93+
<version>4.12</version>
94+
<scope>test</scope>
95+
</dependency>
96+
<dependency>
97+
<groupId>org.mockito</groupId>
98+
<artifactId>mockito-core</artifactId>
99+
<version>2.13.0</version>
100+
<scope>test</scope>
101+
</dependency>
102+
<dependency>
103+
<groupId>com.google.appengine</groupId>
104+
<artifactId>appengine-testing</artifactId>
105+
<version>1.9.60</version>
106+
<scope>test</scope>
107+
</dependency>
108+
<dependency>
109+
<groupId>com.google.truth</groupId>
110+
<artifactId>truth</artifactId>
111+
<version>0.39</version>
112+
<scope>test</scope>
113+
</dependency>
114+
</dependencies>
115+
<build>
116+
<!-- for hot reload of the web application -->
117+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
118+
<plugins>
119+
<plugin>
120+
<groupId>com.google.cloud.tools</groupId>
121+
<artifactId>appengine-maven-plugin</artifactId>
122+
<version>1.3.1</version>
123+
<configuration>
124+
<deploy.promote>true</deploy.promote>
125+
<deploy.stopPreviousVersion>true</deploy.stopPreviousVersion>
126+
</configuration>
127+
</plugin>
128+
129+
<plugin>
130+
<groupId>org.apache.maven.plugins</groupId>
131+
<artifactId>maven-war-plugin</artifactId>
132+
<version>3.1.0</version>
133+
</plugin>
134+
135+
</plugins>
136+
</build>
137+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2018 Google Inc.
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+
22+
import java.io.IOException;
23+
import java.util.List;
24+
25+
public class BigQueryHome {
26+
private static BigQueryRunner queryRunner;
27+
28+
private static BigQueryRunner getQueryRunner() throws IOException {
29+
if (queryRunner == null) {
30+
queryRunner = BigQueryRunner.getInstance();
31+
}
32+
return queryRunner;
33+
}
34+
35+
public static String getMostRecentRun() throws IOException {
36+
return convertRunToHtmlTable(BigQueryRunner.getMostRecentRunResult());
37+
}
38+
39+
public static String getMetricAverages() throws IOException {
40+
return convertAveragesToHtmlTable(getQueryRunner().getTimeSeriesValues());
41+
}
42+
43+
private static String convertRunToHtmlTable(TableResult result) {
44+
if (result == null) {
45+
return "";
46+
}
47+
48+
StringBuilder sb = new StringBuilder();
49+
for (FieldValueList row : result.iterateAll()) {
50+
sb.append("<tr>");
51+
String url = row.get("url").getStringValue();
52+
addColumn(sb, String.format("<a href=\"%s\">%s</a>", url, url));
53+
addColumn(sb, row.get("view_count").getLongValue());
54+
sb.append("</tr>");
55+
}
56+
return sb.toString();
57+
}
58+
59+
private static String convertAveragesToHtmlTable(List<TimeSeriesSummary> values) {
60+
61+
StringBuilder sb = new StringBuilder();
62+
for (TimeSeriesSummary metric : values) {
63+
sb.append("<tr>");
64+
addColumn(sb, metric.getName());
65+
addColumn(sb, metric.getValues().size());
66+
addColumn(sb, metric.getMostRecentRunTime());
67+
addColumn(sb, metric.getMostRecentValue());
68+
addColumn(sb, metric.getAverage());
69+
sb.append("</tr>");
70+
}
71+
return sb.toString();
72+
}
73+
74+
private static <T> void addColumn(StringBuilder sb, T content) {
75+
sb.append("<td>").append(content.toString()).append("</td>");
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2018 Google Inc.
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(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
39+
"Interrupted while running BigQuery job.");
40+
}
41+
// redirect to home page
42+
resp.sendRedirect("/");
43+
}
44+
}

0 commit comments

Comments
 (0)