Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 950d97a

Browse files
Cloud Bigtable writes samples (GoogleCloudPlatform#2201)
* Bigtable write samples * Cleaning up test * Fixing lint issues * Fixing imports in test * Cleaning up samples and showing error handling * removing note about the row commit bug * Add fixture to write test * Use test fixtures to create and delete test tables.
1 parent 6561c3d commit 950d97a

File tree

7 files changed

+257
-0
lines changed

7 files changed

+257
-0
lines changed

bigtable/snippets/writes/__init__.py

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-bigtable==0.32.1
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2019, Google LLC
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
# [START bigtable_writes_batch]
16+
import datetime
17+
18+
from google.cloud import bigtable
19+
20+
21+
def write_batch(project_id, instance_id, table_id):
22+
client = bigtable.Client(project=project_id, admin=True)
23+
instance = client.instance(instance_id)
24+
table = instance.table(table_id)
25+
26+
timestamp = datetime.datetime.utcnow()
27+
column_family_id = "stats_summary"
28+
29+
rows = [table.row("tablet#a0b81f74#20190501"),
30+
table.row("tablet#a0b81f74#20190502")]
31+
32+
rows[0].set_cell(column_family_id,
33+
"connected_wifi",
34+
1,
35+
timestamp)
36+
rows[0].set_cell(column_family_id,
37+
"os_build",
38+
"12155.0.0-rc1",
39+
timestamp)
40+
rows[1].set_cell(column_family_id,
41+
"connected_wifi",
42+
1,
43+
timestamp)
44+
rows[1].set_cell(column_family_id,
45+
"os_build",
46+
"12145.0.0-rc6",
47+
timestamp)
48+
49+
response = table.mutate_rows(rows)
50+
for i, status in enumerate(response):
51+
if status.code != 0:
52+
print("Error writing row: {}".format(status.message))
53+
54+
print('Successfully wrote 2 rows.')
55+
# [END bigtable_writes_batch]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2019, Google LLC
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
# [START bigtable_writes_conditional]
16+
import datetime
17+
18+
from google.cloud import bigtable
19+
from google.cloud.bigtable import row_filters
20+
21+
22+
def write_conditional(project_id, instance_id, table_id):
23+
client = bigtable.Client(project=project_id, admin=True)
24+
instance = client.instance(instance_id)
25+
table = instance.table(table_id)
26+
27+
timestamp = datetime.datetime.utcnow()
28+
column_family_id = "stats_summary"
29+
30+
row_key = "phone#4c410523#20190501"
31+
32+
row_filter = row_filters.RowFilterChain(
33+
filters=[row_filters.FamilyNameRegexFilter(column_family_id),
34+
row_filters.ColumnQualifierRegexFilter('os_build'),
35+
row_filters.ValueRegexFilter("PQ2A\\..*")])
36+
row = table.row(row_key, filter_=row_filter)
37+
row.set_cell(column_family_id,
38+
"os_name",
39+
"android",
40+
timestamp)
41+
row.commit()
42+
43+
print('Successfully updated row\'s os_name.')
44+
# [END bigtable_writes_conditional]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2019, Google LLC
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
# [START bigtable_writes_increment]
16+
from google.cloud import bigtable
17+
18+
19+
def write_increment(project_id, instance_id, table_id):
20+
client = bigtable.Client(project=project_id, admin=True)
21+
instance = client.instance(instance_id)
22+
table = instance.table(table_id)
23+
24+
column_family_id = "stats_summary"
25+
26+
row_key = "phone#4c410523#20190501"
27+
row = table.row(row_key, append=True)
28+
29+
# Decrement the connected_wifi value by 1.
30+
row.increment_cell_value(column_family_id, "connected_wifi", -1)
31+
row.commit()
32+
33+
print('Successfully updated row {}.'.format(row_key))
34+
# [END bigtable_writes_increment]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2019, Google LLC
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
# [START bigtable_writes_simple]
16+
import datetime
17+
from google.cloud import bigtable
18+
19+
20+
def write_simple(project_id, instance_id, table_id):
21+
client = bigtable.Client(project=project_id, admin=True)
22+
instance = client.instance(instance_id)
23+
table = instance.table(table_id)
24+
25+
timestamp = datetime.datetime.utcnow()
26+
column_family_id = "stats_summary"
27+
28+
row_key = "phone#4c410523#20190501"
29+
30+
row = table.row(row_key)
31+
row.set_cell(column_family_id,
32+
"connected_cell",
33+
1,
34+
timestamp)
35+
row.set_cell(column_family_id,
36+
"connected_wifi",
37+
1,
38+
timestamp)
39+
row.set_cell(column_family_id,
40+
"os_build",
41+
"PQ2A.190405.003",
42+
timestamp)
43+
44+
row.commit()
45+
46+
print('Successfully wrote row {}.'.format(row_key))
47+
# [END bigtable_writes_simple]
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Copyright 2018 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import uuid
17+
import pytest
18+
19+
from google.cloud import bigtable
20+
21+
from .write_batch import write_batch
22+
from .write_conditionally import write_conditional
23+
from .write_increment import write_increment
24+
from .write_simple import write_simple
25+
26+
PROJECT = os.environ['GCLOUD_PROJECT']
27+
BIGTABLE_INSTANCE = os.environ['BIGTABLE_CLUSTER']
28+
TABLE_ID_PREFIX = 'mobile-time-series-{}'
29+
30+
31+
@pytest.fixture
32+
def bigtable_client():
33+
return bigtable.Client(project=PROJECT, admin=True)
34+
35+
36+
@pytest.fixture
37+
def bigtable_instance(bigtable_client):
38+
return bigtable_client.instance(BIGTABLE_INSTANCE)
39+
40+
41+
@pytest.fixture
42+
def table_id(bigtable_instance):
43+
table_id = TABLE_ID_PREFIX.format(str(uuid.uuid4())[:16])
44+
table = bigtable_instance.table(table_id)
45+
if table.exists():
46+
table.delete()
47+
48+
column_family_id = 'stats_summary'
49+
column_families = {column_family_id: None}
50+
table.create(column_families=column_families)
51+
52+
yield table_id
53+
54+
table.delete()
55+
56+
57+
def test_writes(capsys, table_id):
58+
write_simple(PROJECT, BIGTABLE_INSTANCE, table_id)
59+
60+
out, _ = capsys.readouterr()
61+
assert 'Successfully wrote row' in out
62+
63+
write_increment(PROJECT, BIGTABLE_INSTANCE, table_id)
64+
65+
out, _ = capsys.readouterr()
66+
assert 'Successfully updated row' in out
67+
68+
write_conditional(PROJECT, BIGTABLE_INSTANCE, table_id)
69+
70+
out, _ = capsys.readouterr()
71+
assert 'Successfully updated row\'s os_name' in out
72+
73+
write_batch(PROJECT, BIGTABLE_INSTANCE, table_id)
74+
75+
out, _ = capsys.readouterr()
76+
assert 'Successfully wrote 2 rows' in out

0 commit comments

Comments
 (0)