|
| 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] |
0 commit comments