|
| 1 | +/* |
| 2 | + Copyright 2017, 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.auth.oauth2.GoogleCredentials; |
| 20 | +import com.google.auth.oauth2.ServiceAccountCredentials; |
| 21 | +import com.google.cloud.bigquery.BigQuery; |
| 22 | +import com.google.cloud.bigquery.BigQueryOptions; |
| 23 | +import com.google.cloud.bigquery.Dataset; |
| 24 | + |
| 25 | +import java.io.File; |
| 26 | +import java.io.FileInputStream; |
| 27 | +import java.io.IOException; |
| 28 | + |
| 29 | +/** |
| 30 | + * Examples for authenticating to Google BigQuery. |
| 31 | + * |
| 32 | + * <p>See: https://cloud.google.com/bigquery/authentication |
| 33 | + */ |
| 34 | +public class AuthSnippets { |
| 35 | + |
| 36 | + // [START default_credentials] |
| 37 | + public static void implicit() { |
| 38 | + // Instantiate a client. If you don't specify credentials when constructing a client, the |
| 39 | + // client library will look for credentials in the environment, such as the |
| 40 | + // GOOGLE_APPLICATION_CREDENTIALS environment variable. |
| 41 | + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); |
| 42 | + |
| 43 | + // Use the client. |
| 44 | + System.out.println("Datasets:"); |
| 45 | + for (Dataset dataset : bigquery.listDatasets().iterateAll()) { |
| 46 | + System.out.printf("%s%n", dataset.getDatasetId().getDataset()); |
| 47 | + } |
| 48 | + } |
| 49 | + // [END default_credentials] |
| 50 | + |
| 51 | + // [START explicit_service_account] |
| 52 | + public static void explicit() throws IOException { |
| 53 | + // Load credentials from JSON key file. If you can't set the GOOGLE_APPLICATION_CREDENTIALS |
| 54 | + // environment variable, you can explicitly load the credentials file to construct the |
| 55 | + // credentials. |
| 56 | + GoogleCredentials credentials; |
| 57 | + File credentialsPath = new File("service_account.json"); // TODO: update to your key path. |
| 58 | + try (FileInputStream serviceAccountStream = new FileInputStream(credentialsPath)) { |
| 59 | + credentials = ServiceAccountCredentials.fromStream(serviceAccountStream); |
| 60 | + } |
| 61 | + |
| 62 | + // Instantiate a client. |
| 63 | + BigQuery bigquery = |
| 64 | + BigQueryOptions.newBuilder().setCredentials(credentials).build().getService(); |
| 65 | + |
| 66 | + // Use the client. |
| 67 | + System.out.println("Datasets:"); |
| 68 | + for (Dataset dataset : bigquery.listDatasets().iterateAll()) { |
| 69 | + System.out.printf("%s%n", dataset.getDatasetId().getDataset()); |
| 70 | + } |
| 71 | + } |
| 72 | + // [END explicit_service_account] |
| 73 | + |
| 74 | + public static void main(String... args) throws IOException { |
| 75 | + boolean validArgs = args.length == 1; |
| 76 | + String sample = "explicit"; |
| 77 | + if (validArgs) { |
| 78 | + sample = args[0]; |
| 79 | + if (!sample.equals("explicit") && !sample.equals("implicit")) { |
| 80 | + validArgs = false; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if (!validArgs) { |
| 85 | + System.err.println("Expected auth type argument: implict|explict"); |
| 86 | + System.exit(1); |
| 87 | + } |
| 88 | + |
| 89 | + if (sample.equals("implicit")) { |
| 90 | + implicit(); |
| 91 | + } else { |
| 92 | + explicit(); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments