Skip to content
This repository was archived by the owner on Mar 23, 2026. It is now read-only.

Commit dbe4d30

Browse files
author
Praful Makani
authored
docs(samples): add get routine (#504)
1 parent 3c1884b commit dbe4d30

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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_get_routine]
20+
import com.google.cloud.bigquery.BigQuery;
21+
import com.google.cloud.bigquery.BigQueryException;
22+
import com.google.cloud.bigquery.BigQueryOptions;
23+
import com.google.cloud.bigquery.Routine;
24+
import com.google.cloud.bigquery.RoutineId;
25+
26+
// Sample to get a routine
27+
public class GetRoutine {
28+
29+
public static void runGetRoutine() {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String datasetName = "MY_DATASET_NAME";
32+
String routineName = "MY_ROUTINE_NAME";
33+
getRoutine(datasetName, routineName);
34+
}
35+
36+
public static void getRoutine(String datasetName, String routineName) {
37+
try {
38+
// Initialize client that will be used to send requests. This client only needs to be created
39+
// once, and can be reused for multiple requests.
40+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
41+
42+
RoutineId routineId = RoutineId.of(datasetName, routineName);
43+
Routine routine = bigquery.getRoutine(routineId);
44+
System.out.println("Routine retrieved successfully" + routine.getDescription());
45+
} catch (BigQueryException e) {
46+
System.out.println("Routine not retrieved. \n" + e.toString());
47+
}
48+
}
49+
}
50+
// [END bigquery_get_routine]
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.framework.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 GetRoutineIT {
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 temporary routine
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 testGetRoutine() {
75+
GetRoutine.getRoutine(BIGQUERY_DATASET_NAME, routineName);
76+
assertThat(bout.toString()).contains("Routine retrieved successfully");
77+
}
78+
}

0 commit comments

Comments
 (0)