Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 0 additions & 141 deletions bigquery/docs/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,147 +228,6 @@ def test_create_partitioned_table(client, to_delete):
assert table.time_partitioning.expiration_ms == 7776000000


@pytest.mark.skip(
reason=(
"update_table() is flaky "
"https://github.com/GoogleCloudPlatform/google-cloud-python/issues/5589"
)
)
def test_manage_table_labels(client, to_delete):
dataset_id = "label_table_dataset_{}".format(_millis())
table_id = "label_table_{}".format(_millis())
dataset = bigquery.Dataset(client.dataset(dataset_id))
client.create_dataset(dataset)
to_delete.append(dataset)

table = bigquery.Table(dataset.table(table_id), schema=SCHEMA)
table = client.create_table(table)

# [START bigquery_label_table]
# from google.cloud import bigquery
# client = bigquery.Client()
# table_ref = client.dataset('my_dataset').table('my_table')
# table = client.get_table(table_ref) # API request

assert table.labels == {}
labels = {"color": "green"}
table.labels = labels

table = client.update_table(table, ["labels"]) # API request

assert table.labels == labels
# [END bigquery_label_table]

# [START bigquery_get_table_labels]
# from google.cloud import bigquery
# client = bigquery.Client()
# dataset_id = 'my_dataset'
# table_id = 'my_table'

dataset_ref = client.dataset(dataset_id)
table_ref = dataset_ref.table(table_id)
table = client.get_table(table_ref) # API Request

# View table labels
print("Table ID: {}".format(table_id))
print("Labels:")
if table.labels:
for label, value in table.labels.items():
print("\t{}: {}".format(label, value))
else:
print("\tTable has no labels defined.")
# [END bigquery_get_table_labels]
assert table.labels == labels

# [START bigquery_delete_label_table]
# from google.cloud import bigquery
# client = bigquery.Client()
# table_ref = client.dataset('my_dataset').table('my_table')
# table = client.get_table(table_ref) # API request

# This example table starts with one label
assert table.labels == {"color": "green"}
# To delete a label from a table, set its value to None
table.labels["color"] = None

table = client.update_table(table, ["labels"]) # API request

assert table.labels == {}
# [END bigquery_delete_label_table]


@pytest.mark.skip(
reason=(
"update_table() is flaky "
"https://github.com/GoogleCloudPlatform/google-cloud-python/issues/5589"
)
)
def test_update_table_description(client, to_delete):
"""Update a table's description."""
dataset_id = "update_table_description_dataset_{}".format(_millis())
table_id = "update_table_description_table_{}".format(_millis())
dataset = bigquery.Dataset(client.dataset(dataset_id))
client.create_dataset(dataset)
to_delete.append(dataset)

table = bigquery.Table(dataset.table(table_id), schema=SCHEMA)
table.description = "Original description."
table = client.create_table(table)

# [START bigquery_update_table_description]
# from google.cloud import bigquery
# client = bigquery.Client()
# table_ref = client.dataset('my_dataset').table('my_table')
# table = client.get_table(table_ref) # API request

assert table.description == "Original description."
table.description = "Updated description."

table = client.update_table(table, ["description"]) # API request

assert table.description == "Updated description."
# [END bigquery_update_table_description]


@pytest.mark.skip(
reason=(
"update_table() is flaky "
"https://github.com/GoogleCloudPlatform/google-cloud-python/issues/5589"
)
)
def test_update_table_expiration(client, to_delete):
"""Update a table's expiration time."""
dataset_id = "update_table_expiration_dataset_{}".format(_millis())
table_id = "update_table_expiration_table_{}".format(_millis())
dataset = bigquery.Dataset(client.dataset(dataset_id))
client.create_dataset(dataset)
to_delete.append(dataset)

table = bigquery.Table(dataset.table(table_id), schema=SCHEMA)
table = client.create_table(table)

# [START bigquery_update_table_expiration]
import datetime
import pytz

# from google.cloud import bigquery
# client = bigquery.Client()
# table_ref = client.dataset('my_dataset').table('my_table')
# table = client.get_table(table_ref) # API request

assert table.expires is None

# set table to expire 5 days from now
expiration = datetime.datetime.now(pytz.utc) + datetime.timedelta(days=5)
table.expires = expiration
table = client.update_table(table, ["expires"]) # API request

# expiration is stored in milliseconds
margin = datetime.timedelta(microseconds=1000)
assert expiration - margin <= table.expires <= expiration + margin
# [END bigquery_update_table_expiration]


