Skip to content

Commit 5aab7af

Browse files
committed
Adding BigQuery + StackDriver Monitoring sample.
1 parent e0918ef commit 5aab7af

File tree

10 files changed

+835
-0
lines changed

10 files changed

+835
-0
lines changed

appengine-java8/bigquery/README.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Google Cloud API Showcase: Cloud BigQuery & StackDriver Monitoring in App Engine Standard Java 8 Environment
2+
3+
This API Showcase demonstrates how to run an AppEngine Standard application with dependencies on both
4+
[Google BigQuery][bigquery] and [StackDriver Monitoring][stackdriver].
5+
6+
[bigquery]: https://cloud.google.com/bigquery/docs
7+
[stackdriver]: https://cloud.google.com/monitoring/docs
8+
9+
The home page of this application provides a form to initiate a query of public data, in this case StackOverflow
10+
questions tagged with `google-bigquery`.
11+
12+
The home page also provides a summary view of the metrics that have been logged in the past 30 days.
13+
14+
## Clone the sample app
15+
16+
Copy the sample apps to your local machine, and cd to the appengine-java8/bigquery directory:
17+
18+
```
19+
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
20+
cd appengine-java8/bigquery
21+
```
22+
23+
## Setup
24+
25+
- Make sure [`gcloud`](https://cloud.google.com/sdk/docs/) is installed and initialized:
26+
```
27+
gcloud init
28+
```
29+
- If this is the first time you are creating an App Engine project
30+
```
31+
gcloud app create
32+
```
33+
- For local development, [set up][set-up] authentication
34+
- Enable [BigQuery][bigquery-api] and [Monitoring][monitoring-api] APIs
35+
- If you have not already enabled your project for StackDriver, do so by following [these instructions][stackdriver-setup].
36+
37+
[set-up]: https://cloud.google.com/docs/authentication/getting-started
38+
[bigquery-api]: https://console.cloud.google.com/launcher/details/google/bigquery-json.googleapis.com
39+
[monitoring-api]: https://console.cloud.google.com/launcher/details/google/monitoring.googleapis.com
40+
[stackdriver-setup]: https://cloud.google.com/monitoring/accounts/tiers#not-enabled
41+
42+
## Run locally
43+
Run using shown Maven command. You can then direct your browser to `http://localhost:8080/` to see the most recent query
44+
run (since the app started) and the metrics from the past 30 days.
45+
46+
```
47+
mvn appengine:run
48+
```
49+
50+
## Deploy
51+
52+
- Deploy to AppEngine Standard using the following Maven command.
53+
```
54+
mvn appengine:deploy
55+
```
56+
- Direct your browser to `https://<your-project-id>.appspot.com`.
57+
- View more in-depth metrics data on the [StackDriver Monitoring Dashboard][dashboard]
58+
59+
[dashboard]: https://pantheon.corp.google.com/monitoring
60+

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,62 @@
1+
package com.example.appengine.bigquery_logging;
2+
3+
import com.google.cloud.bigquery.FieldValueList;
4+
import com.google.cloud.bigquery.TableResult;
5+
import com.google.protobuf.util.Timestamps;
6+
7+
import java.io.IOException;
8+
import java.util.List;
9+
10+
public class BigQueryHome {
11+
private static BigQueryRunner queryRunner;
12+
13+
private static BigQueryRunner getQueryRunner() throws IOException {
14+
if (queryRunner == null) {
15+
queryRunner = BigQueryRunner.getInstance();
16+
}
17+
return queryRunner;
18+
}
19+
20+
public static String getMostRecentRun() throws IOException {
21+
return convertRunToHtmlTable(getQueryRunner().getMostRecentRunResult());
22+
}
23+
24+
public static String getMetricAverages() throws IOException {
25+
return convertAveragesToHtmlTable(getQueryRunner().getTimeSeriesValues());
26+
}
27+
28+
private static String convertRunToHtmlTable(TableResult result) {
29+
if (result == null) {
30+
return "";
31+
}
32+
33+
StringBuilder sb = new StringBuilder();
34+
for (FieldValueList row : result.iterateAll()) {
35+
sb.append("<tr>");
36+
String url = row.get("url").getStringValue();
37+
addColumn(sb, String.format("<a href=\"%s\">%s</a>", url, url));
38+
addColumn(sb, row.get("view_count").getLongValue());
39+
sb.append("</tr>");
40+
}
41+
return sb.toString();
42+
}
43+
44+
private static String convertAveragesToHtmlTable(List<TimeSeriesSummary> values) {
45+
46+
StringBuilder sb = new StringBuilder();
47+
for(TimeSeriesSummary metric : values) {
48+
sb.append("<tr>");
49+
addColumn(sb, metric.getName());
50+
addColumn(sb, metric.getValues().size());
51+
addColumn(sb, metric.getMostRecentRunTime());
52+
addColumn(sb, metric.getMostRecentValue());
53+
addColumn(sb, metric.getAverage());
54+
sb.append("</tr>");
55+
}
56+
return sb.toString();
57+
}
58+
59+
private static <T> void addColumn(StringBuilder sb, T content) {
60+
sb.append("<td>").append(content.toString()).append("</td>");
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.bigquery_logging;
18+
19+
import javax.servlet.annotation.WebServlet;
20+
import javax.servlet.http.HttpServlet;
21+
import javax.servlet.http.HttpServletRequest;
22+
import javax.servlet.http.HttpServletResponse;
23+
import java.io.IOException;
24+
25+
@WebServlet(name = "Run 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+
BigQueryRunner queryRunner = this.queryRunner;
36+
try {
37+
queryRunner.Run();
38+
} catch (InterruptedException e) {
39+
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
40+
"Interrupted while running BigQuery job.");
41+
}
42+
// redirect to home page
43+
resp.sendRedirect("/");
44+
}
45+
}

0 commit comments

Comments
 (0)