-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added collection of Managing Dataset samples #6
Changes from 7 commits
6d70183
27f5828
ea12d6f
9cc5169
55e6a61
1a5270f
a589575
ba7c6fd
34db4d4
502facb
8f72584
2b30305
d8e579a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Copyright 2019 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
def delete_dataset(client, dataset_id): | ||
|
||
# [START bigquery_delete_dataset] | ||
from google.cloud import bigquery | ||
|
||
# TODO(developer): Construct a BigQuery client object. | ||
# client = bigquery.Client() | ||
|
||
# TODO(developer): Set model_id to the ID of the model to fetch. | ||
# dataset_id = 'your-project.your_dataset' | ||
|
||
client.delete_dataset(dataset_id) | ||
# [END bigquery_delete_dataset] | ||
|
||
print("Deleted dataset '{}'.".format(dataset_id)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Copyright 2019 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
def get_dataset(client, dataset_id): | ||
|
||
# [START bigquery_get_dataset] | ||
from google.cloud import bigquery | ||
|
||
# TODO(developer): Construct a BigQuery client object. | ||
# client = bigquery.Client() | ||
|
||
# TODO(developer): Set dataset_id to the ID of the dataset to fetch. | ||
# dataset_id = 'your-project.your_dataset' | ||
|
||
dataset = client.get_dataset(dataset_id) | ||
|
||
full_dataset_id = "{}.{}".format(dataset.project, dataset.dataset_id) | ||
friendly_name = dataset.friendly_name | ||
print( | ||
"Got dataset '{}' with friendly_name '{}'.".format( | ||
full_dataset_id, friendly_name | ||
) | ||
) | ||
|
||
# View dataset properties | ||
print("Dataset ID: {}".format(dataset_id)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can delete this line since you're printing the ID in the same sample in the print statement above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed! |
||
print("Description: {}".format(dataset.description)) | ||
print("Labels:") | ||
labels = dataset.labels | ||
if labels: | ||
for label, value in labels.items(): | ||
print("\t{}: {}".format(label, value)) | ||
else: | ||
print("\tDataset has no labels defined.") | ||
|
||
# View tables in dataset | ||
print("Tables:") | ||
tables = list(client.list_tables(dataset)) # API request(s) | ||
if tables: | ||
for table in tables: | ||
print("\t{}".format(table.table_id)) | ||
else: | ||
print("\tThis dataset does not contain any tables.") | ||
|
||
# [END bigquery_get_dataset] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Copyright 2019 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
def list_datasets(client): | ||
|
||
# [START bigquery_list_datasets] | ||
from google.cloud import bigquery | ||
|
||
# TODO(developer): Construct a BigQuery client object. | ||
# client = bigquery.Client() | ||
|
||
datasets = list(client.list_datasets()) | ||
project = client.project | ||
|
||
if datasets: | ||
print("Datasets in project {}:".format(project)) | ||
for dataset in datasets: # API request(s) | ||
print("\t{}".format(dataset.dataset_id)) | ||
else: | ||
print("{} project does not contain any datasets.".format(project)) | ||
# [END bigquery_list_datasets] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,10 +13,52 @@ | |
# limitations under the License. | ||
|
||
from .. import create_dataset | ||
from .. import delete_dataset | ||
from .. import get_dataset | ||
from .. import list_datasets | ||
from .. import update_dataset_description | ||
from .. import update_dataset_default_table_expiration | ||
from .. import update_dataset_access | ||
|
||
|
||
def test_create_dataset(capsys, client, random_dataset_id): | ||
def test_dataset_samples(capsys, client, random_dataset_id): | ||
|
||
# create dataset | ||
create_dataset.create_dataset(client, random_dataset_id) | ||
out, err = capsys.readouterr() | ||
assert "Created dataset {}".format(random_dataset_id) in out | ||
|
||
# get dataset | ||
get_dataset.get_dataset(client, random_dataset_id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These should all be separate tests. That way we can more easily tell when an individual sample is broken. Models API is an exception because it's so slow to create one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All separated out into their own tests. Had to include the create_dataset function for some of the tests or else they wouldn't pass (needs a dataset to make changes to!). |
||
out, err = capsys.readouterr() | ||
assert random_dataset_id in out | ||
|
||
# list dataset | ||
list_datasets.list_datasets(client) | ||
out, err = capsys.readouterr() | ||
assert "Datasets in project {}:".format(client.project) in out | ||
|
||
# update dataset description | ||
update_dataset_description.update_dataset_description(client, random_dataset_id) | ||
out, err = capsys.readouterr() | ||
assert "Updated description." in out | ||
|
||
# update dataset table expiration | ||
update_dataset_default_table_expiration.update_dataset_default_table_expiration( | ||
client, random_dataset_id | ||
) | ||
out, err = capsys.readouterr() | ||
assert "Updated dataset {}".format(random_dataset_id) in out | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please look for the expected expiration There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved the time into it's own fixture so it's easier to be used in the test. |
||
|
||
# update dataset permissions | ||
update_dataset_access.update_dataset_access(client, random_dataset_id) | ||
out, err = capsys.readouterr() | ||
assert ( | ||
"Updated dataset '{}' with modified user permissions.".format(random_dataset_id) | ||
in out | ||
) | ||
|
||
# delete dataset | ||
delete_dataset.delete_dataset(client, random_dataset_id) | ||
out, err = capsys.readouterr() | ||
assert "Deleted dataset '{}'.".format(random_dataset_id) in out |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright 2019 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
def update_dataset_access(client, dataset_id): | ||
|
||
# [START bigquery_update_dataset_access] | ||
tswast marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from google.cloud import bigquery | ||
|
||
# TODO(developer): Construct a BigQuery client object. | ||
# client = bigquery.Client() | ||
|
||
# TODO(developer): Set dataset_id to the ID of the dataset to fetch. | ||
# dataset_id = 'your-project.your_dataset' | ||
|
||
dataset = client.get_dataset(dataset_id) | ||
|
||
entry = bigquery.AccessEntry( | ||
role="READER", | ||
entity_type="userByEmail", | ||
entity_id="[email protected]", | ||
) | ||
|
||
entries = list(dataset.access_entries) | ||
entries.append(entry) | ||
dataset.access_entries = entries | ||
|
||
dataset = client.update_dataset(dataset, ["access_entries"]) # API request | ||
|
||
full_dataset_id = "{}.{}".format(dataset.project, dataset.dataset_id) | ||
print( | ||
"Updated dataset '{}' with modified user permissions.".format(full_dataset_id) | ||
) | ||
# [END bigquery_update_dataset_access] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original sample also had an example showing the
delete_contents=True
option. I think you could actually do the same, but also show offnot_found_ok=True
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok! Added the parameters and notes about what they do.