@pytest.mark.skip(
reason=(
"update_table() is flaky "
Expand Down
37 changes: 35 additions & 2 deletions bigquery/docs/usage/tables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ Browse data rows in a table with the
:start-after: [START bigquery_browse_table]
:end-before: [END bigquery_browse_table]

Get labels from the existing table:

.. literalinclude:: ../samples/get_table_labels.py
:language: python
:dedent: 4
:start-after: [START bigquery_get_table_labels]
:end-before: [END bigquery_get_table_labels]

Creating a Table
^^^^^^^^^^^^^^^^

Expand Down Expand Up @@ -113,15 +121,24 @@ See also: `Loading Parquet data from Cloud Storage
Updating a Table
^^^^^^^^^^^^^^^^

Update a property in a table's metadata with the
Update a description property in a table's metadata with the
:func:`~google.cloud.bigquery.client.Client.update_table` method:

.. literalinclude:: ../snippets.py
.. literalinclude:: ../samples/update_table_description.py
:language: python
:dedent: 4
:start-after: [START bigquery_update_table_description]
:end-before: [END bigquery_update_table_description]

Update a expires property in a table's metadata with the
:func:`~google.cloud.bigquery.client.Client.update_table` method:

.. literalinclude:: ../samples/update_table_expiration.py
:language: python
:dedent: 4
:start-after: [START bigquery_update_table_expiration]
:end-before: [END bigquery_update_table_expiration]

Insert rows into a table's data with the
:func:`~google.cloud.bigquery.client.Client.insert_rows` method:

Expand Down Expand Up @@ -154,6 +171,22 @@ Add an empty column to the existing table with the
:start-after: [START bigquery_add_empty_column]
:end-before: [END bigquery_add_empty_column]

Add labels to the existing table:

.. literalinclude:: ../samples/label_table.py
:language: python
:dedent: 4
:start-after: [START bigquery_label_table]
:end-before: [END bigquery_label_table]

Delete labels from the existing table:

.. literalinclude:: ../samples/delete_table_labels.py
:language: python
:dedent: 4
:start-after: [START bigquery_delete_label_table]
:end-before: [END bigquery_delete_label_table]

Copying a Table
^^^^^^^^^^^^^^^

Expand Down
36 changes: 36 additions & 0 deletions bigquery/samples/delete_table_labels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2020 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_table_labels(table_id):

# [START bigquery_delete_label_table]

from google.cloud import bigquery

# Construct a BigQuery client object.
client = bigquery.Client()

# TODO(developer): Set table_id to the ID of the table to create.
# table_id = "your-project.your_dataset.your_table_name"

table = client.get_table(table_id) # Make an API request.

# To delete a label from a dataset, set its value to None.
table.labels["color"] = None

table = client.update_table(table, ["labels"]) # Make an API request.
print("Labels deleted from {}".format(table_id))
# [END bigquery_delete_label_table]
return table
38 changes: 38 additions & 0 deletions bigquery/samples/get_table_labels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2020 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_table_labels(table_id):

# [START bigquery_get_table_labels]

from google.cloud import bigquery

# Construct a BigQuery client object.
client = bigquery.Client()

# TODO(developer): Set table_id to the ID of the table to create.
# table_id = "your-project.your_dataset.your_table_name"

table = client.get_table(table_id) # Make an API request.

# View table labels.
print("Table ID: {}".format(table_id))
print("Labels:")
if table.labels:
for label, value in table.labels.items():
print("\t{}: {}".format(label, value))
else:
print("\tTable has no labels defined.")
# [END bigquery_get_table_labels]
35 changes: 35 additions & 0 deletions bigquery/samples/label_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2020 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 label_table(table_id):

# [START bigquery_label_table]

from google.cloud import bigquery

# Construct a BigQuery client object.
client = bigquery.Client()

# TODO(developer): Set table_id to the ID of the table to create.
# table_id = "your-project.your_dataset.your_table_name"

table = client.get_table(table_id) # Make an API request.
assert table.labels == {}
labels = {"color": "green"}
table.labels = labels

table = client.update_table(table, ["labels"]) # API request
print("Labels added to {}".format(table_id))
# [END bigquery_label_table]
33 changes: 33 additions & 0 deletions bigquery/samples/tests/test_table_label_samples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2020 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.

from .. import label_table
from .. import get_table_labels
from .. import delete_table_labels


def test_table_label_samples(capsys, table_id):

label_table.label_table(table_id)
out, err = capsys.readouterr()
assert "Labels added to {}".format(table_id) in out

get_table_labels.get_table_labels(table_id)
out, err = capsys.readouterr()
assert "color: green" in out

dataset = delete_table_labels.delete_table_labels(table_id)
out, err = capsys.readouterr()
assert "Labels deleted from {}".format(table_id) in out
assert dataset.labels.get("color") is None
22 changes: 22 additions & 0 deletions bigquery/samples/tests/test_update_table_description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2020 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.

from .. import update_table_description


def test_update_table_descritpion(capsys, random_table_id):

update_table_description.update_table_description(random_table_id)
out, _ = capsys.readouterr()
assert "Updated description." in out
Loading