Skip to content

Commit 8309831

Browse files
committed
Adding BigQuery + StackDriver Monitoring API Showcase sample.
1 parent 398f9c3 commit 8309831

File tree

5 files changed

+502
-2
lines changed

5 files changed

+502
-2
lines changed

bigquery/cloud-client/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,45 @@ documentation](https://cloud.google.com/bigquery/create-simple-app-api):
4646

4747
mvn exec:java -Dexec.mainClass=com.example.bigquery.SimpleApp
4848

49+
## BigQuery + Logging SimpleApp
50+
51+
This API Showcase demonstrates how to run a BigQuery query and then log some metrics to StackDriver Monitoring
52+
from the results, including the query runtime and number of rows returned.
53+
54+
### Clone the sample app
55+
56+
Copy the sample apps to your local machine, and cd to the bigquery/cloud-client directory:
57+
58+
```
59+
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
60+
cd java-docs-samples/bigquery/cloud-client
61+
```
62+
63+
### Setup
64+
65+
- Make sure [`gcloud`](https://cloud.google.com/sdk/docs/) is installed and initialized:
66+
```
67+
gcloud init
68+
```
69+
- If this is the first time you are creating an App Engine project
70+
```
71+
gcloud app create
72+
```
73+
- For local development, [set up][set-up] authentication
74+
- Enable [BigQuery][bigquery-api] and [Monitoring][monitoring-api] APIs
75+
- If you have not already enabled your project for StackDriver, do so by following [these instructions][stackdriver-setup].
76+
77+
[set-up]: https://cloud.google.com/docs/authentication/getting-started
78+
[bigquery-api]: https://console.cloud.google.com/launcher/details/google/bigquery-json.googleapis.com
79+
[monitoring-api]: https://console.cloud.google.com/launcher/details/google/monitoring.googleapis.com
80+
[stackdriver-setup]: https://cloud.google.com/monitoring/accounts/tiers#not-enabled
81+
82+
### Run the SimpleApp
83+
84+
Build your project with:
85+
86+
mvn clean package -DskipTests
87+
88+
Provide the projectId as a system variable when running the sample.
89+
90+
mvn exec:java -Dexec.mainClass=com.example.bigquery_logging.SimpleApp -DprojectId=<your-project-id>

bigquery/cloud-client/pom.xml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
</parent>
3131

3232
<properties>
33-
<maven.compiler.target>1.7</maven.compiler.target>
34-
<maven.compiler.source>1.7</maven.compiler.source>
33+
<maven.compiler.target>1.8</maven.compiler.target>
34+
<maven.compiler.source>1.8</maven.compiler.source>
3535
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3636
</properties>
3737

@@ -43,6 +43,15 @@
4343
<version>0.33.0-beta</version>
4444
</dependency>
4545
<!-- [END dependencies] -->
46+
47+
<!-- [START monitoring_dependencies ] -->
48+
<dependency>
49+
<groupId>com.google.cloud</groupId>
50+
<artifactId>google-cloud-monitoring</artifactId>
51+
<version>0.33.0-beta</version>
52+
</dependency>
53+
<!-- [END monitoring_dependencies ] -->
54+
4655
<dependency>
4756
<groupId>commons-cli</groupId>
4857
<artifactId>commons-cli</artifactId>
@@ -67,5 +76,11 @@
6776
<version>0.39</version>
6877
<scope>test</scope>
6978
</dependency>
79+
<dependency>
80+
<groupId>org.mockito</groupId>
81+
<artifactId>mockito-core</artifactId>
82+
<version>2.13.0</version>
83+
<scope>test</scope>
84+
</dependency>
7085
</dependencies>
7186
</project>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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.bigquery_logging;
18+
19+
import com.google.api.MetricDescriptor;
20+
21+
import java.util.Objects;
22+
23+
public class RequiredMetric {
24+
private String shortName;
25+
private String name;
26+
private String description;
27+
private MetricDescriptor.MetricKind metricKind;
28+
private MetricDescriptor.ValueType valueType;
29+
30+
public RequiredMetric(String shortName, String description) {
31+
this.shortName = shortName;
32+
this.name = "custom.googleapis.com/" + shortName;
33+
this.description = description;
34+
this.metricKind = MetricDescriptor.MetricKind.GAUGE;
35+
this.valueType = MetricDescriptor.ValueType.INT64;
36+
}
37+
38+
public RequiredMetric(String shortName,
39+
String name,
40+
String description,
41+
MetricDescriptor.MetricKind metricKind,
42+
MetricDescriptor.ValueType valueType) {
43+
this.shortName = shortName;
44+
this.name = name;
45+
this.description = description;
46+
this.metricKind = metricKind;
47+
this.valueType = valueType;
48+
}
49+
50+
public String getShortName() {
51+
return shortName;
52+
}
53+
54+
public String getName() {
55+
return name;
56+
}
57+
58+
public String getDescription() {
59+
return description;
60+
}
61+
62+
public MetricDescriptor.MetricKind getMetricKind() {
63+
return metricKind;
64+
}
65+
66+
public MetricDescriptor.ValueType getValueType() {
67+
return valueType;
68+
}
69+
70+
@Override
71+
public boolean equals(Object o) {
72+
if (this == o) return true;
73+
if (o == null || getClass() != o.getClass()) return false;
74+
RequiredMetric that = (RequiredMetric) o;
75+
return Objects.equals(name, that.name) &&
76+
metricKind == that.metricKind &&
77+
valueType == that.valueType;
78+
}
79+
80+
@Override
81+
public int hashCode() {
82+
return Objects.hash(name, metricKind, valueType);
83+
}
84+
85+
86+
}

0 commit comments

Comments
 (0)