Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit 78bf38b

Browse files
author
Praful Makani
authored
docs(samples): add quick start (#143)
* docs(samples): add quick start * docs(samples): modify code
1 parent da26519 commit 78bf38b

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2020 Google LLC
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.bigqueryconnection;
18+
19+
// [START bigqueryconnection_quickstart]
20+
import com.google.cloud.bigquery.connection.v1.ListConnectionsRequest;
21+
import com.google.cloud.bigquery.connection.v1.LocationName;
22+
import com.google.cloud.bigqueryconnection.v1.ConnectionServiceClient;
23+
import java.io.IOException;
24+
25+
// Sample to demonstrates basic usage of the BigQuery connection API.
26+
public class QuickstartSample {
27+
28+
public static void main(String[] args) throws IOException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "MY_PROJECT_ID";
31+
String location = "MY_LOCATION";
32+
listConnections(projectId, location);
33+
}
34+
35+
public static void listConnections(String projectId, String location) throws IOException {
36+
try (ConnectionServiceClient connectionServiceClient = ConnectionServiceClient.create()) {
37+
LocationName parent = LocationName.of(projectId, location);
38+
int pageSize = 10;
39+
ListConnectionsRequest request =
40+
ListConnectionsRequest.newBuilder()
41+
.setParent(parent.toString())
42+
.setPageSize(pageSize)
43+
.build();
44+
ConnectionServiceClient.ListConnectionsPagedResponse response =
45+
connectionServiceClient.listConnections(request);
46+
47+
// Print the results.
48+
System.out.println("List of connections:");
49+
response
50+
.iterateAll()
51+
.forEach(connection -> System.out.println("Connection Name: " + connection.getName()));
52+
}
53+
}
54+
}
55+
// [END bigqueryconnection_quickstart]
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2020 Google LLC
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.bigqueryconnection;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.IOException;
24+
import java.io.PrintStream;
25+
import java.util.logging.Level;
26+
import java.util.logging.Logger;
27+
import org.junit.After;
28+
import org.junit.Before;
29+
import org.junit.BeforeClass;
30+
import org.junit.Test;
31+
32+
public class QuickstartSampleIT {
33+
34+
private static final Logger LOG = Logger.getLogger(QuickstartSampleIT.class.getName());
35+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
36+
37+
private ByteArrayOutputStream bout;
38+
private PrintStream out;
39+
private PrintStream originalPrintStream;
40+
41+
private static String requireEnvVar(String varName) {
42+
String value = System.getenv(varName);
43+
assertNotNull(
44+
"Environment variable " + varName + " is required to perform these tests.",
45+
System.getenv(varName));
46+
return value;
47+
}
48+
49+
@BeforeClass
50+
public static void checkRequirements() {
51+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
52+
}
53+
54+
@Before
55+
public void setUp() {
56+
bout = new ByteArrayOutputStream();
57+
out = new PrintStream(bout);
58+
originalPrintStream = System.out;
59+
System.setOut(out);
60+
}
61+
62+
@After
63+
public void tearDown() {
64+
// restores print statements in the original method
65+
System.out.flush();
66+
System.setOut(originalPrintStream);
67+
LOG.log(Level.INFO, bout.toString());
68+
}
69+
70+
@Test
71+
public void testQuickstart() throws IOException {
72+
QuickstartSample.listConnections(PROJECT_ID, "US");
73+
assertThat(bout.toString()).contains("List of connections:");
74+
}
75+
}

0 commit comments

Comments
 (0)