You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "liferoad (via GitHub)" <gi...@apache.org> on 2023/04/24 21:16:17 UTC

[GitHub] [beam] liferoad commented on a diff in pull request #25969: Add the example to learn transforms

liferoad commented on code in PR #25969:
URL: https://github.com/apache/beam/pull/25969#discussion_r1175789554


##########
examples/notebooks/get-started/learn_beam_transforms_by_doing.ipynb:
##########
@@ -0,0 +1,740 @@
+{
+  "nbformat": 4,
+  "nbformat_minor": 0,
+  "metadata": {
+    "colab": {
+      "provenance": []
+    },
+    "kernelspec": {
+      "name": "python3",
+      "display_name": "Python 3"
+    },
+    "language_info": {
+      "name": "python"
+    }
+  },
+  "cells": [
+    {
+      "cell_type": "code",
+      "source": [
+        "#@title ###### Licensed to the Apache Software Foundation (ASF), Version 2.0 (the \"License\")\n",
+        "\n",
+        "# Licensed to the Apache Software Foundation (ASF) under one\n",
+        "# or more contributor license agreements. See the NOTICE file\n",
+        "# distributed with this work for additional information\n",
+        "# regarding copyright ownership. The ASF licenses this file\n",
+        "# to you under the Apache License, Version 2.0 (the\n",
+        "# \"License\"); you may not use this file except in compliance\n",
+        "# with the License. You may obtain a copy of the License at\n",
+        "#\n",
+        "#   http://www.apache.org/licenses/LICENSE-2.0\n",
+        "#\n",
+        "# Unless required by applicable law or agreed to in writing,\n",
+        "# software distributed under the License is distributed on an\n",
+        "# \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n",
+        "# KIND, either express or implied. See the License for the\n",
+        "# specific language governing permissions and limitations\n",
+        "# under the License."
+      ],
+      "metadata": {
+        "cellView": "form",
+        "id": "QgmD1wbmT4mj"
+      },
+      "execution_count": null,
+      "outputs": []
+    },
+    {
+      "cell_type": "markdown",
+      "source": [
+        "# Learn Beam PTransforms\n",
+        "\n",
+        "After this notebook, you should be able to:\n",
+        "1. Use user-defined functions in your `PTransforms`\n",
+        "2. Learn Beam SDK composite transforms\n",
+        "3. Create you own composite transforms to simplify your `Pipeline`\n",
+        "\n",
+        "For basic Beam `PTransforms`, please check out [this Notebook](https://colab.research.google.com/github/apache/beam/blob/master/examples/notebooks/get-started/learn_beam_basics_by_doing.ipynb).\n",
+        "\n",
+        "Beam Python SDK also provides [a list of built-in transforms](https://beam.apache.org/documentation/transforms/python/overview/).\n"
+      ],
+      "metadata": {
+        "id": "RuUHlGZjVt6W"
+      }
+    },
+    {
+      "cell_type": "markdown",
+      "source": [
+        "## How To Approach This Tutorial\n",
+        "\n",
+        "This tutorial was designed for someone who likes to learn by doing. There will be code cells where you can write your own code to test your understanding.\n",

Review Comment:
   Done.



##########
examples/notebooks/get-started/learn_beam_transforms_by_doing.ipynb:
##########
@@ -0,0 +1,740 @@
+{
+  "nbformat": 4,
+  "nbformat_minor": 0,
+  "metadata": {
+    "colab": {
+      "provenance": []
+    },
+    "kernelspec": {
+      "name": "python3",
+      "display_name": "Python 3"
+    },
+    "language_info": {
+      "name": "python"
+    }
+  },
+  "cells": [
+    {
+      "cell_type": "code",
+      "source": [
+        "#@title ###### Licensed to the Apache Software Foundation (ASF), Version 2.0 (the \"License\")\n",
+        "\n",
+        "# Licensed to the Apache Software Foundation (ASF) under one\n",
+        "# or more contributor license agreements. See the NOTICE file\n",
+        "# distributed with this work for additional information\n",
+        "# regarding copyright ownership. The ASF licenses this file\n",
+        "# to you under the Apache License, Version 2.0 (the\n",
+        "# \"License\"); you may not use this file except in compliance\n",
+        "# with the License. You may obtain a copy of the License at\n",
+        "#\n",
+        "#   http://www.apache.org/licenses/LICENSE-2.0\n",
+        "#\n",
+        "# Unless required by applicable law or agreed to in writing,\n",
+        "# software distributed under the License is distributed on an\n",
+        "# \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n",
+        "# KIND, either express or implied. See the License for the\n",
+        "# specific language governing permissions and limitations\n",
+        "# under the License."
+      ],
+      "metadata": {
+        "cellView": "form",
+        "id": "QgmD1wbmT4mj"
+      },
+      "execution_count": null,
+      "outputs": []
+    },
+    {
+      "cell_type": "markdown",
+      "source": [
+        "# Learn Beam PTransforms\n",
+        "\n",
+        "After this notebook, you should be able to:\n",
+        "1. Use user-defined functions in your `PTransforms`\n",
+        "2. Learn Beam SDK composite transforms\n",
+        "3. Create you own composite transforms to simplify your `Pipeline`\n",
+        "\n",
+        "For basic Beam `PTransforms`, please check out [this Notebook](https://colab.research.google.com/github/apache/beam/blob/master/examples/notebooks/get-started/learn_beam_basics_by_doing.ipynb).\n",
+        "\n",
+        "Beam Python SDK also provides [a list of built-in transforms](https://beam.apache.org/documentation/transforms/python/overview/).\n"
+      ],
+      "metadata": {
+        "id": "RuUHlGZjVt6W"
+      }
+    },
+    {
+      "cell_type": "markdown",
+      "source": [
+        "## How To Approach This Tutorial\n",
+        "\n",
+        "This tutorial was designed for someone who likes to learn by doing. There will be code cells where you can write your own code to test your understanding.\n",
+        "\n",
+        "As such, to get the most out of this tutorial, we strongly recommend typing code by hand as you’re working through the tutorial and not using copy/paste. This will help you develop muscle memory and a stronger understanding."
+      ],
+      "metadata": {
+        "id": "Ldx0Z7nWGopE"
+      }
+    },
+    {
+      "cell_type": "markdown",
+      "source": [
+        "To begin, run the cell below to install and import Apache Beam."
+      ],
+      "metadata": {
+        "id": "jy1zaj4NDE0T"
+      }
+    },
+    {
+      "cell_type": "code",
+      "execution_count": null,
+      "metadata": {
+        "id": "pNure-fW8hl3"
+      },
+      "outputs": [],
+      "source": [
+        "# Run a shell command and import beam\n",
+        "!pip install --quiet apache-beam\n",
+        "import apache_beam as beam\n",
+        "beam.__version__"
+      ]
+    },
+    {
+      "cell_type": "code",
+      "source": [
+        "# Set the logging level to reduce verbose information\n",
+        "import logging\n",
+        "\n",
+        "logging.root.setLevel(logging.ERROR)"
+      ],
+      "metadata": {
+        "id": "vyksB2VMtv3m"
+      },
+      "execution_count": null,
+      "outputs": []
+    },
+    {
+      "cell_type": "markdown",
+      "source": [
+        "\n",
+        "\n",
+        "---\n",
+        "\n",
+        "\n",
+        "\n",
+        "---\n",
+        "\n"
+      ],
+      "metadata": {
+        "id": "M1ku4nX_Gutb"
+      }
+    },
+    {
+      "cell_type": "markdown",
+      "source": [
+        "## 1. Simple User-Defined Function (UDF)\n",
+        "\n",
+        "Some `PTransforms` allow you to run your own functions and user-defined code to specify how your transform is applied. For example, the below `CombineGlobally` transform,"
+      ],
+      "metadata": {
+        "id": "0qDeT34SS1_8"
+      }
+    },
+    {
+      "cell_type": "code",
+      "source": [
+        "pc = [1, 10, 100, 1000]\n",
+        "\n",
+        "# User-defined function\n",
+        "def bounded_sum(values, bound=500):\n",
+        "  return min(sum(values), bound)\n",
+        "\n",
+        "small_sum = pc | beam.CombineGlobally(bounded_sum)  # [500]\n",
+        "large_sum = pc | beam.CombineGlobally(bounded_sum, bound=5000)  # [1111]\n",
+        "\n",
+        "print(small_sum, large_sum)"
+      ],
+      "metadata": {
+        "id": "UZTWBGZ0TQWF"
+      },
+      "execution_count": null,
+      "outputs": []
+    },
+    {
+      "cell_type": "markdown",
+      "source": [
+        "## 2. Transforms: ParDo and Combine\n",
+        "\n",
+        "A `ParDo` transform considers each element in the input `PCollection`, performs your user code to process each element, and emits zero, one, or multiple elements to an output `PCollection`. `Combine` is another Beam transform for combining collections of elements or values in your data.\n",
+        "Both allow flexible UDF to define how you process the data."

Review Comment:
   Done.



-- 
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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org