Skip to content

Commit b8e4c18

Browse files
feat: new sample - Tables: Add empty column (#85)
* feat: new sample - Tables: Add empty column * format code * update base on comments * update base on comments
1 parent 7a83dab commit b8e4c18

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed
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+
// [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]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 AddEmptyColumnIT {
31+
private ByteArrayOutputStream bout;
32+
private PrintStream out;
33+
34+
private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME");
35+
private static final String BIGQUERY_TEST_TABLE = System.getenv("BIGQUERY_TEST_TABLE");
36+
37+
private static void requireEnvVar(String varName) {
38+
assertNotNull(
39+
"Environment variable " + varName + " is required to perform these tests.",
40+
System.getenv(varName));
41+
}
42+
43+
@BeforeClass
44+
public static void checkRequirements() {
45+
requireEnvVar("BIGQUERY_DATASET_NAME");
46+
requireEnvVar("BIGQUERY_TEST_TABLE");
47+
}
48+
49+
@Before
50+
public void setUp() throws Exception {
51+
bout = new ByteArrayOutputStream();
52+
out = new PrintStream(bout);
53+
System.setOut(out);
54+
}
55+
56+
@After
57+
public void tearDown() {
58+
System.setOut(null);
59+
}
60+
61+
@Test
62+
public void addEmptyColumn() {
63+
String randomColumnName = "new_" + UUID.randomUUID().toString().replace('-', '_');
64+
AddEmptyColumn.addEmptyColumn(randomColumnName, BIGQUERY_DATASET_NAME, BIGQUERY_TEST_TABLE);
65+
assertThat(bout.toString()).contains("Empty column successfully added to table");
66+
}
67+
}

0 commit comments

Comments
 (0)