Skip to content

Commit 00cc6d6

Browse files
committed
BigQuery: Add explicit service account key auth sample.
1 parent a77fee7 commit 00cc6d6

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

bigquery/cloud-client/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
service_account.json
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 explicit_service_account]
37+
public static void explicit() throws IOException {
38+
// Load credentials from JSON key file.
39+
GoogleCredentials credentials;
40+
File credentialsPath = new File("service_account.json"); // TODO: update to your key path.
41+
try (FileInputStream serviceAccountStream = new FileInputStream(credentialsPath)) {
42+
credentials = ServiceAccountCredentials.fromStream(serviceAccountStream);
43+
}
44+
45+
// Instantiate a client.
46+
BigQuery bigquery =
47+
BigQueryOptions.newBuilder().setCredentials(credentials).build().getService();
48+
49+
// Use the client.
50+
for (Dataset dataset : bigquery.listDatasets().iterateAll()) {
51+
System.out.printf("%s%n", dataset.getDatasetId().getDataset());
52+
}
53+
}
54+
// [END explicit_service_account]
55+
56+
// [START default_credentials]
57+
public static void implicit() {
58+
// Instantiates a client
59+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
60+
61+
// Use the client.
62+
System.out.println("Datasets:");
63+
for (Dataset dataset : bigquery.listDatasets().iterateAll()) {
64+
System.out.printf("%s%n", dataset.getDatasetId().getDataset());
65+
}
66+
}
67+
// [END default_credentials]
68+
69+
public static void main(String... args) throws IOException {
70+
boolean validArgs = args.length == 1;
71+
String sample = "explicit";
72+
if (validArgs) {
73+
sample = args[0];
74+
if (!sample.equals("explicit") && !sample.equals("implicit")) {
75+
validArgs = false;
76+
}
77+
}
78+
79+
if (!validArgs) {
80+
System.err.println("Expected auth type argument: implict|explict");
81+
System.exit(1);
82+
}
83+
84+
if (sample.equals("implicit")) {
85+
implicit();
86+
} else {
87+
explicit();
88+
}
89+
}
90+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 static com.google.common.truth.Truth.assertThat;
20+
21+
import org.junit.After;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.junit.runners.JUnit4;
26+
27+
import java.io.ByteArrayOutputStream;
28+
import java.io.PrintStream;
29+
30+
/** Tests for auth samples. */
31+
@RunWith(JUnit4.class)
32+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
33+
public class AuthSnippetsIT {
34+
private ByteArrayOutputStream bout;
35+
private PrintStream out;
36+
37+
@Before
38+
public void setUp() {
39+
bout = new ByteArrayOutputStream();
40+
out = new PrintStream(bout);
41+
System.setOut(out);
42+
}
43+
44+
@After
45+
public void tearDown() {
46+
System.setOut(null);
47+
}
48+
49+
@Test
50+
public void testAuthSnippetsImplicit() throws Exception {
51+
AuthSnippets.main(new String[]{"implicit"});
52+
String got = bout.toString();
53+
assertThat(got).contains("Datasets:");
54+
}
55+
}

0 commit comments

Comments
 (0)