GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BigQuery REST samples to label datasets and tables. · keirsan/java-docs-samples@2c6a7aa

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • bigquery
    • pom.xml
    • rest
      • README.md
      • pom.xml
      • src
        • main/java/com/example/bigquery
          • LabelsSample.java
        • test/java/com/example/bigquery
          • Constants.java
          • LabelsSampleIT.java
  • pom.xml
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎bigquery/pom.xml‎

Copy file name to clipboardExpand all lines: bigquery/pom.xml
+6-17Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,12 @@
7575
</properties>
7676

7777
<build>
78-
<sourceDirectory>src/main/java</sourceDirectory>
79-
<resources>
80-
<resource>
81-
<directory>src/main/resources</directory>
82-
</resource>
83-
</resources>
84-
<plugins>
85-
<plugin>
86-
<groupId>org.apache.maven.plugins</groupId>
87-
<artifactId>maven-compiler-plugin</artifactId>
88-
<version>3.2</version>
89-
<configuration>
90-
<source>5</source>
91-
<target>5</target>
92-
</configuration>
93-
</plugin>
94-
</plugins>
78+
<sourceDirectory>src/main/java</sourceDirectory>
79+
<resources>
80+
<resource>
81+
<directory>src/main/resources</directory>
82+
</resource>
83+
</resources>
9584
</build>
9685

9786
</project>
Collapse file

‎bigquery/rest/README.md‎

