Skip to content

Commit 8e4d772

Browse files
author
Jon Wayne Parrott
authored
Add bigquery create and copy table examples (#514)
* Add bigquery create table sample * Add copy table sample * Fix test table ids
1 parent 4c7741d commit 8e4d772

File tree

3 files changed

+126
-3
lines changed

3 files changed

+126
-3
lines changed

bigquery/cloud-client/export_data_to_gcs_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
DATASET_ID = 'test_dataset'
18-
TABLE_ID = 'test_import_table'
18+
TABLE_ID = 'test_table'
1919

2020

2121
def test_export_data_to_gcs(cloud_config, capsys):

bigquery/cloud-client/snippets.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525
"""
2626

2727
import argparse
28+
import time
29+
import uuid
2830

2931
from gcloud import bigquery
32+
import gcloud.bigquery.job
3033

3134

3235
def list_projects():
@@ -82,6 +85,32 @@ def list_tables(dataset_name, project=None):
8285
print(table.name)
8386

8487

88+
def create_table(dataset_name, table_name, project=None):
89+
"""Creates a simple table in the given dataset.
90+
91+
If no project is specified, then the currently active project is used.
92+
"""
93+
bigquery_client = bigquery.Client(project=project)
94+
dataset = bigquery_client.dataset(dataset_name)
95+
96+
if not dataset.exists():
97+
print('Dataset {} does not exist.'.format(dataset_name))
98+
return
99+
100+
table = dataset.table(table_name)
101+
102+
# Set the table schema
103+
table.schema = (
104+
bigquery.SchemaField('Name', 'STRING'),
105+
bigquery.SchemaField('Age', 'INTEGER'),
106+
bigquery.SchemaField('Weight', 'FLOAT'),
107+
)
108+
109+
table.create()
110+
111+
print('Created table {} in dataset {}.'.format(table_name, dataset_name))
112+
113+
85114
def list_rows(dataset_name, table_name, project=None):
86115
"""Prints rows in the given table.
87116
@@ -126,6 +155,50 @@ def list_rows(dataset_name, table_name, project=None):
126155
print(format_string.format(*row))
127156

128157

158+
def copy_table(dataset_name, table_name, new_table_name, project=None):
159+
"""Copies a table.
160+
161+
If no project is specified, then the currently active project is used.
162+
"""
163+
bigquery_client = bigquery.Client(project=project)
164+
dataset = bigquery_client.dataset(dataset_name)
165+
table = dataset.table(table_name)
166+
167+
# This sample shows the destination table in the same dataset and project,
168+
# however, it's possible to copy across datasets and projects. You can
169+
# also copy muliple source tables into a single destination table by
170+
# providing addtional arguments to `copy_table`.
171+
destination_table = dataset.table(new_table_name)
172+
173+
# Create a job to copy the table to the destination table.
174+
job_id = str(uuid.uuid4())
175+
job = bigquery_client.copy_table(
176+
job_id, destination_table, table)
177+
178+
# Create the table if it doesn't exist.
179+
job.create_disposition = (
180+
gcloud.bigquery.job.CreateDisposition.CREATE_IF_NEEDED)
181+
182+
# Start the job.
183+
job.begin()
184+
185+
# Wait for the the job to finish.
186+
print('Waiting for job to finish...')
187+
wait_for_job(job)
188+
189+
print('Table {} copied to {}.'.format(table_name, new_table_name))
190+
191+
192+
def wait_for_job(job):
193+
while True:
194+
job.reload() # Refreshes the state via a GET request.
195+
if job.state == 'DONE':
196+
if job.error_result:
197+
raise RuntimeError(job.error_result)
198+
return
199+
time.sleep(1)
200+
201+
129202
def delete_table(dataset_name, table_name, project=None):
130203
"""Deletes a table in a given dataset.
131204
@@ -155,11 +228,22 @@ def delete_table(dataset_name, table_name, project=None):
155228
'list-tables', help=list_tables.__doc__)
156229
list_tables_parser.add_argument('dataset_name')
157230

231+
create_table_parser = subparsers.add_parser(
232+
'create-table', help=create_table.__doc__)
233+
create_table_parser.add_argument('dataset_name')
234+
create_table_parser.add_argument('table_name')
235+
158236
list_rows_parser = subparsers.add_parser(
159237
'list-rows', help=list_rows.__doc__)
160238
list_rows_parser.add_argument('dataset_name')
161239
list_rows_parser.add_argument('table_name')
162240

241+
copy_table_parser = subparsers.add_parser(
242+
'copy-table', help=copy_table.__doc__)
243+
copy_table_parser.add_argument('dataset_name')
244+
copy_table_parser.add_argument('table_name')
245+
copy_table_parser.add_argument('new_table_name')
246+
163247
delete_table_parser = subparsers.add_parser(
164248
'delete-table', help=delete_table.__doc__)
165249
delete_table_parser.add_argument('dataset_name')
@@ -171,7 +255,11 @@ def delete_table(dataset_name, table_name, project=None):
171255
list_datasets(args.project)
172256
elif args.command == 'list-tables':
173257
list_tables(args.dataset_name, args.project)
258+
elif args.command == 'create-table':
259+
create_table(args.dataset_name, args.table_name, args.project)
174260
elif args.command == 'list-rows':
175261
list_rows(args.dataset_name, args.table_name, args.project)
262+
elif args.command == 'copy-table':
263+
copy_table(args.dataset_name, args.table_name, args.new_table_name)
176264
elif args.command == 'delete-table':
177265
delete_table(args.dataset_name, args.table_name, args.project)

bigquery/cloud-client/snippets_test.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020

2121
DATASET_ID = 'test_dataset'
22-
TABLE_ID = 'test_import_table'
22+
TABLE_ID = 'test_table'
2323

2424

2525
@pytest.mark.xfail(
@@ -62,7 +62,42 @@ def test_list_rows(capsys):
6262
assert 'Age' in out
6363

6464

65-
def test_delete_table(capsys):
65+
@pytest.fixture
66+
def temporary_table():
67+
"""Fixture that returns a factory for tables that do not yet exist and
68+
will be automatically deleted after the test."""
69+
bigquery_client = bigquery.Client()
70+
dataset = bigquery_client.dataset(DATASET_ID)
71+
tables = []
72+
73+
def factory(table_name):
74+
new_table = dataset.table(table_name)
75+
if new_table.exists():
76+
new_table.delete()
77+
tables.append(new_table)
78+
return new_table
79+
80+
yield factory
81+
82+
for table in tables:
83+
if table.exists():
84+
table.delete()
85+
86+
87+
def test_create_table(temporary_table):
88+
new_table = temporary_table('test_create_table')
89+
snippets.create_table(DATASET_ID, new_table.name)
90+
assert new_table.exists()
91+
92+
93+
@pytest.mark.slow
94+
def test_copy_table(temporary_table):
95+
new_table = temporary_table('test_copy_table')
96+
snippets.copy_table(DATASET_ID, TABLE_ID, new_table.name)
97+
assert new_table.exists()
98+
99+
100+
def test_delete_table():
66101
# Create a table to delete
67102
bigquery_client = bigquery.Client()
68103
dataset = bigquery_client.dataset(DATASET_ID)

0 commit comments

Comments
 (0)