|
| 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] |
0 commit comments