33
33
34
34
35
35
def list_projects ():
36
- raise NotImplementedError (
37
- 'https://github.com/GoogleCloudPlatform/gcloud-python/issues/2143' )
36
+ bigquery_client = bigquery .Client ()
37
+
38
+ projects = []
39
+ page_token = None
40
+
41
+ while True :
42
+ results , page_token = bigquery_client .list_projects (
43
+ page_token = page_token )
44
+ projects .extend (results )
45
+
46
+ if not page_token :
47
+ break
48
+
49
+ for project in projects :
50
+ print (project .project_id )
38
51
39
52
40
53
def list_datasets (project = None ):
41
54
"""Lists all datasets in a given project.
42
55
43
- If no project is specified, then the currently active project is used
56
+ If no project is specified, then the currently active project is used.
44
57
"""
45
58
bigquery_client = bigquery .Client (project = project )
46
59
@@ -59,6 +72,20 @@ def list_datasets(project=None):
59
72
print (dataset .name )
60
73
61
74
75
+ def create_dataset (dataset_name , project = None ):
76
+ """Craetes a dataset in a given project.
77
+
78
+ If no project is specified, then the currently active project is used.
79
+ """
80
+ bigquery_client = bigquery .Client (project = project )
81
+
82
+ dataset = bigquery_client .dataset (dataset_name )
83
+
84
+ dataset .create ()
85
+
86
+ print ('Created dataset {}.' .format (dataset_name ))
87
+
88
+
62
89
def list_tables (dataset_name , project = None ):
63
90
"""Lists all of the tables in a given dataset.
64
91
@@ -221,9 +248,16 @@ def delete_table(dataset_name, table_name, project=None):
221
248
222
249
subparsers = parser .add_subparsers (dest = 'command' )
223
250
251
+ list_projects_parser = subparsers .add_parser (
252
+ 'list-projects' , help = list_projects .__doc__ )
253
+
224
254
list_datasets_parser = subparsers .add_parser (
225
255
'list-datasets' , help = list_datasets .__doc__ )
226
256
257
+ create_dataset_parser = subparsers .add_parser (
258
+ 'list-datasets' , help = list_datasets .__doc__ )
259
+ create_dataset_parser .add_argument ('dataset_name' )
260
+
227
261
list_tables_parser = subparsers .add_parser (
228
262
'list-tables' , help = list_tables .__doc__ )
229
263
list_tables_parser .add_argument ('dataset_name' )
@@ -251,8 +285,12 @@ def delete_table(dataset_name, table_name, project=None):
251
285
252
286
args = parser .parse_args ()
253
287
254
- if args .command == 'list-datasets' :
288
+ if args .command == 'list-projects' :
289
+ list_projects ()
290
+ elif args .command == 'list-datasets' :
255
291
list_datasets (args .project )
292
+ elif args .command == 'create-dataset' :
293
+ create_dataset (args .dataset_name , args .project )
256
294
elif args .command == 'list-tables' :
257
295
list_tables (args .dataset_name , args .project )
258
296
elif args .command == 'create-table' :
0 commit comments