You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2020/09/24 17:13:29 UTC
[GitHub] [beam] davidcavazos commented on a change in pull request #12892: [BEAM-10937] Add first introductory notebook
davidcavazos commented on a change in pull request #12892:
URL: https://github.com/apache/beam/pull/12892#discussion_r494480642
##########
File path: examples/notebooks/tour-of-beam/getting-started.ipynb
##########
@@ -0,0 +1,1021 @@
+{
+ "license": [
+ "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."
+ ],
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "name": "Getting started -- Tour of Beam",
+ "provenance": [],
+ "toc_visible": true,
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "<a href=\"https://colab.research.google.com/github/apache/beam/blob/master/examples/notebooks/tour-of-beam/getting-started.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "5UC_aGanx6oE",
+ "colab_type": "text"
+ },
+ "source": [
+ "# Getting started -- *Tour of Beam*\n",
+ "\n",
+ "[Apache Beam](https://beam.apache.org/get-started/beam-overview/)\n",
+ "is a library for parallel data processing.\n",
+ "\n",
+ "Beam is commonly used for\n",
+ "[Extract-Transform-Load (ETL)](https://en.wikipedia.org/wiki/Extract,_transform,_load)\n",
+ "jobs, where we _extract_ data from a data source, _transform_ that data, and _load_ it into a data sink like a database.\n",
+ "It does particularly well with large amounts of data since it can use mutliple machines to process everything at the same time.\n",
+ "\n",
+ "Let's begin by installing the `apache-beam` package with `pip`."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "R_Yhoc6N_Flg",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "# Install apache-beam with pip.\n",
+ "!pip install --quiet apache-beam"
+ ],
+ "execution_count": 25,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "WwxLB5EiVKP_",
+ "colab_type": "text"
+ },
+ "source": [
+ "You can express a _data processing pipeline_, and then run it on the\n",
+ "[_runner_ of your choice](https://beam.apache.org/documentation/runners/capability-matrix/).\n",
+ "For now, we use the `DirectRunner` which runs locally for simplicity."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "8rfQeLk2y9lx",
+ "colab_type": "text"
+ },
+ "source": [
+ "# What is a _pipeline_?\n",
+ "\n",
+ "A __pipeline__ is a __sequence of data transformations__.\n",
+ "You can think of it like a production line,\n",
+ "data comes in from one end,\n",
+ "it gets transformed by each step.\n",
+ "The outputs from one step are passed as inputs to the next step.\n",
+ "\n",
+ "In Beam, your data lives in a __`PCollection`__,\n",
+ "which stands for _Parallel Collection_.\n",
+ "A `PCollection` is like a __list of elements__,\n",
+ "but without any order guarantees.\n",
+ "This allows Beam to easily parallelize and distribute\n",
+ "the `PCollection`'s elements."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "81VNY478gep_",
+ "colab_type": "text"
+ },
+ "source": [
+ ""
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "LaljqKvs4UU5",
+ "colab_type": "text"
+ },
+ "source": [
+ "Once you have your data, the next step is to transform it.\n",
+ "In Beam, you transform data using **`PTransform`**s,\n",
+ "which stands for _Parallel Transform_.\n",
+ "A `PTransform` is like a __function__,\n",
+ "they take some inputs, transform them and create some outputs."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "Me_hvd6XgtPC",
+ "colab_type": "text"
+ },
+ "source": [
+ ""
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "SVk4BsuKVlLj",
+ "colab_type": "text"
+ },
+ "source": [
+ "Now let's dive into creating our first pipeline.\n",
+ "\n",
+ "For this first pipeline, let's just feed it some data from a Python list and print the results.\n",
+ "\n",
+ "Each _step_ in the pipeline is delimited by the _pipe operator_ `|`.\n",
+ "The outputs of each transform are passed to the next transform as inputs.\n",
+ "And we can save the final results into a `PCollection` variable.\n",
+ "\n",
+ "```py\n",
+ "# We pass the elements from step1 through step3 and save the results into `outputs`.\n",
+ "outputs = pipeline | step1 | step2 | step3\n",
+ "```\n",
+ "\n",
+ "Pipelines can quickly grow long, so it's sometimes easier to read if we surround them with parentheses and break them into multiple lines.\n",
+ "\n",
+ "```py\n",
+ "# This is equivalent to the example above.\n",
+ "outputs = (\n",
+ " pipeline\n",
+ " | step1\n",
+ " | step2\n",
+ " | step3\n",
+ ")\n",
+ "```\n",
+ "\n",
+ "Also, Beam expects each transform, or step, to have a unique _label_, or description.\n",
+ "This makes it a lot easier to debug, and it's in general a good practice to start.\n",
+ "You can use the _right shift operator_ `>>` to add a label to your transforms, like `'My description' >> MyTransform`.\n",
+ "\n",
+ "```py\n",
+ "# Try to give short but descriptive labels.\n",
+ "# These serve both as comments and help debug later on.\n",
+ "outputs = (\n",
+ " pipeline\n",
+ " | 'First step' >> step1\n",
+ " | 'Second step' >> step2\n",
+ " | 'Third step' >> step3\n",
+ ")\n",
+ "```\n",
+ "\n",
+ "> ℹ️ The syntax might seem a little strange at first, but you'll get used to it.\n",
+ "> Once you're familiar with it, you'll see it's actually easy to read and write.\n",
Review comment:
Thank you for the suggestion!
----------------------------------------------------------------
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:
users@infra.apache.org