Skip to content

Commit f955408

Browse files
author
Praful Makani
authored
docs(samples): add create routine ddl (#495)
1 parent 053ca07 commit f955408

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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_ddl]
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.Job;
24+
import com.google.cloud.bigquery.JobInfo;
25+
import com.google.cloud.bigquery.QueryJobConfiguration;
26+
27+
// Sample to create a routine using DDL
28+
public class CreateRoutineDDL {
29+
30+
public static void runCreateRoutineDDL() {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String projectId = "MY_PROJECT_ID";
33+
String datasetId = "MY_DATASET_ID";
34+
String routineId = "MY_ROUTINE_ID";
35+
String sql =
36+
"CREATE FUNCTION "
37+
+ "`"
38+
+ projectId
39+
+ "."
40+
+ datasetId
41+
+ "."
42+
+ routineId
43+
+ "`"
44+
+ "( arr ARRAY<STRUCT<name STRING, val INT64>>) AS ( (SELECT SUM(IF(elem.name = \"foo\",elem.val,null)) FROM UNNEST(arr) AS elem))";
45+
createRoutineDDL(sql);
46+
}
47+
48+
public static void createRoutineDDL(String sql) {
49+
try {
50+
// Initialize client that will be used to send requests. This client only needs to be created
51+
// once, and can be reused for multiple requests.
52+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
53+
54+
QueryJobConfiguration config = QueryJobConfiguration.newBuilder(sql).build();
55+
56+
// create a routine using query and it will wait to complete job.
57+
Job job = bigquery.create(JobInfo.of(config));
58+
job = job.waitFor();
59+
if (job.isDone()) {
60+
System.out.println("Routine created successfully");
61+
} else {
62+
System.out.println("Routine was not created");
63+
}
64+
} catch (BigQueryException | InterruptedException e) {
65+
System.out.println("Routine was not created. \n" + e.toString());
66+
}
67+
}
68+
}
69+
// [END bigquery_create_routine_ddl]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.example.bigquery;
2+
3+
import static com.google.common.truth.Truth.assertThat;
4+
import static junit.framework.TestCase.assertNotNull;
5+
6+
import java.io.ByteArrayOutputStream;
7+
import java.io.PrintStream;
8+
import java.util.UUID;
9+
import org.junit.After;
10+
import org.junit.Before;
11+
import org.junit.BeforeClass;
12+
import org.junit.Test;
13+
14+
public class CreateRoutineDDLIT {
15+
16+
private String routineName;
17+
private ByteArrayOutputStream bout;
18+
private PrintStream out;
19+
20+
private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT");
21+
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
22+
23+
private static String requireEnvVar(String varName) {
24+
String value = System.getenv(varName);
25+
assertNotNull(
26+
"Environment variable " + varName + " is required to perform these tests.",
27+
System.getenv(varName));
28+
return value;
29+
}
30+
31+
@BeforeClass
32+
public static void checkRequirements() {
33+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
34+
requireEnvVar("BIGQUERY_DATASET_NAME");
35+
}
36+
37+
@Before
38+
public void setUp() {
39+
routineName = "MY_ROUTINE_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8);
40+
bout = new ByteArrayOutputStream();
41+
out = new PrintStream(bout);
42+
System.setOut(out);
43+
}
44+
45+
@After
46+
public void tearDown() {
47+
// Clean up
48+
DeleteRoutine.deleteRoutine(BIGQUERY_DATASET_NAME, routineName);
49+
System.setOut(null);
50+
}
51+
52+
@Test
53+
public void testCreateRoutineDDL() {
54+
String sql =
55+
"CREATE FUNCTION "
56+
+ "`"
57+
+ PROJECT_ID
58+
+ "."
59+
+ BIGQUERY_DATASET_NAME
60+
+ "."
61+
+ routineName
62+
+ "`"
63+
+ "( arr ARRAY<STRUCT<name STRING, val INT64>>) AS ( (SELECT SUM(IF(elem.name = \"foo\",elem.val,null)) FROM UNNEST(arr) AS elem))";
64+
CreateRoutineDDL.createRoutineDDL(sql);
65+
assertThat(bout.toString()).contains("Routine created successfully");
66+
}
67+
}

0 commit comments

Comments
 (0)