WJA
WJA

Reputation: 7004

How to check if a dataset exists in BigQuery?

We can initiate a BigQuery dataset as follows:

dataset_ref = self.client.dataset(dataset_id=self.dataset_id)
dataset = bigquery.Dataset(dataset_ref)

How can I check if this dataset exists already? When I look at the properties of dataset they seem to be overlapping for sets that exists and one that do not exist.

Upvotes: 1

Views: 6218

Answers (2)

Cem
Cem

Reputation: 616

You can ignore the error using exists_ok parameter in create_dataset function.

# create dataset if not exists

self.client.create_dataset(dataset_id=self.dataset_id, exists_ok=True)

I have found it in Google's bigquery API repo.

Upvotes: 2

mkos
mkos

Reputation: 111

The docs recommend using get_dataset to determine if a dataset exists.

from google.cloud.exceptions import NotFound

dataset_id = "pigs_in_space"

try:
    client.get_dataset(dataset_id)  # Make an API request.
    print("Dataset {} already exists".format(dataset_id))
except NotFound:
    print("Dataset {} is not found".format(dataset_id))

Upvotes: 7

Related Questions