See the BigQuery
+ * documentation.
+ */
+ public static void labelDataset(
+ String projectId, String datasetId, String labelKey, String labelValue) throws IOException {
+
+ // Authenticate requests using Google Application Default credentials.
+ GoogleCredential credential = GoogleCredential.getApplicationDefault();
+ credential = credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/bigquery"));
+
+ // Get a new access token.
+ // Note that access tokens have an expiration. You can reuse a token rather than requesting a
+ // new one if it is not yet expired.
+ credential.refreshToken();
+ String accessToken = credential.getAccessToken();
+
+ // Set the content of the request.
+ Dataset dataset = new Dataset();
+ dataset.addLabel(labelKey, labelValue);
+ HttpContent content = new JsonHttpContent(JSON_FACTORY, dataset);
+
+ // Send the request to the BigQuery API.
+ String urlFormat =
+ "https://www.googleapis.com/bigquery/v2/projects/%s/datasets/%s"
+ + "?fields=labels&access_token=%s";
+ GenericUrl url = new GenericUrl(String.format(urlFormat, projectId, datasetId, accessToken));
+ HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory();
+ HttpRequest request = requestFactory.buildPostRequest(url, content);
+ request.setParser(JSON_FACTORY.createJsonObjectParser());
+
+ // Workaround for transports which do not support PATCH requests.
+ // See: http://stackoverflow.com/a/32503192/101923
+ request.setHeaders(new HttpHeaders().set("X-HTTP-Method-Override", "PATCH"));
+ HttpResponse response = request.execute();
+
+ // Check for errors.
+ if (response.getStatusCode() != 200) {
+ throw new RuntimeException(response.getStatusMessage());
+ }
+
+ Dataset responseDataset = response.parseAs(Dataset.class);
+ System.out.printf(
+ "Updated label \"%s\" with value \"%s\"\n",
+ labelKey, responseDataset.getLabels().get(labelKey));
+ }
+ // [END label_dataset]
+
+ // [START label_table]
+ public static class Table {
+ @Key private Map See the BigQuery
+ * documentation.
+ */
+ public static void labelTable(
+ String projectId,
+ String datasetId,
+ String tableId,
+ String labelKey,
+ String labelValue)
+ throws IOException {
+
+ // Authenticate requests using Google Application Default credentials.
+ GoogleCredential credential = GoogleCredential.getApplicationDefault();
+ credential = credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/bigquery"));
+
+ // Get a new access token.
+ // Note that access tokens have an expiration. You can reuse a token rather than requesting a
+ // new one if it is not yet expired.
+ credential.refreshToken();
+ String accessToken = credential.getAccessToken();
+
+ // Set the content of the request.
+ Table table = new Table();
+ table.addLabel(labelKey, labelValue);
+ HttpContent content = new JsonHttpContent(JSON_FACTORY, table);
+
+ // Send the request to the BigQuery API.
+ String urlFormat =
+ "https://www.googleapis.com/bigquery/v2/projects/%s/datasets/%s/tables/%s"
+ + "?fields=labels&access_token=%s";
+ GenericUrl url =
+ new GenericUrl(String.format(urlFormat, projectId, datasetId, tableId, accessToken));
+ HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory();
+ HttpRequest request = requestFactory.buildPostRequest(url, content);
+ request.setParser(JSON_FACTORY.createJsonObjectParser());
+
+ // Workaround for transports which do not support PATCH requests.
+ // See: http://stackoverflow.com/a/32503192/101923
+ request.setHeaders(new HttpHeaders().set("X-HTTP-Method-Override", "PATCH"));
+ HttpResponse response = request.execute();
+
+ // Check for errors.
+ if (response.getStatusCode() != 200) {
+ throw new RuntimeException(response.getStatusMessage());
+ }
+
+ Table responseTable = response.parseAs(Table.class);
+ System.out.printf(
+ "Updated label \"%s\" with value \"%s\"\n",
+ labelKey, responseTable.getLabels().get(labelKey));
+ }
+ // [END label_table]
+
+ public static void printUsage() {
+ System.err.println("Command expects 4 or 5 arguments:");
+ System.err.println("\tproject dataset [table] key value");
+ }
+
+ public static void main(final String[] args) throws IOException, InterruptedException {
+ if (args.length != 4 && args.length != 5) {
+ printUsage();
+ System.exit(1);
+ }
+
+ if (args.length == 4) {
+ String projectId = args[0];
+ String datasetId = args[1];
+ String labelKey = args[2];
+ String labelValue = args[3];
+ labelDataset(projectId, datasetId, labelKey, labelValue);
+ } else {
+ String projectId = args[0];
+ String datasetId = args[1];
+ String tableId = args[2];
+ String labelKey = args[3];
+ String labelValue = args[4];
+ labelTable(projectId, datasetId, tableId, labelKey, labelValue);
+ }
+ }
+}
diff --git a/bigquery/rest/src/test/java/com/example/bigquery/Constants.java b/bigquery/rest/src/test/java/com/example/bigquery/Constants.java
new file mode 100644
index 00000000000..c2401339b62
--- /dev/null
+++ b/bigquery/rest/src/test/java/com/example/bigquery/Constants.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2016 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License. You may obtain a
+ * copy of the License at
+ *
+ * http=//www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.example.bigquery;
+
+public class Constants {
+ public static final String DATASET_ID = "test_dataset";
+ public static final String TABLE_ID = "test_table";
+}
diff --git a/bigquery/rest/src/test/java/com/example/bigquery/LabelsSampleIT.java b/bigquery/rest/src/test/java/com/example/bigquery/LabelsSampleIT.java
new file mode 100644
index 00000000000..f9156da0320
--- /dev/null
+++ b/bigquery/rest/src/test/java/com/example/bigquery/LabelsSampleIT.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2016 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License. You may obtain a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.example.bigquery;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+/** Integration tests for sample which labels datasets and tables. */
+@RunWith(JUnit4.class)
+@SuppressWarnings("checkstyle:abbreviationaswordinname")
+public class LabelsSampleIT {
+ private ByteArrayOutputStream bout;
+ private PrintStream out;
+ private String projectId;
+
+ @Before
+ public void setUp() {
+ projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
+ bout = new ByteArrayOutputStream();
+ out = new PrintStream(bout);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() {
+ System.setOut(null);
+ }
+
+ @Test
+ public void testLabelDataset() throws Exception {
+ LabelsSample.main(
+ new String[] {projectId, Constants.DATASET_ID, "environment", "test"});
+ String got = bout.toString();
+ assertThat(got).contains("Updated label \"environment\" with value \"test\"");
+ }
+
+ @Test
+ public void testLabelTable() throws Exception {
+ LabelsSample.main(
+ new String[] {
+ projectId,
+ Constants.DATASET_ID,
+ Constants.TABLE_ID,
+ "data-owner",
+ "my-team"});
+ String got = bout.toString();
+ assertThat(got).contains("Updated label \"data-owner\" with value \"my-team\"");
+ }
+}
diff --git a/pom.xml b/pom.xml
index 20c9254cb0f..26cc5708d72 100644
--- a/pom.xml
+++ b/pom.xml
@@ -79,6 +79,7 @@