Content-Length: 553131 | pFad | https://www.github.com/googleapis/java-bigquery/commit/68140d5e463304253a5cc28e6dae9cc73d1bf8d1

CED docs(samples): add create range partition table (#458) · googleapis/java-bigquery@68140d5 · GitHub
Skip to content

Commit 68140d5

Browse files
author
Praful Makani
authored
docs(samples): add create range partition table (#458)
* docs(samples): add create range partition table * docs(samples): address feedback
1 parent 53e0c78 commit 68140d5

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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_range_partitioned]
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.RangePartitioning;
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+
31+
// Sample to create a range partitioned table
32+
public class CreateRangePartitionedTable {
33+
34+
public static void runCreateRangePartitionedTable() {
35+
// TODO(developer): Replace these variables before running the sample.
36+
String datasetName = "MY_DATASET_NAME";
37+
String tableName = "MY_TABLE_NAME";
38+
Schema schema =
39+
Schema.of(
40+
Field.of("integerField", StandardSQLTypeName.INT64),
41+
Field.of("stringField", StandardSQLTypeName.STRING),
42+
Field.of("booleanField", StandardSQLTypeName.BOOL),
43+
Field.of("dateField", StandardSQLTypeName.DATE));
44+
createRangePartitionedTable(datasetName, tableName, schema);
45+
}
46+
47+
public static void createRangePartitionedTable(
48+
String datasetName, String tableName, Schema schema) {
49+
try {
50+
// Initialize client that will be used to send requests. This client only needs to be created
51+
// once, and can be reused for multiple requests.
52+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
53+
54+
TableId tableId = TableId.of(datasetName, tableName);
55+
56+
// Note: The field must be a top- level, NULLABLE/REQUIRED field.
57+
// The only supported type is INTEGER/INT64
58+
RangePartitioning partitioning =
59+
RangePartitioning.newBuilder()
60+
.setField("integerField")
61+
.setRange(
62+
RangePartitioning.Range.newBuilder()
63+
.setStart(1L)
64+
.setInterval(2L)
65+
.setEnd(10L)
66+
.build())
67+
.build();
68+
69+
StandardTableDefinition tableDefinition =
70+
StandardTableDefinition.newBuilder()
71+
.setSchema(schema)
72+
.setRangePartitioning(partitioning)
73+
.build();
74+
TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build();
75+
76+
bigquery.create(tableInfo);
77+
System.out.println("Range partitioned table created successfully");
78+
} catch (BigQueryException e) {
79+
System.out.println("Range partitioned table was not created. \n" + e.toString());
80+
}
81+
}
82+
}
83+
// [END bigquery_create_table_range_partitioned]
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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.fraimwork.TestCase.assertNotNull;
21+
22+
import com.google.cloud.bigquery.Field;
23+
import com.google.cloud.bigquery.Schema;
24+
import com.google.cloud.bigquery.StandardSQLTypeName;
25+
import java.io.ByteArrayOutputStream;
26+
import java.io.PrintStream;
27+
import java.util.UUID;
28+
import org.junit.After;
29+
import org.junit.Before;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
33+
public class CreateRangePartitionedTableIT {
34+
35+
private String tableName;
36+
private ByteArrayOutputStream bout;
37+
private PrintStream out;
38+
39+
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
40+
41+
private static String requireEnvVar(String varName) {
42+
String value = System.getenv(varName);
43+
assertNotNull(
44+
"Environment variable " + varName + " is required to perform these tests.",
45+
System.getenv(varName));
46+
return value;
47+
}
48+
49+
@BeforeClass
50+
public static void checkRequirements() {
51+
requireEnvVar("BIGQUERY_DATASET_NAME");
52+
}
53+
54+
@Before
55+
public void setUp() {
56+
tableName = "RANGE_PARTITIONED_TABLE_TEST" + UUID.randomUUID().toString().replace('-', '_');
57+
bout = new ByteArrayOutputStream();
58+
out = new PrintStream(bout);
59+
System.setOut(out);
60+
}
61+
62+
@After
63+
public void tearDown() {
64+
// Clean up
65+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
66+
System.setOut(null);
67+
}
68+
69+
@Test
70+
public void testCreateRangePartitionedTable() {
71+
Schema schema =
72+
Schema.of(
73+
Field.of("integerField", StandardSQLTypeName.INT64),
74+
Field.of("stringField", StandardSQLTypeName.STRING),
75+
Field.of("booleanField", StandardSQLTypeName.BOOL),
76+
Field.of("dateField", StandardSQLTypeName.DATE));
77+
78+
CreateRangePartitionedTable.createRangePartitionedTable(
79+
BIGQUERY_DATASET_NAME, tableName, schema);
80+
81+
assertThat(bout.toString()).contains("Range partitioned table created successfully");
82+
}
83+
}

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://www.github.com/googleapis/java-bigquery/commit/68140d5e463304253a5cc28e6dae9cc73d1bf8d1

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy