Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions bigtable/snippets/writes/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
google-cloud-bigtable==0.32.1
52 changes: 52 additions & 0 deletions bigtable/snippets/writes/write_batch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/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()
Comment thread
billyjacobson marked this conversation as 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)

table.mutate_rows(rows)
Comment thread
billyjacobson marked this conversation as resolved.
Outdated

print('Successfully wrote 2 rows.')
# [END bigtable_writes_batch]
45 changes: 45 additions & 0 deletions bigtable/snippets/writes/write_conditionally.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/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.row_filters import ColumnQualifierRegexFilter, \
Comment thread
billyjacobson marked this conversation as resolved.
Outdated
FamilyNameRegexFilter, RowFilterChain, ValueRegexFilter


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 = RowFilterChain(
filters=[FamilyNameRegexFilter(column_family_id),
ColumnQualifierRegexFilter('os_build'),
ValueRegexFilter("PQ2A\\..*")])
row = table.row(row_key, filter_=row_filter)
row.set_cell(column_family_id,
"os_name",
"android",
timestamp)
row.commit()
Comment thread
billyjacobson marked this conversation as resolved.

print('Successfully updated row\'s os_name.')
# [END bigtable_writes_conditional]
32 changes: 32 additions & 0 deletions bigtable/snippets/writes/write_increment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/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)
row.increment_cell_value(column_family_id, "connected_wifi", -1)
Comment thread
billyjacobson marked this conversation as resolved.
row.commit()

print('Successfully updated row {}.'.format(row_key))
# [END bigtable_writes_increment]
47 changes: 47 additions & 0 deletions 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]
62 changes: 62 additions & 0 deletions bigtable/snippets/writes/writes_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# 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

from google.cloud import bigtable
from write_batch import write_batch
Comment thread
billyjacobson marked this conversation as resolved.
Outdated
from write_conditionally import write_conditional
from write_increment import write_increment
from write_simple import write_simple

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


def test_writes(capsys):
client = bigtable.Client(project=PROJECT, admin=True)
instance = client.instance(BIGTABLE_INSTANCE)

table_id = TABLE_ID_PREFIX.format(str(uuid.uuid4())[:16])
table = 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.


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

table.delete()