-
Notifications
You must be signed in to change notification settings - Fork 6.6k
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
Changes from all commits
87c68ae
971e1b7
1176825
a87606a
222fd2c
e12b69f
3d63d6a
3233a42
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 @@ | ||
google-cloud-bigtable==0.32.1 |
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() | ||
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] |
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] |
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] |
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] |
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) | ||
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. Use a pytest fixture with https://docs.pytest.org/en/latest/fixture.html#fixture-finalization-executing-teardown-code 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. Not sure how to do this with what I have. I put 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. That's not how you use fixtures... An example fixture: 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. 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. I don't really understand this still. These examples all look different. Just committed what I understood 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. OK. I just pushed 3233a42 with my requested changes. The gist of "fixtures" are that they are the equivalent of a |
||
|
||
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 |
Uh oh!
There was an error while loading. Please reload this page.