Skip to content

Commit 93f6cc2

Browse files
feat: add new sample - Table: Create clustered table (#120)
* feat: add new sample - Table: Create clustered table * nit update
1 parent 833b953 commit 93f6cc2

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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_table_clustered]
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.Clustering;
24+
import com.google.cloud.bigquery.Field;
25+
import com.google.cloud.bigquery.Schema;
26+
import com.google.cloud.bigquery.StandardSQLTypeName;
27+
import com.google.cloud.bigquery.StandardTableDefinition;
28+
import com.google.cloud.bigquery.TableId;
29+
import com.google.cloud.bigquery.TableInfo;
30+
import com.google.cloud.bigquery.TimePartitioning;
31+
import com.google.common.collect.ImmutableList;
32+
33+
public class CreateClusteredTable {
34+
public static void runCreateClusteredTable() {
35+
// TODO(developer): Replace these variables before running the sample.
36+
String datasetName = "MY_DATASET_NAME";
37+
String tableName = "MY_TABLE_NAME";
38+
createClusteredTable(datasetName, tableName);
39+
}
40+
41+
public static void createClusteredTable(String datasetName, String tableName) {
42+
try {
43+
// Initialize client that will be used to send requests. This client only needs to be created
44+
// once, and can be reused for multiple requests.
45+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
46+
47+
TableId tableId = TableId.of(datasetName, tableName);
48+
49+
TimePartitioning partitioning = TimePartitioning.of(TimePartitioning.Type.DAY);
50+
51+
Schema schema =
52+
Schema.of(
53+
Field.of("name", StandardSQLTypeName.STRING),
54+
Field.of("post_abbr", StandardSQLTypeName.STRING),
55+
Field.of("date", StandardSQLTypeName.DATE));
56+
57+
Clustering clustering =
58+
Clustering.newBuilder().setFields(ImmutableList.of("name", "post_abbr")).build();
59+
60+
StandardTableDefinition tableDefinition =
61+
StandardTableDefinition.newBuilder()
62+
.setSchema(schema)
63+
.setTimePartitioning(partitioning)
64+
.setClustering(clustering)
65+
.build();
66+
TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build();
67+
68+
bigquery.create(tableInfo);
69+
System.out.println("Clustered table created successfully");
70+
} catch (BigQueryException e) {
71+
System.out.println("Clustered table was not created. \n" + e.toString());
72+
}
73+
}
74+
}
75+
// [END bigquery_create_table_clustered]
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.BeforeClass;
27+
import org.junit.Test;
28+
29+
public class CreateClusteredTableIT {
30+
private ByteArrayOutputStream bout;
31+
private PrintStream out;
32+
33+
private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME");
34+
35+
private static void requireEnvVar(String varName) {
36+
assertNotNull(
37+
"Environment variable " + varName + " is required to perform these tests.",
38+
System.getenv(varName));
39+
}
40+
41+
@BeforeClass
42+
public static void checkRequirements() {
43+
requireEnvVar("BIGQUERY_DATASET_NAME");
44+
}
45+
46+
@Before
47+
public void setUp() {
48+
bout = new ByteArrayOutputStream();
49+
out = new PrintStream(bout);
50+
System.setOut(out);
51+
}
52+
53+
@After
54+
public void tearDown() {
55+
System.setOut(null);
56+
}
57+
58+
@Test
59+
public void createClusteredTable() {
60+
String tableName = "MY_CLUSTERED_TABLE";
61+
62+
CreateClusteredTable.createClusteredTable(BIGQUERY_DATASET_NAME, tableName);
63+
64+
assertThat(bout.toString()).contains("Clustered table created successfully");
65+
66+
// Clean up
67+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
68+
}
69+
}

0 commit comments

Comments
 (0)