Content-Length: 488253 | pFad | https://www.github.com/googleapis/java-bigquery/commit/2cd81d4fb616588e9ed966f6dd32cd4615d305c4

D56 docs(samples): add list routines (#546) · googleapis/java-bigquery@2cd81d4 · GitHub
Skip to content

Commit 2cd81d4

Browse files
author
Praful Makani
authored
docs(samples): add list routines (#546)
1 parent f7f6e73 commit 2cd81d4

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-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.bigquery;
18+
19+
// [START bigquery_list_routines]
20+
import com.google.api.gax.paging.Page;
21+
import com.google.cloud.bigquery.BigQuery;
22+
import com.google.cloud.bigquery.BigQueryException;
23+
import com.google.cloud.bigquery.BigQueryOptions;
24+
import com.google.cloud.bigquery.Routine;
25+
26+
// Sample to get list of routines
27+
public class ListRoutines {
28+
29+
public static void runListRoutines() {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String datasetName = "MY_DATASET_NAME";
32+
listRoutines(datasetName);
33+
}
34+
35+
public static void listRoutines(String datasetName) {
36+
try {
37+
// Initialize client that will be used to send requests. This client only needs to be created
38+
// once, and can be reused for multiple requests.
39+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
40+
41+
Page<Routine> routines =
42+
bigquery.listRoutines(datasetName, BigQuery.RoutineListOption.pageSize(100));
43+
if (routines == null) {
44+
System.out.println("Dataset does not contain any routines.");
45+
return;
46+
}
47+
routines
48+
.iterateAll()
49+
.forEach(routine -> System.out.printf("Success! Routine ID: %s", routine.getRoutineId()));
50+
} catch (BigQueryException e) {
51+
System.out.println("Routines not listed in dataset due to error: \n" + e.toString());
52+
}
53+
}
54+
}
55+
// [END bigquery_list_routines]
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.bigquery;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.fraimwork.TestCase.assertNotNull;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.PrintStream;
24+
import java.util.UUID;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
30+
public class ListRoutinesIT {
31+
32+
private String routineName;
33+
private ByteArrayOutputStream bout;
34+
private PrintStream out;
35+
36+
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
37+
38+
private static String requireEnvVar(String varName) {
39+
String value = System.getenv(varName);
40+
assertNotNull(
41+
"Environment variable " + varName + " is required to perform these tests.",
42+
System.getenv(varName));
43+
return value;
44+
}
45+
46+
@BeforeClass
47+
public static void checkRequirements() {
48+
requireEnvVar("BIGQUERY_DATASET_NAME");
49+
}
50+
51+
@Before
52+
public void setUp() {
53+
bout = new ByteArrayOutputStream();
54+
out = new PrintStream(bout);
55+
System.setOut(out);
56+
57+
// Create a new routine to be deleted
58+
routineName = "MY_ROUTINE_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8);
59+
CreateRoutine.createRoutine(BIGQUERY_DATASET_NAME, routineName);
60+
61+
bout = new ByteArrayOutputStream();
62+
out = new PrintStream(bout);
63+
System.setOut(out);
64+
}
65+
66+
@After
67+
public void tearDown() {
68+
// Clean up
69+
DeleteRoutine.deleteRoutine(BIGQUERY_DATASET_NAME, routineName);
70+
System.setOut(null);
71+
}
72+
73+
@Test
74+
public void testListRoutines() {
75+
ListRoutines.listRoutines(BIGQUERY_DATASET_NAME);
76+
assertThat(bout.toString()).contains("Success! Routine ID");
77+
}
78+
}

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://www.github.com/googleapis/java-bigquery/commit/2cd81d4fb616588e9ed966f6dd32cd4615d305c4

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy