Skip to content

Commit dd823f5

Browse files
chore(bigtable): add bigtable samples (#17240)
Adding back hand-written samples to python-bigtable There is an argument that these samples should live in https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main, but I think it's best to keep the generated and hand-written samples together for now. And then we can revisit this again after splitting the admin and data surfaces. Let me know if you disagree
1 parent 7d230af commit dd823f5

113 files changed

Lines changed: 10415 additions & 3 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/google-cloud-bigtable/CONTRIBUTING.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ On Debian/Ubuntu::
9595
************
9696
Coding Style
9797
************
98-
- We use the automatic code formatter ``black``. You can run it using
99-
the nox session ``blacken``. This will eliminate many lint errors. Run via::
98+
- We use the automatic code formatter ``ruff``. You can run it using
99+
the nox session ``format``. This will eliminate many lint errors. Run via::
100100

101-
$ nox -s blacken
101+
$ nox -s format
102102

103103
- PEP8 compliance is required, with exceptions defined in the linter configuration.
104104
If you have ``nox`` installed, you can test that you have not introduced
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
See https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
See https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/CONTRIBUTING.md
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[//]: # "This README.md file is auto-generated, all changes to this file will be lost."
2+
[//]: # "To regenerate it, use `python -m synthtool`."
3+
4+
## Python Samples for Cloud Bigtable
5+
6+
This directory contains samples for Cloud Bigtable, which may be used as a refererence for how to use this product.
7+
8+
## Additional Information
9+
10+
You can read the documentation for more details on API usage and use GitHub
11+
to <a href="https://github.com/googleapis/python-bigtable">browse the source</a> and [report issues][issues].
12+
13+
### Contributing
14+
View the [contributing guidelines][contrib_guide], the [Python style guide][py_style] for more information.
15+
16+
[authentication]: https://cloud.google.com/docs/authentication/getting-started
17+
[enable_billing]:https://cloud.google.com/apis/docs/getting-started#enabling_billing
18+
[client_library_python]: https://googlecloudplatform.github.io/google-cloud-python/
19+
[issues]: https://github.com/GoogleCloudPlatform/google-cloud-python/issues
20+
[contrib_guide]: https://github.com/googleapis/google-cloud-python/blob/main/CONTRIBUTING.rst
21+
[py_style]: http://google.github.io/styleguide/pyguide.html
22+
[cloud_sdk]: https://cloud.google.com/sdk/docs
23+
[gcloud_shell]: https://cloud.google.com/shell/docs
24+
[gcloud_shell]: https://cloud.google.com/shell/docs

packages/google-cloud-bigtable/samples/__init__.py

Whitespace-only changes.

packages/google-cloud-bigtable/samples/beam/__init__.py

Whitespace-only changes.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Copyright 2020 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+
import datetime
15+
16+
import apache_beam as beam
17+
from apache_beam.io.gcp.bigtableio import WriteToBigTable
18+
from apache_beam.options.pipeline_options import PipelineOptions
19+
20+
from google.cloud.bigtable import row
21+
22+
23+
class BigtableOptions(PipelineOptions):
24+
@classmethod
25+
def _add_argparse_args(cls, parser):
26+
parser.add_argument(
27+
"--bigtable-project",
28+
help="The Bigtable project ID, this can be different than your "
29+
"Dataflow project",
30+
default="bigtable-project",
31+
)
32+
parser.add_argument(
33+
"--bigtable-instance",
34+
help="The Bigtable instance ID",
35+
default="bigtable-instance",
36+
)
37+
parser.add_argument(
38+
"--bigtable-table",
39+
help="The Bigtable table ID in the instance.",
40+
default="bigtable-table",
41+
)
42+
43+
44+
class CreateRowFn(beam.DoFn):
45+
def process(self, key):
46+
direct_row = row.DirectRow(row_key=key)
47+
direct_row.set_cell(
48+
"stats_summary", b"os_build", b"android", datetime.datetime.now()
49+
)
50+
return [direct_row]
51+
52+
53+
def run(argv=None):
54+
"""Build and run the pipeline."""
55+
options = BigtableOptions(argv)
56+
with beam.Pipeline(options=options) as p:
57+
(
58+
p
59+
| beam.Create(["phone#4c410523#20190501", "phone#4c410523#20190502"])
60+
| beam.ParDo(CreateRowFn())
61+
| WriteToBigTable(
62+
project_id=options.bigtable_project,
63+
instance_id=options.bigtable_instance,
64+
table_id=options.bigtable_table,
65+
)
66+
)
67+
68+
69+
if __name__ == "__main__":
70+
run()
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2020 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+
import os
15+
import uuid
16+
17+
import pytest
18+
19+
from ..utils import create_table_cm
20+
from . import hello_world_write
21+
22+
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
23+
BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"]
24+
TABLE_ID = f"mobile-time-series-beam-{str(uuid.uuid4())[:16]}"
25+
26+
27+
@pytest.fixture(scope="module", autouse=True)
28+
def table():
29+
with create_table_cm(
30+
PROJECT, BIGTABLE_INSTANCE, TABLE_ID, {"stats_summary": None}
31+
) as table:
32+
yield table
33+
34+
35+
def test_hello_world_write(table):
36+
hello_world_write.run(
37+
[
38+
"--bigtable-project=%s" % PROJECT,
39+
"--bigtable-instance=%s" % BIGTABLE_INSTANCE,
40+
"--bigtable-table=%s" % TABLE_ID,
41+
]
42+
)
43+
44+
rows = table.read_rows()
45+
count = 0
46+
for _ in rows:
47+
count += 1
48+
assert count == 2

0 commit comments

Comments
 (0)