Skip to content

Commit 337f748

Browse files
sangramqlcrwilcox
authored andcommitted
Bigtable Row Set snippets (googleapis#7016)
* add conditional row snippets * add snippets * Add row set snippets * remove unwanted changes in snippets.py * Rectify snippets file name and tags * blacken
1 parent fd178d6 commit 337f748

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

bigtable/docs/snippets_table.py

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
INSTANCE_ID = "snippet-" + unique_resource_id("-")
4343
CLUSTER_ID = "clus-1-" + unique_resource_id("-")
4444
TABLE_ID = "tabl-1-" + unique_resource_id("-")
45-
COLUMN_FAMILY_ID = "col_fam_id-" + unique_resource_id("-")
4645
LOCATION_ID = "us-central1-f"
4746
ALT_LOCATION_ID = "us-central1-a"
4847
PRODUCTION = enums.Instance.Type.PRODUCTION
@@ -55,8 +54,14 @@
5554
.strftime("%Y-%m-%dt%H-%M-%S")
5655
)
5756
LABELS = {LABEL_KEY: str(LABEL_STAMP)}
57+
COLUMN_FAMILY_ID = "col_fam_id1"
5858
COL_NAME1 = b"col-name1"
5959
CELL_VAL1 = b"cell-val"
60+
ROW_KEY1 = b"row_key_id1"
61+
COLUMN_FAMILY_ID2 = "col_fam_id2"
62+
COL_NAME2 = b"col-name2"
63+
CELL_VAL2 = b"cell-val2"
64+
ROW_KEY2 = b"row_key_id2"
6065

6166

6267
class Config(object):
@@ -90,6 +95,9 @@ def setup_module():
9095
gc_rule = column_family.MaxVersionsGCRule(2)
9196
column_family1 = Config.TABLE.column_family(COLUMN_FAMILY_ID, gc_rule=gc_rule)
9297
column_family1.create()
98+
gc_rule2 = column_family.MaxVersionsGCRule(4)
99+
column_family2 = Config.TABLE.column_family(COLUMN_FAMILY_ID2, gc_rule=gc_rule2)
100+
column_family2.create()
93101

94102

95103
def teardown_module():
@@ -403,5 +411,80 @@ def test_bigtable_table_row():
403411
table.truncate(timeout=300)
404412

405413

414+
def test_bigtable_add_row_add_row_range_add_row_range_from_keys():
415+
row_keys = [
416+
b"row_key_1",
417+
b"row_key_2",
418+
b"row_key_3",
419+
b"row_key_4",
420+
b"row_key_5",
421+
b"row_key_6",
422+
b"row_key_7",
423+
b"row_key_8",
424+
b"row_key_9",
425+
]
426+
427+
rows = []
428+
for row_key in row_keys:
429+
row = Config.TABLE.row(row_key)
430+
row.set_cell(COLUMN_FAMILY_ID, COL_NAME1, CELL_VAL1)
431+
rows.append(row)
432+
Config.TABLE.mutate_rows(rows)
433+
434+
# [START bigtable_add_row_key]
435+
from google.cloud.bigtable import Client
436+
from google.cloud.bigtable.row_set import RowSet
437+
438+
client = Client(admin=True)
439+
instance = client.instance(INSTANCE_ID)
440+
table = instance.table(TABLE_ID)
441+
442+
row_set = RowSet()
443+
row_set.add_row_key(b"row_key_5")
444+
# [END bigtable_add_row_key]
445+
446+
read_rows = table.read_rows(row_set=row_set)
447+
expected_row_keys = [b"row_key_5"]
448+
found_row_keys = [row.row_key for row in read_rows]
449+
assert found_row_keys == expected_row_keys
450+
451+
# [START bigtable_add_row_range]
452+
from google.cloud.bigtable import Client
453+
from google.cloud.bigtable.row_set import RowSet
454+
from google.cloud.bigtable.row_set import RowRange
455+
456+
client = Client(admin=True)
457+
instance = client.instance(INSTANCE_ID)
458+
table = instance.table(TABLE_ID)
459+
460+
row_set = RowSet()
461+
row_set.add_row_range(RowRange(start_key=b"row_key_3", end_key=b"row_key_7"))
462+
# [END bigtable_add_row_range]
463+
464+
read_rows = table.read_rows(row_set=row_set)
465+
expected_row_keys = [b"row_key_3", b"row_key_4", b"row_key_5", b"row_key_6"]
466+
found_row_keys = [row.row_key for row in read_rows]
467+
assert found_row_keys == expected_row_keys
468+
469+
# [START bigtable_row_range_from_keys]
470+
from google.cloud.bigtable import Client
471+
from google.cloud.bigtable.row_set import RowSet
472+
473+
client = Client(admin=True)
474+
instance = client.instance(INSTANCE_ID)
475+
table = instance.table(TABLE_ID)
476+
477+
row_set = RowSet()
478+
row_set.add_row_range_from_keys(start_key=b"row_key_3", end_key=b"row_key_7")
479+
# [END bigtable_row_range_from_keys]
480+
481+
read_rows = table.read_rows(row_set=row_set)
482+
expected_row_keys = [b"row_key_3", b"row_key_4", b"row_key_5", b"row_key_6"]
483+
found_row_keys = [row.row_key for row in read_rows]
484+
assert found_row_keys == expected_row_keys
485+
486+
table.truncate(timeout=200)
487+
488+
406489
if __name__ == "__main__":
407490
pytest.main()

bigtable/google/cloud/bigtable/row_set.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ def __ne__(self, other):
5353
def add_row_key(self, row_key):
5454
"""Add row key to row_keys list.
5555
56+
For example:
57+
58+
.. literalinclude:: snippets_table.py
59+
:start-after: [START bigtable_add_row_key]
60+
:end-before: [END bigtable_add_row_key]
61+
5662
:type row_key: bytes
5763
:param row_key: The key of a row to read
5864
"""
@@ -61,6 +67,12 @@ def add_row_key(self, row_key):
6167
def add_row_range(self, row_range):
6268
"""Add row_range to row_ranges list.
6369
70+
For example:
71+
72+
.. literalinclude:: snippets_table.py
73+
:start-after: [START bigtable_add_row_range]
74+
:end-before: [END bigtable_add_row_range]
75+
6476
:type row_range: class:`RowRange`
6577
:param row_range: The row range object having start and end key
6678
"""
@@ -71,6 +83,12 @@ def add_row_range_from_keys(
7183
):
7284
"""Add row range to row_ranges list from the row keys
7385
86+
For example:
87+
88+
.. literalinclude:: snippets_table.py
89+
:start-after: [START bigtable_row_range_from_keys]
90+
:end-before: [END bigtable_row_range_from_keys]
91+
7492
:type start_key: bytes
7593
:param start_key: (Optional) Start key of the row range. If left empty,
7694
will be interpreted as the empty string.

0 commit comments

Comments
 (0)