forked from apache/beam
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Docs] Add transform catalog page for enrichment transform (apache#30187
) * add transform catalog page * add to side bar * add snippet of bigtable cluster * Update website/www/site/content/en/documentation/transforms/python/elementwise/enrichment.md Co-authored-by: Rebecca Szper <[email protected]> * Update website/www/site/content/en/documentation/transforms/python/elementwise/enrichment.md Co-authored-by: Rebecca Szper <[email protected]> * Update website/www/site/content/en/documentation/transforms/python/elementwise/enrichment.md Co-authored-by: Rebecca Szper <[email protected]> * update context from review * add enrichment-notebook link * Update website/www/site/content/en/documentation/transforms/python/elementwise/enrichment.md Co-authored-by: Rebecca Szper <[email protected]> * Update website/www/site/content/en/documentation/transforms/python/elementwise/enrichment.md Co-authored-by: Rebecca Szper <[email protected]> --------- Co-authored-by: Rebecca Szper <[email protected]>
- Loading branch information
1 parent
bfe6168
commit 914cf14
Showing
7 changed files
with
175 additions
and
2 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
sdks/python/apache_beam/examples/snippets/transforms/elementwise/enrichment.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# coding=utf-8 | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You 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. | ||
# | ||
|
||
# pytype: skip-file | ||
# pylint: disable=line-too-long | ||
|
||
|
||
def enrichment_with_bigtable(): | ||
# [START enrichment_with_bigtable] | ||
import apache_beam as beam | ||
from apache_beam.transforms.enrichment import Enrichment | ||
from apache_beam.transforms.enrichment_handlers.bigtable import BigTableEnrichmentHandler | ||
|
||
project_id = 'apache-beam-testing' | ||
instance_id = 'beam-test' | ||
table_id = 'bigtable-enrichment-test' | ||
row_key = 'product_id' | ||
|
||
data = [ | ||
beam.Row(sale_id=1, customer_id=1, product_id=1, quantity=1), | ||
beam.Row(sale_id=3, customer_id=3, product_id=2, quantity=3), | ||
beam.Row(sale_id=5, customer_id=5, product_id=4, quantity=2) | ||
] | ||
|
||
bigtable_handler = BigTableEnrichmentHandler( | ||
project_id=project_id, | ||
instance_id=instance_id, | ||
table_id=table_id, | ||
row_key=row_key) | ||
with beam.Pipeline() as p: | ||
_ = ( | ||
p | ||
| "Create" >> beam.Create(data) | ||
| "Enrich W/ BigTable" >> Enrichment(bigtable_handler) | ||
| "Print" >> beam.Map(print)) | ||
# [END enrichment_with_bigtable] |
53 changes: 53 additions & 0 deletions
53
sdks/python/apache_beam/examples/snippets/transforms/elementwise/enrichment_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# coding=utf-8 | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You 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. | ||
# | ||
# pytype: skip-file | ||
# pylint: disable=line-too-long | ||
|
||
import unittest | ||
from io import StringIO | ||
|
||
import mock | ||
|
||
# pylint: disable=unused-import | ||
try: | ||
from apache_beam.examples.snippets.transforms.elementwise.enrichment import enrichment_with_bigtable | ||
from apache_beam.io.requestresponse import RequestResponseIO | ||
except ImportError: | ||
raise unittest.SkipTest('RequestResponseIO dependencies are not installed') | ||
|
||
|
||
def validate_enrichment_with_bigtable(): | ||
expected = '''[START enrichment_with_bigtable] | ||
Row(sale_id=1, customer_id=1, product_id=1, quantity=1, product={'product_id': '1', 'product_name': 'pixel 5', 'product_stock': '2'}) | ||
Row(sale_id=3, customer_id=3, product_id=2, quantity=3, product={'product_id': '2', 'product_name': 'pixel 6', 'product_stock': '4'}) | ||
Row(sale_id=5, customer_id=5, product_id=4, quantity=2, product={'product_id': '4', 'product_name': 'pixel 8', 'product_stock': '10'}) | ||
[END enrichment_with_bigtable]'''.splitlines()[1:-1] | ||
return expected | ||
|
||
|
||
@mock.patch('sys.stdout', new_callable=StringIO) | ||
class EnrichmentTest(unittest.TestCase): | ||
def test_enrichment_with_bigtable(self, mock_stdout): | ||
enrichment_with_bigtable() | ||
output = mock_stdout.getvalue().splitlines() | ||
expected = validate_enrichment_with_bigtable() | ||
self.assertEqual(output, expected) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...e/www/site/content/en/documentation/transforms/python/elementwise/enrichment.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
--- | ||
title: "Enrichment" | ||
--- | ||
<!-- | ||
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. | ||
--> | ||
|
||
# Enrichment transform | ||
|
||
{{< localstorage language language-py >}} | ||
|
||
<table> | ||
<tr> | ||
<td> | ||
<a> | ||
{{< button-pydoc path="apache_beam.transforms" class="Enrichment" >}} | ||
</a> | ||
</td> | ||
</tr> | ||
</table> | ||
|
||
|
||
The enrichment transform lets you dynamically enrich data in a pipeline by doing a key-value lookup to a remote service. The transform uses [`RequestResponeIO`](https://beam.apache.org/releases/pydoc/current/apache_beam.io.requestresponseio.html#apache_beam.io.requestresponseio.RequestResponseIO) internally. This feature uses client-side throttling to ensure that the remote service isn't overloaded with requests. If service-side errors occur, like `TooManyRequests` and `Timeout` exceptions, it retries the requests by using exponential backoff. | ||
|
||
In Apache Beam 2.54.0 and later versions, the transform includes a built-in enrichment handler for [Bigtable](https://cloud.google.com/bigtable/docs/overview). | ||
|
||
## Use Bigtable to enrich data | ||
|
||
The following example demonstrates how to create a pipeline that use the enrichment transform with [`BigTableEnrichmentHandler`](https://beam.apache.org/releases/pydoc/current/apache_beam.transforms.enrichment_handlers.bigtable.html#apache_beam.transforms.enrichment_handlers.bigtable.BigTableEnrichmentHandler). | ||
|
||
The data stored in the Bigtable cluster uses the following format: | ||
|
||
| Row key | product:product_id | product:product_name | product:product_stock | | ||
|:---------:|:--------------------:|:----------------------:|:-----------------------:| | ||
| 1 | 1 | pixel 5 | 2 | | ||
| 2 | 2 | pixel 6 | 4 | | ||
| 3 | 3 | pixel 7 | 20 | | ||
| 4 | 4 | pixel 8 | 10 | | ||
|
||
|
||
{{< highlight language="py" >}} | ||
{{< code_sample "sdks/python/apache_beam/examples/snippets/transforms/elementwise/enrichment.py" enrichment_with_bigtable >}} | ||
{{</ highlight >}} | ||
|
||
{{< paragraph class="notebook-skip" >}} | ||
Output: | ||
{{< /paragraph >}} | ||
{{< highlight class="notebook-skip" >}} | ||
{{< code_sample "sdks/python/apache_beam/examples/snippets/transforms/elementwise/enrichment_test.py" enrichment_with_bigtable >}} | ||
{{< /highlight >}} | ||
|
||
## Related transforms | ||
|
||
Not applicable. | ||
|
||
{{< button-pydoc path="apache_beam.transforms" class="Enrichment" >}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters