[ https://issues.apache.org/jira/browse/BEAM-7389?focusedWorklogId=293381&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-293381 ]
ASF GitHub Bot logged work on BEAM-7389: ---------------------------------------- Author: ASF GitHub Bot Created on: 12/Aug/19 21:08 Start Date: 12/Aug/19 21:08 Worklog Time Spent: 10m Work Description: davidcavazos commented on pull request #9260: [BEAM-7389] Add code examples for FlatMap page URL: https://github.com/apache/beam/pull/9260#discussion_r313129117 ########## File path: website/src/documentation/transforms/python/element-wise/flatmap.md ########## @@ -19,24 +19,287 @@ limitations under the License. --> # FlatMap -<table align="left"> - <a target="_blank" class="button" + +<script type="text/javascript"> +localStorage.setItem('language', 'language-py') +</script> + +<table> + <td> + <a class="button" target="_blank" href="https://beam.apache.org/releases/pydoc/current/apache_beam.transforms.core.html#apache_beam.transforms.core.FlatMap"> - <img src="https://beam.apache.org/images/logos/sdks/python.png" width="20px" height="20px" - alt="Pydoc" /> - Pydoc + <img src="https://beam.apache.org/images/logos/sdks/python.png" + width="20px" height="20px" alt="Pydoc" /> + Pydoc </a> + </td> </table> <br> + Applies a simple 1-to-many mapping function over each element in the collection. The many elements are flattened into the resulting collection. ## Examples -See [BEAM-7389](https://issues.apache.org/jira/browse/BEAM-7389) for updates. -## Related transforms +In the following examples, we create a pipeline with a `PCollection` of produce their icon, name, and duration. +Then, we apply `FlatMap` in multiple ways to yield zero or more elements per each input element into the resulting `PCollection`. + +`FlatMap` accepts a function that returns an `iterable`, +where each of the output `iterable`'s elements is an element of the resulting `PCollection`. + +### Example 1: FlatMap with a predefined function + +We use the function `str.split` which takes a single `str` element and outputs a `list` of `str`s. +This pipeline splits the input element using whitespaces, creating a list of zero or more elements. + +```py +{% github_sample /apache/beam/blob/master/sdks/python/apache_beam/examples/snippets/transforms/element_wise/flat_map.py tag:flat_map_simple %}``` + +Output `PCollection` after `FlatMap`: + +``` +{% github_sample /apache/beam/blob/master/sdks/python/apache_beam/examples/snippets/transforms/element_wise/flat_map_test.py tag:plants %}``` + +<table> + <td> + <a class="button" target="_blank" + href="https://github.com/apache/beam/blob/master/sdks/python/apache_beam/examples/snippets/transforms/element_wise/flat_map.py"> + <img src="https://www.tensorflow.org/images/GitHub-Mark-32px.png" + width="20px" height="20px" alt="View on GitHub" /> + View on GitHub + </a> + </td> +</table> +<br> + +### Example 2: FlatMap with a function + +We define a function `split_words` which splits an input `str` element using the delimiter `','` and outputs a `list` of `str`s. + +```py +{% github_sample /apache/beam/blob/master/sdks/python/apache_beam/examples/snippets/transforms/element_wise/flat_map.py tag:flat_map_function %}``` + +Output `PCollection` after `FlatMap`: + +``` +{% github_sample /apache/beam/blob/master/sdks/python/apache_beam/examples/snippets/transforms/element_wise/flat_map_test.py tag:plants %}``` + +<table> + <td> + <a class="button" target="_blank" + href="https://github.com/apache/beam/blob/master/sdks/python/apache_beam/examples/snippets/transforms/element_wise/flat_map.py"> + <img src="https://www.tensorflow.org/images/GitHub-Mark-32px.png" + width="20px" height="20px" alt="View on GitHub" /> + View on GitHub + </a> + </td> +</table> +<br> + +### Example 3: FlatMap with a lambda function + +For this example, we want to flatten a `PCollection` of lists of `str`s into a `PCollection` of `str`s. +Each input element is already an `iterable`, where each element is what we want in the resulting `PCollection`. +We use a lambda function that returns the same input element it received. + +```py +{% github_sample /apache/beam/blob/master/sdks/python/apache_beam/examples/snippets/transforms/element_wise/flat_map.py tag:flat_map_lambda %}``` + +Output `PCollection` after `FlatMap`: + +``` +{% github_sample /apache/beam/blob/master/sdks/python/apache_beam/examples/snippets/transforms/element_wise/flat_map_test.py tag:plants %}``` + +<table> + <td> + <a class="button" target="_blank" + href="https://github.com/apache/beam/blob/master/sdks/python/apache_beam/examples/snippets/transforms/element_wise/flat_map.py"> + <img src="https://www.tensorflow.org/images/GitHub-Mark-32px.png" + width="20px" height="20px" alt="View on GitHub" /> + View on GitHub + </a> + </td> +</table> +<br> + +### Example 4: FlatMap with a generator + +For this example, we want to flatten a `PCollection` of lists of `str`s into a `PCollection` of `str`s. +We use a generator to iterate over the input list and yield each of the elements. +Each yielded result in the generator will be an element in the resulting `PCollection`. Review comment: Thanks, fixing ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org Issue Time Tracking ------------------- Worklog Id: (was: 293381) Time Spent: 40h 40m (was: 40.5h) > Colab examples for element-wise transforms (Python) > --------------------------------------------------- > > Key: BEAM-7389 > URL: https://issues.apache.org/jira/browse/BEAM-7389 > Project: Beam > Issue Type: Improvement > Components: website > Reporter: Rose Nguyen > Assignee: David Cavazos > Priority: Minor > Time Spent: 40h 40m > Remaining Estimate: 0h > -- This message was sent by Atlassian JIRA (v7.6.14#76016)