25
25
"""
26
26
27
27
import argparse
28
+ import time
29
+ import uuid
28
30
29
31
from gcloud import bigquery
32
+ import gcloud .bigquery .job
30
33
31
34
32
35
def list_projects ():
@@ -82,6 +85,32 @@ def list_tables(dataset_name, project=None):
82
85
print (table .name )
83
86
84
87
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
+
85
114
def list_rows (dataset_name , table_name , project = None ):
86
115
"""Prints rows in the given table.
87
116
@@ -126,6 +155,50 @@ def list_rows(dataset_name, table_name, project=None):
126
155
print (format_string .format (* row ))
127
156
128
157
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
+
129
202
def delete_table (dataset_name , table_name , project = None ):
130
203
"""Deletes a table in a given dataset.
131
204
@@ -155,11 +228,22 @@ def delete_table(dataset_name, table_name, project=None):
155
228
'list-tables' , help = list_tables .__doc__ )
156
229
list_tables_parser .add_argument ('dataset_name' )
157
230
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
+
158
236
list_rows_parser = subparsers .add_parser (
159
237
'list-rows' , help = list_rows .__doc__ )
160
238
list_rows_parser .add_argument ('dataset_name' )
161
239
list_rows_parser .add_argument ('table_name' )
162
240
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
+
163
247
delete_table_parser = subparsers .add_parser (
164
248
'delete-table' , help = delete_table .__doc__ )
165
249
delete_table_parser .add_argument ('dataset_name' )
@@ -171,7 +255,11 @@ def delete_table(dataset_name, table_name, project=None):
171
255
list_datasets (args .project )
172
256
elif args .command == 'list-tables' :
173
257
list_tables (args .dataset_name , args .project )
258
+ elif args .command == 'create-table' :
259
+ create_table (args .dataset_name , args .table_name , args .project )
174
260
elif args .command == 'list-rows' :
175
261
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 )
176
264
elif args .command == 'delete-table' :
177
265
delete_table (args .dataset_name , args .table_name , args .project )
0 commit comments