Copy file name to clipboard
+40Lines changed: 40 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Getting Started with BigQuery with the REST API
2+
3+
Google's BigQuery Service features a REST-based API that allows developers to
4+
create applications to run ad-hoc queries on massive datasets. These sample
5+
Java applications demonstrate how to access the BigQuery API directly using the
6+
Google HTTP Client.
7+
8+
## Quickstart
9+
10+
Install [Maven](http://maven.apache.org/).
11+
12+
Build your project with:
13+
14+
mvn clean package -DskipTests
15+
16+
You can then run a given `ClassName` via:
17+
18+
mvn exec:java -Dexec.mainClass=com.example.bigquery.ClassName \
19+
-Dexec.args="any arguments to the app"
20+
21+
### Labeling a dataset
22+
23+
[Label a dataset](https://cloud.google.com/bigquery/docs/labeling-datasets).
24+
25+
mvn exec:java -Dexec.mainClass=com.example.bigquery.LabelsSample \
26+
-Dexec.args="project-id dataset-id label-key label-value"
27+
28+
## Products
29+
- [Google BigQuery][2]
30+
31+
## Language
32+
- [Java][3]
33+
34+
## Dependencies
35+
- [Google HTTP Client Library for Java][4]
36+
37+
[2]: https://cloud.google.com/bigquery
38+
[3]: https://java.com
39+
[4]: https://github.com/google/google-http-java-client
40+
Collapse file

‎bigquery/rest/pom.xml‎

Copy file name to clipboard
+100Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<!--
2+
Copyright 2016 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+
<version>1.0.0</version>
19+
20+
<groupId>com.google.cloud.samples</groupId>
21+
<artifactId>bigquery-rest-samples</artifactId>
22+
<packaging>jar</packaging>
23+
24+
<!-- Parent POM defines common plugins and properties. -->
25+
<parent>
26+
<artifactId>doc-samples</artifactId>
27+
<groupId>com.google.cloud</groupId>
28+
<version>1.0.0</version>
29+
<relativePath>../..</relativePath>
30+
</parent>
31+
32+
<properties>
33+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
34+
</properties>
35+
36+
<dependencies>
37+
<dependency>
38+
<groupId>com.google.guava</groupId>
39+
<artifactId>guava</artifactId>
40+
<version>20.0</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>com.google.api-client</groupId>
44+
<artifactId>google-api-client</artifactId>
45+
<version>1.22.0</version>
46+
<exclusions>
47+
<exclusion> <!-- exclude an old version of Guava -->
48+
<groupId>com.google.guava</groupId>
49+
<artifactId>guava-jdk5</artifactId>
50+
</exclusion>
51+
</exclusions>
52+
</dependency>
53+
<dependency>
54+
<groupId>com.google.http-client</groupId>
55+
<artifactId>google-http-client</artifactId>
56+
<version>1.22.0</version>
57+
<exclusions>
58+
<exclusion> <!-- exclude an old version of Guava -->
59+
<groupId>com.google.guava</groupId>
60+
<artifactId>guava-jdk5</artifactId>
61+
</exclusion>
62+
</exclusions>
63+
</dependency>
64+
<dependency>
65+
<groupId>com.google.oauth-client</groupId>
66+
<artifactId>google-oauth-client</artifactId>
67+
<version>1.22.0</version>
68+
<exclusions>
69+
<exclusion> <!-- exclude an old version of Guava -->
70+
<groupId>com.google.guava</groupId>
71+
<artifactId>guava-jdk5</artifactId>
72+
</exclusion>
73+
</exclusions>
74+
</dependency>
75+
76+
<!-- Test dependencies -->
77+
<dependency>
78+
<groupId>junit</groupId>
79+
<artifactId>junit</artifactId>
80+
<version>4.12</version>
81+
<scope>test</scope>
82+
</dependency>
83+
<dependency>
84+
<groupId>com.google.truth</groupId>
85+
<artifactId>truth</artifactId>
86+
<version>0.31</version>
87+
<scope>test</scope>
88+
</dependency>
89+
</dependencies>
90+
91+
<build>
92+
<sourceDirectory>src/main/java</sourceDirectory>
93+
<resources>
94+
<resource>
95+
<directory>src/main/resources</directory>
96+
</resource>
97+
</resources>
98+
</build>
99+
100+
</project>
Collapse file

‎bigquery/rest/src/main/java/com/example/bigquery/LabelsSample.java‎

Copy file name to clipboard
+210Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
/*
2+
* Copyright 2016 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;
18+
19+
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
20+
import com.google.api.client.http.GenericUrl;
21+
import com.google.api.client.http.HttpContent;
22+
import com.google.api.client.http.HttpHeaders;
23+
import com.google.api.client.http.HttpRequest;
24+
import com.google.api.client.http.HttpRequestFactory;
25+
import com.google.api.client.http.HttpResponse;
26+
import com.google.api.client.http.HttpTransport;
27+
import com.google.api.client.http.javanet.NetHttpTransport;
28+
import com.google.api.client.http.json.JsonHttpContent;
29+
import com.google.api.client.json.JsonFactory;
30+
import com.google.api.client.json.jackson2.JacksonFactory;
31+
import com.google.api.client.util.Key;
32+
33+
import java.io.IOException;
34+
import java.util.Arrays;
35+
import java.util.HashMap;
36+
import java.util.Map;
37+
38+
/** Sample demonstrating labeling a BigQuery dataset or table. */
39+
public class LabelsSample {
40+
41+
// [START label_dataset]
42+
static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
43+
static final JsonFactory JSON_FACTORY = new JacksonFactory();
44+
45+
public static class Dataset {
46+
@Key private Map<String, String> labels;
47+
48+
public Map<String, String> getLabels() {
49+
return this.labels;
50+
}
51+
52+
public Dataset addLabel(String key, String value) {
53+
if (this.labels == null) {
54+
this.labels = new HashMap<>();
55+
}
56+
this.labels.put(key, value);
57+
return this;
58+
}
59+
}
60+
61+
/**
62+
* Add or modify a label on a dataset.
63+
*
64+
* <p>See <a href="https://cloud.google.com/bigquery/docs/labeling-datasets">the BigQuery
65+
* documentation</a>.
66+
*/
67+
public static void labelDataset(
68+
String projectId, String datasetId, String labelKey, String labelValue) throws IOException {
69+
70+
// Authenticate requests using Google Application Default credentials.
71+
GoogleCredential credential = GoogleCredential.getApplicationDefault();
72+
credential = credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/bigquery"));
73+
74+
// Get a new access token.
75+
// Note that access tokens have an expiration. You can reuse a token rather than requesting a
76+
// new one if it is not yet expired.
77+
credential.refreshToken();
78+
String accessToken = credential.getAccessToken();
79+
80+
// Set the content of the request.
81+
Dataset dataset = new Dataset();
82+
dataset.addLabel(labelKey, labelValue);
83+
HttpContent content = new JsonHttpContent(JSON_FACTORY, dataset);
84+
85+
// Send the request to the BigQuery API.
86+
String urlFormat =
87+
"https://www.googleapis.com/bigquery/v2/projects/%s/datasets/%s"
88+
+ "?fields=labels&access_token=%s";
89+
GenericUrl url = new GenericUrl(String.format(urlFormat, projectId, datasetId, accessToken));
90+
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory();
91+
HttpRequest request = requestFactory.buildPostRequest(url, content);
92+
request.setParser(JSON_FACTORY.createJsonObjectParser());
93+
94+
// Workaround for transports which do not support PATCH requests.
95+
// See: http://stackoverflow.com/a/32503192/101923
96+
request.setHeaders(new HttpHeaders().set("X-HTTP-Method-Override", "PATCH"));
97+
HttpResponse response = request.execute();
98+
99+
// Check for errors.
100+
if (response.getStatusCode() != 200) {
101+
throw new RuntimeException(response.getStatusMessage());
102+
}
103+
104+
Dataset responseDataset = response.parseAs(Dataset.class);
105+
System.out.printf(
106+
"Updated label \"%s\" with value \"%s\"\n",
107+
labelKey, responseDataset.getLabels().get(labelKey));
108+
}
109+
// [END label_dataset]
110+
111+
// [START label_table]
112+
public static class Table {
113+
@Key private Map<String, String> labels;
114+
115+
public Map<String, String> getLabels() {
116+
return this.labels;
117+
}
118+
119+
public Table addLabel(String key, String value) {
120+
if (this.labels == null) {
121+
this.labels = new HashMap<>();
122+
}
123+
this.labels.put(key, value);
124+
return this;
125+
}
126+
}
127+
128+
/**
129+
* Add or modify a label on a table.
130+
*
131+
* <p>See <a href="https://cloud.google.com/bigquery/docs/labeling-datasets">the BigQuery
132+
* documentation</a>.
133+
*/
134+
public static void labelTable(
135+
String projectId,
136+
String datasetId,
137+
String tableId,
138+
String labelKey,
139+
String labelValue)
140+
throws IOException {
141+
142+
// Authenticate requests using Google Application Default credentials.
143+
GoogleCredential credential = GoogleCredential.getApplicationDefault();
144+
credential = credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/bigquery"));
145+
146+
// Get a new access token.
147+
// Note that access tokens have an expiration. You can reuse a token rather than requesting a
148+
// new one if it is not yet expired.
149+
credential.refreshToken();
150+
String accessToken = credential.getAccessToken();
151+
152+
// Set the content of the request.
153+
Table table = new Table();
154+
table.addLabel(labelKey, labelValue);
155+
HttpContent content = new JsonHttpContent(JSON_FACTORY, table);
156+
157+
// Send the request to the BigQuery API.
158+
String urlFormat =
159+
"https://www.googleapis.com/bigquery/v2/projects/%s/datasets/%s/tables/%s"
160+
+ "?fields=labels&access_token=%s";
161+
GenericUrl url =
162+
new GenericUrl(String.format(urlFormat, projectId, datasetId, tableId, accessToken));
163+
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory();
164+
HttpRequest request = requestFactory.buildPostRequest(url, content);
165+
request.setParser(JSON_FACTORY.createJsonObjectParser());
166+
167+
// Workaround for transports which do not support PATCH requests.
168+
// See: http://stackoverflow.com/a/32503192/101923
169+
request.setHeaders(new HttpHeaders().set("X-HTTP-Method-Override", "PATCH"));
170+
HttpResponse response = request.execute();
171+
172+
// Check for errors.
173+
if (response.getStatusCode() != 200) {
174+
throw new RuntimeException(response.getStatusMessage());
175+
}
176+
177+
Table responseTable = response.parseAs(Table.class);
178+
System.out.printf(
179+
"Updated label \"%s\" with value \"%s\"\n",
180+
labelKey, responseTable.getLabels().get(labelKey));
181+
}
182+
// [END label_table]
183+
184+
public static void printUsage() {
185+
System.err.println("Command expects 4 or 5 arguments:");
186+
System.err.println("\tproject dataset [table] key value");
187+
}
188+
189+
public static void main(final String[] args) throws IOException, InterruptedException {
190+
if (args.length != 4 && args.length != 5) {
191+
printUsage();
192+
System.exit(1);
193+
}
194+
195+
if (args.length == 4) {
196+
String projectId = args[0];
197+
String datasetId = args[1];
198+
String labelKey = args[2];
199+
String labelValue = args[3];
200+
labelDataset(projectId, datasetId, labelKey, labelValue);
201+
} else {
202+
String projectId = args[0];
203+
String datasetId = args[1];
204+
String tableId = args[2];
205+
String labelKey = args[3];
206+
String labelValue = args[4];
207+
labelTable(projectId, datasetId, tableId, labelKey, labelValue);
208+
}
209+
}
210+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.