Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Cloud Bigtable writes samples #2201

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

Merged
merged 8 commits into from
Jun 26, 2019
Merged
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
Empty file.
1 change: 1 addition & 0 deletions 1 bigtable/snippets/writes/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
google-cloud-bigtable==0.32.1
55 changes: 55 additions & 0 deletions 55 bigtable/snippets/writes/write_batch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env python

# 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
#
# http://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.
# [START bigtable_writes_batch]
import datetime

from google.cloud import bigtable


def write_batch(project_id, instance_id, table_id):
client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)

timestamp = datetime.datetime.utcnow()
billyjacobson marked this conversation as resolved.
Show resolved Hide resolved
column_family_id = "stats_summary"

rows = [table.row("tablet#a0b81f74#20190501"),
table.row("tablet#a0b81f74#20190502")]

rows[0].set_cell(column_family_id,
"connected_wifi",
1,
timestamp)
rows[0].set_cell(column_family_id,
"os_build",
"12155.0.0-rc1",
timestamp)
rows[1].set_cell(column_family_id,
"connected_wifi",
1,
timestamp)
rows[1].set_cell(column_family_id,
"os_build",
"12145.0.0-rc6",
timestamp)

response = table.mutate_rows(rows)
for i, status in enumerate(response):
if status.code != 0:
print("Error writing row: {}".format(status.message))

print('Successfully wrote 2 rows.')
# [END bigtable_writes_batch]
44 changes: 44 additions & 0 deletions 44 bigtable/snippets/writes/write_conditionally.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python

# 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
#
# http://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.
# [START bigtable_writes_conditional]
import datetime

from google.cloud import bigtable
from google.cloud.bigtable import row_filters


def write_conditional(project_id, instance_id, table_id):
client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)

timestamp = datetime.datetime.utcnow()
column_family_id = "stats_summary"

row_key = "phone#4c410523#20190501"

row_filter = row_filters.RowFilterChain(
filters=[row_filters.FamilyNameRegexFilter(column_family_id),
row_filters.ColumnQualifierRegexFilter('os_build'),
row_filters.ValueRegexFilter("PQ2A\\..*")])
row = table.row(row_key, filter_=row_filter)
row.set_cell(column_family_id,
"os_name",
"android",
timestamp)
row.commit()
billyjacobson marked this conversation as resolved.
Show resolved Hide resolved

print('Successfully updated row\'s os_name.')
# [END bigtable_writes_conditional]
34 changes: 34 additions & 0 deletions 34 bigtable/snippets/writes/write_increment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python

# 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
#
# http://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.
# [START bigtable_writes_increment]
from google.cloud import bigtable


def write_increment(project_id, instance_id, table_id):
client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)

column_family_id = "stats_summary"

row_key = "phone#4c410523#20190501"
row = table.row(row_key, append=True)

# Decrement the connected_wifi value by 1.
row.increment_cell_value(column_family_id, "connected_wifi", -1)
billyjacobson marked this conversation as resolved.
Show resolved Hide resolved
row.commit()

print('Successfully updated row {}.'.format(row_key))
# [END bigtable_writes_increment]
47 changes: 47 additions & 0 deletions 47 bigtable/snippets/writes/write_simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python

# 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
#
# http://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.
# [START bigtable_writes_simple]
import datetime
from google.cloud import bigtable


def write_simple(project_id, instance_id, table_id):
client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)

timestamp = datetime.datetime.utcnow()
column_family_id = "stats_summary"

row_key = "phone#4c410523#20190501"

row = table.row(row_key)
row.set_cell(column_family_id,
"connected_cell",
1,
timestamp)
row.set_cell(column_family_id,
"connected_wifi",
1,
timestamp)
row.set_cell(column_family_id,
"os_build",
"PQ2A.190405.003",
timestamp)

row.commit()

print('Successfully wrote row {}.'.format(row_key))
# [END bigtable_writes_simple]
76 changes: 76 additions & 0 deletions 76 bigtable/snippets/writes/writes_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Copyright 2018 Google Inc.
#
# 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
#
# http://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.

import os
import uuid
import pytest

from google.cloud import bigtable

from .write_batch import write_batch
from .write_conditionally import write_conditional
from .write_increment import write_increment
from .write_simple import write_simple

billyjacobson marked this conversation as resolved.
Show resolved Hide resolved
PROJECT = os.environ['GCLOUD_PROJECT']
BIGTABLE_INSTANCE = os.environ['BIGTABLE_CLUSTER']
TABLE_ID_PREFIX = 'mobile-time-series-{}'


@pytest.fixture
def bigtable_client():
return bigtable.Client(project=PROJECT, admin=True)


@pytest.fixture
def bigtable_instance(bigtable_client):
return bigtable_client.instance(BIGTABLE_INSTANCE)


@pytest.fixture
def table_id(bigtable_instance):
table_id = TABLE_ID_PREFIX.format(str(uuid.uuid4())[:16])
table = bigtable_instance.table(table_id)
if table.exists():
table.delete()

column_family_id = 'stats_summary'
column_families = {column_family_id: None}
table.create(column_families=column_families)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a pytest fixture with yield so that the table is cleaned up even if the tests fail.

https://docs.pytest.org/en/latest/fixture.html#fixture-finalization-executing-teardown-code

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how to do this with what I have. I put @pytest.fixture(scope="module") before def tests_writes, but that seems to break things and then I'm not sure what else to do.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not how you use fixtures...

An example fixture:

https://github.com/googleapis/google-cloud-python/blob/0e231cab5f066ae5b3e254d0cb7d204e3dac7ebc/bigquery/samples/tests/conftest.py#L58-L68

Note: it's in conftest.py, but could just as easily be in the same file as the tests.

To use, simply add an argument to your test function with the same name as the fixture function.

https://github.com/googleapis/google-cloud-python/blob/0e231cab5f066ae5b3e254d0cb7d204e3dac7ebc/bigquery/samples/tests/test_list_tables.py#L18

Copy link
Member Author

@billyjacobson billyjacobson Jun 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand this still. These examples all look different. Just committed what I understood

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I just pushed 3233a42 with my requested changes.

The gist of "fixtures" are that they are the equivalent of a setUp function in JUnit. If you yield from a fixture, the remaining piece of the function is equivalent to tearDown in JUnit.


yield table_id

table.delete()


def test_writes(capsys, table_id):
write_simple(PROJECT, BIGTABLE_INSTANCE, table_id)

out, _ = capsys.readouterr()
assert 'Successfully wrote row' in out

write_increment(PROJECT, BIGTABLE_INSTANCE, table_id)

out, _ = capsys.readouterr()
assert 'Successfully updated row' in out

write_conditional(PROJECT, BIGTABLE_INSTANCE, table_id)

out, _ = capsys.readouterr()
assert 'Successfully updated row\'s os_name' in out

write_batch(PROJECT, BIGTABLE_INSTANCE, table_id)

out, _ = capsys.readouterr()
assert 'Successfully wrote 2 rows' in out
Morty Proxy This is a proxified and sanitized view of the page, visit original site.