Skip to content

Commit 6f95631

Browse files
feat: new sample - Tables: Relax column (#89)
1 parent 66b225a commit 6f95631

File tree

3 files changed

+167
-1
lines changed

3 files changed

+167
-1
lines changed

samples/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
<groupId>com.google.cloud.samples</groupId>
2828
<artifactId>shared-configuration</artifactId>
2929
<version>1.0.11</version>
30-
<relativePath></relativePath>
3130
</parent>
3231

3332
<properties>
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 com.google.cloud.bigquery.BigQuery;
20+
import com.google.cloud.bigquery.BigQueryException;
21+
import com.google.cloud.bigquery.BigQueryOptions;
22+
import com.google.cloud.bigquery.Field;
23+
import com.google.cloud.bigquery.LegacySQLTypeName;
24+
import com.google.cloud.bigquery.Schema;
25+
import com.google.cloud.bigquery.StandardTableDefinition;
26+
import com.google.cloud.bigquery.Table;
27+
28+
// [START bigquery_relax_column]
29+
public class RelaxColumnMode {
30+
31+
public static void runRelaxColumnMode() {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String datasetName = "MY_DATASET_NAME";
34+
String tableId = "MY_TABLE_NAME";
35+
relaxColumnMode(datasetName, tableId);
36+
}
37+
38+
public static void relaxColumnMode(String datasetName, String tableId) {
39+
try {
40+
// Initialize client that will be used to send requests. This client only needs to be created
41+
// once, and can be reused for multiple requests.
42+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
43+
44+
Table table = bigquery.getTable(datasetName, tableId);
45+
46+
// Create new relaxed schema based on the existing table schema
47+
Schema relaxedSchema =
48+
Schema.of(
49+
// The only supported modification you can make to a column's mode is changing it from
50+
// REQUIRED to NULLABLE
51+
// Changing a column's mode from REQUIRED to NULLABLE is also called column relaxation
52+
// INFO: LegacySQLTypeName will be updated to StandardSQLTypeName in release 1.103.0
53+
Field.newBuilder("word", LegacySQLTypeName.STRING)
54+
.setMode(Field.Mode.NULLABLE)
55+
.build(),
56+
Field.newBuilder("word_count", LegacySQLTypeName.STRING)
57+
.setMode(Field.Mode.NULLABLE)
58+
.build(),
59+
Field.newBuilder("corpus", LegacySQLTypeName.STRING)
60+
.setMode(Field.Mode.NULLABLE)
61+
.build(),
62+
Field.newBuilder("corpus_date", LegacySQLTypeName.STRING)
63+
.setMode(Field.Mode.NULLABLE)
64+
.build());
65+
66+
// Update the table with the new schema
67+
Table updatedTable =
68+
table.toBuilder().setDefinition(StandardTableDefinition.of(relaxedSchema)).build();
69+
updatedTable.update();
70+
System.out.println("Table schema successfully relaxed.");
71+
} catch (BigQueryException e) {
72+
System.out.println("Table schema not relaxed \n" + e.toString());
73+
}
74+
}
75+
}
76+
// [END bigquery_relax_column]
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 com.google.cloud.bigquery.Field;
23+
import com.google.cloud.bigquery.Field.Mode;
24+
import com.google.cloud.bigquery.LegacySQLTypeName;
25+
import com.google.cloud.bigquery.Schema;
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.PrintStream;
28+
import java.util.UUID;
29+
import org.junit.After;
30+
import org.junit.Before;
31+
import org.junit.BeforeClass;
32+
import org.junit.Test;
33+
34+
public class RelaxColumnModeIT {
35+
private ByteArrayOutputStream bout;
36+
private PrintStream out;
37+
38+
private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME");
39+
40+
private static void requireEnvVar(String varName) {
41+
assertNotNull(
42+
"Environment variable " + varName + " is required to perform these tests.",
43+
System.getenv(varName));
44+
}
45+
46+
@BeforeClass
47+
public static void checkRequirements() {
48+
requireEnvVar("BIGQUERY_DATASET_NAME");
49+
}
50+
51+
@Before
52+
public void setUp() throws Exception {
53+
bout = new ByteArrayOutputStream();
54+
out = new PrintStream(bout);
55+
System.setOut(out);
56+
}
57+
58+
@After
59+
public void tearDown() {
60+
System.setOut(null);
61+
}
62+
63+
@Test
64+
public void testRelaxColumnMode() {
65+
// Create a new table with REQUIRED columns for each test to relax its column mode since this is
66+
// a one-way operation
67+
String generatedTableName =
68+
"gcloud_test_table_temp_" + UUID.randomUUID().toString().replace('-', '_');
69+
Schema originalSchema =
70+
Schema.of(
71+
// The only supported modification you can make to a column's mode is changing it from
72+
// REQUIRED to NULLABLE
73+
// Changing a column's mode from REQUIRED to NULLABLE is also called column relaxation
74+
// INFO: LegacySQLTypeName will be updated to StandardSQLTypeName in release 1.103.0
75+
Field.newBuilder("word", LegacySQLTypeName.STRING).setMode(Mode.REQUIRED).build(),
76+
Field.newBuilder("word_count", LegacySQLTypeName.STRING)
77+
.setMode(Field.Mode.REQUIRED)
78+
.build(),
79+
Field.newBuilder("corpus", LegacySQLTypeName.STRING)
80+
.setMode(Field.Mode.REQUIRED)
81+
.build(),
82+
Field.newBuilder("corpus_date", LegacySQLTypeName.STRING)
83+
.setMode(Field.Mode.REQUIRED)
84+
.build());
85+
CreateTable.createTable(BIGQUERY_DATASET_NAME, generatedTableName, originalSchema);
86+
87+
// Relax table column mode
88+
RelaxColumnMode.relaxColumnMode(BIGQUERY_DATASET_NAME, generatedTableName);
89+
assertThat(bout.toString()).contains("Table schema successfully relaxed.");
90+
}
91+
}

0 commit comments

Comments
 (0)