2525"""
2626
2727import argparse
28+ import time
29+ import uuid
2830
2931from gcloud import bigquery
32+ import gcloud .bigquery .job
3033
3134
3235def 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+
85114def 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+
129202def 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 )
0 commit comments