Skip to content

Commit 99c324c

Browse files
author
Praful Makani
authored
docs(samples): add create and delete routine (#493)
1 parent 2a10616 commit 99c324c

File tree

4 files changed

+266
-0
lines changed

4 files changed

+266
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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_create_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.RoutineArgument;
24+
import com.google.cloud.bigquery.RoutineId;
25+
import com.google.cloud.bigquery.RoutineInfo;
26+
import com.google.cloud.bigquery.StandardSQLDataType;
27+
import com.google.cloud.bigquery.StandardSQLTypeName;
28+
import com.google.common.collect.ImmutableList;
29+
30+
// Sample to create a routine
31+
public class CreateRoutine {
32+
33+
public static void runCreateRoutine() {
34+
// TODO(developer): Replace these variables before running the sample.
35+
String datasetName = "MY_DATASET_NAME";
36+
String routineName = "MY_ROUTINE_NAME";
37+
createRoutine(datasetName, routineName);
38+
}
39+
40+
public static void createRoutine(String datasetName, String routineName) {
41+
try {
42+
// Initialize client that will be used to send requests. This client only needs to be created
43+
// once, and can be reused for multiple requests.
44+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
45+
46+
RoutineId routineId = RoutineId.of(datasetName, routineName);
47+
48+
RoutineInfo routineInfo =
49+
RoutineInfo.newBuilder(routineId)
50+
.setRoutineType("SCALAR_FUNCTION")
51+
.setLanguage("SQL")
52+
.setBody("x * 3")
53+
.setArguments(
54+
ImmutableList.of(
55+
RoutineArgument.newBuilder()
56+
.setName("x")
57+
.setDataType(
58+
StandardSQLDataType.newBuilder(StandardSQLTypeName.INT64).build())
59+
.build()))
60+
.build();
61+
bigquery.create(routineInfo);
62+
System.out.println("Routine created successfully");
63+
} catch (BigQueryException e) {
64+
System.out.println("Routine was not created. \n" + e.toString());
65+
}
66+
}
67+
}
68+
// [END bigquery_create_routine]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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_delete_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.RoutineId;
24+
25+
// Sample to delete a routine
26+
public class DeleteRoutine {
27+
28+
public static void runDeleteRoutine() {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String datasetName = "MY_DATASET_NAME";
31+
String routineName = "MY_ROUTINE_NAME";
32+
deleteRoutine(datasetName, routineName);
33+
}
34+
35+
public static void deleteRoutine(String datasetName, String routineName) {
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+
boolean success = bigquery.delete(RoutineId.of(datasetName, routineName));
41+
if (success) {
42+
System.out.println("Routine deleted successfully");
43+
} else {
44+
System.out.println("Routine was not found");
45+
}
46+
} catch (BigQueryException e) {
47+
System.out.println("Routine was not deleted. \n" + e.toString());
48+
}
49+
}
50+
}
51+
// [END bigquery_delete_routine]
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 CreateRoutineIT {
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+
routineName = "MY_ROUTINE_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8);
54+
bout = new ByteArrayOutputStream();
55+
out = new PrintStream(bout);
56+
System.setOut(out);
57+
}
58+
59+
@After
60+
public void tearDown() {
61+
// Clean up
62+
DeleteRoutine.deleteRoutine(BIGQUERY_DATASET_NAME, routineName);
63+
System.setOut(null);
64+
}
65+
66+
@Test
67+
public void testCreateRoutine() {
68+
CreateRoutine.createRoutine(BIGQUERY_DATASET_NAME, routineName);
69+
assertThat(bout.toString()).contains("Routine created successfully");
70+
}
71+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 DeleteRoutineIT {
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+
System.setOut(null);
69+
}
70+
71+
@Test
72+
public void testDeleteRoutine() {
73+
DeleteRoutine.deleteRoutine(BIGQUERY_DATASET_NAME, routineName);
74+
assertThat(bout.toString()).contains("Routine deleted successfully");
75+
}
76+
}

0 commit comments

Comments
 (0)