|
| 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