Skip to content

Commit 96d2c87

Browse files
authored
docs(samples): Added UpdateTableDescription example. (#360)
1 parent f327bbd commit 96d2c87

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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_update_table_description]
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.Table;
24+
25+
public class UpdateTableDescription {
26+
27+
public static void runUpdateTableDescription() {
28+
// TODO(developer): Replace these variables before running the sample.
29+
String datasetName = "MY_DATASET_NAME";
30+
String tableName = "MY_TABLE_NAME";
31+
String newDescription = "this is the new table description";
32+
updateTableDescription(datasetName, tableName, newDescription);
33+
}
34+
35+
public static void updateTableDescription(String datasetName, String tableName,
36+
String newDescription) {
37+
try {
38+
// Initialize client that will be used to send requests. This client only needs to be created
39+
// once, and can be reused for multiple requests.
40+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
41+
42+
Table table = bigquery.getTable(datasetName, tableName);
43+
bigquery.update(table.toBuilder().setDescription(newDescription).build());
44+
System.out.println("Table description updated successfully to " + newDescription);
45+
} catch (BigQueryException e) {
46+
System.out.println("Table description was not updated \n" + e.toString());
47+
}
48+
}
49+
}
50+
// [END bigquery_update_table_description]
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 UpdateTableDescriptionIT {
31+
private ByteArrayOutputStream bout;
32+
private PrintStream out;
33+
34+
private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME");
35+
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+
}
47+
48+
@Before
49+
public void setUp() throws Exception {
50+
bout = new ByteArrayOutputStream();
51+
out = new PrintStream(bout);
52+
System.setOut(out);
53+
}
54+
55+
@After
56+
public void tearDown() {
57+
System.setOut(null);
58+
}
59+
60+
@Test
61+
public void updateTableDescription() {
62+
// Create a table in order to modify its description
63+
String tableName = "MY_TABLE_NAME_" + UUID.randomUUID().toString().replace("-", "_");
64+
CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, null);
65+
String newDescription = "new description!";
66+
67+
// Modify table's description
68+
UpdateTableDescription.updateTableDescription(BIGQUERY_DATASET_NAME, tableName, newDescription);
69+
70+
assertThat(bout.toString())
71+
.contains("Table description updated successfully to " + newDescription);
72+
73+
// Clean up
74+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
75+
76+
}
77+
}

0 commit comments

Comments
 (0)