Skip to content

Commit 606330e

Browse files
committed
Add dataset helper methods
1 parent 79df2b5 commit 606330e

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

bigquery/client.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,33 @@ def get_query_rows(self, job_id, offset=None, limit=None, timeout=0):
321321
records += [self._transform_row(row, schema) for row in rows]
322322
return records
323323

324+
def check_dataset(self, dataset_id):
325+
"""Check to see if a dataset exists.
326+
Args:
327+
dataset: dataset unique id
328+
Returns:
329+
bool indicating if the table exists.
330+
"""
331+
dataset = self.get_dataset(dataset_id)
332+
return bool(dataset)
333+
334+
def get_dataset(self, dataset_id):
335+
"""
336+
Retrieve a dataset if it exists, otherwise return an empty dict.
337+
Args:
338+
dataset: dataset unique id
339+
Returns:
340+
dictionary containing the dataset object if it exists, otherwise
341+
an empty dictionary
342+
"""
343+
try:
344+
dataset = self.bigquery.datasets().get(
345+
projectId=self.project_id, datasetId=dataset_id).execute()
346+
except HttpError:
347+
dataset = {}
348+
349+
return dataset
350+
324351
def check_table(self, dataset, table):
325352
"""Check to see if a table exists.
326353

0 commit comments

Comments
 (0)