|
| 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_add_empty_column] |
| 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.Field; |
| 24 | +import com.google.cloud.bigquery.FieldList; |
| 25 | +import com.google.cloud.bigquery.LegacySQLTypeName; |
| 26 | +import com.google.cloud.bigquery.Schema; |
| 27 | +import com.google.cloud.bigquery.StandardTableDefinition; |
| 28 | +import com.google.cloud.bigquery.Table; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +public class AddEmptyColumn { |
| 33 | + |
| 34 | + public static void runAddEmptyColumn() { |
| 35 | + // TODO(developer): Replace these variables before running the sample. |
| 36 | + String datasetName = "MY_DATASET_NAME"; |
| 37 | + String tableId = "MY_TABLE_NAME"; |
| 38 | + String newColumnName = "NEW_COLUMN_NAME"; |
| 39 | + addEmptyColumn(newColumnName, datasetName, tableId); |
| 40 | + } |
| 41 | + |
| 42 | + public static void addEmptyColumn(String newColumnName, String datasetName, String tableId) { |
| 43 | + try { |
| 44 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 45 | + // once, and can be reused for multiple requests. |
| 46 | + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); |
| 47 | + |
| 48 | + Table table = bigquery.getTable(datasetName, tableId); |
| 49 | + Schema schema = table.getDefinition().getSchema(); |
| 50 | + FieldList fields = schema.getFields(); |
| 51 | + |
| 52 | + // Create the new field/column |
| 53 | + Field newField = Field.of(newColumnName, LegacySQLTypeName.STRING); |
| 54 | + |
| 55 | + // Create a new schema adding the current fields, plus the new one |
| 56 | + List<Field> fieldList = new ArrayList<Field>(); |
| 57 | + fields.forEach(fieldList::add); |
| 58 | + fieldList.add(newField); |
| 59 | + Schema newSchema = Schema.of(fieldList); |
| 60 | + |
| 61 | + // Update the table with the new schema |
| 62 | + Table updatedTable = |
| 63 | + table.toBuilder().setDefinition(StandardTableDefinition.of(newSchema)).build(); |
| 64 | + updatedTable.update(); |
| 65 | + System.out.println("Empty column successfully added to table"); |
| 66 | + } catch (BigQueryException e) { |
| 67 | + System.out.println("Empty column was not added. \n" + e.toString()); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | +// [END bigquery_add_empty_column] |
0 commit comments