|
| 1 | +# Copyright 2019 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +def create_table_range_partitioned(client, table_id): |
| 17 | + |
| 18 | + # [START bigquery_create_table_range_partitioned] |
| 19 | + from google.cloud import bigquery |
| 20 | + |
| 21 | + # TODO(developer): Construct a BigQuery client object. |
| 22 | + # client = bigquery.Client() |
| 23 | + |
| 24 | + # TODO(developer): Set table_id to the ID of the table to create. |
| 25 | + # table_id = "your-project.your_dataset.your_table_name" |
| 26 | + |
| 27 | + schema = [ |
| 28 | + bigquery.SchemaField("full_name", "STRING"), |
| 29 | + bigquery.SchemaField("city", "STRING"), |
| 30 | + bigquery.SchemaField("zipcode", "INTEGER"), |
| 31 | + ] |
| 32 | + |
| 33 | + table = bigquery.Table(table_id, schema=schema) |
| 34 | + table.range_partitioning = bigquery.RangePartitioning( |
| 35 | + # To use integer range partitioning, select a top-level REQUIRED / |
| 36 | + # NULLABLE column with INTEGER / INT64 data type. |
| 37 | + field="zipcode", |
| 38 | + range_=bigquery.PartitionRange(start=0, end=100000, interval=10), |
| 39 | + ) |
| 40 | + table = client.create_table(table) # Make an API request. |
| 41 | + print( |
| 42 | + "Created table {}.{}.{}".format(table.project, table.dataset_id, table.table_id) |
| 43 | + ) |
| 44 | + # [END bigquery_create_table_range_partitioned] |
| 45 | + return table |
0 commit comments