Skip to content

Commit 745d67e

Browse files
committed
Added a create view function to client.
1 parent 6513ff3 commit 745d67e

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

bigquery/client.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,46 @@ def create_table(self, dataset, table, schema):
398398
else:
399399
return {}
400400

401+
def create_view(self, dataset, view, query):
402+
"""Create a new view in the dataset.
403+
404+
Args:
405+
dataset: the dataset to create the view in.
406+
view: the name of view to create.
407+
query: a query that BigQuery executes when the view is referenced.
408+
409+
Returns:
410+
bool indicating if the view was successfully created or not,
411+
or response from BigQuery if swallow_results is set for False.
412+
"""
413+
414+
body = {
415+
'tableReference': {
416+
'tableId': view,
417+
'projectId': self.project_id,
418+
'datasetId': dataset
419+
},
420+
'view': {
421+
'query': query
422+
}
423+
}
424+
425+
try:
426+
view = self.bigquery.tables().insert(
427+
projectId=self.project_id,
428+
datasetId=dataset,
429+
body=body
430+
).execute()
431+
if self.swallow_results:
432+
return True
433+
else:
434+
return view
435+
436+
except HttpError as e:
437+
logging.error(('Cannot create view {0}.{1}\n'
438+
'Http Error: {2}').format(dataset, view,
439+
e.content))
440+
401441
def delete_table(self, dataset, table):
402442
"""Delete a table from the dataset.
403443

0 commit comments

Comments
 (0)