andyxiexu commented on a change in pull request #15518:
URL: https://github.com/apache/beam/pull/15518#discussion_r713275461



##########
File path: sdks/python/apache_beam/runners/interactive/examples/Run Beam SQL 
with beam_sql magic.ipynb
##########
@@ -0,0 +1,548 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "id": "a999c0fd",
+   "metadata": {},
+   "source": [
+    "Licensed under the Apache License, 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.\n",
+    "-->\n"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "5562c11a",
+   "metadata": {},
+   "source": [
+    "# Run Beam SQL in notebooks\n",
+    "\n",
+    "[Beam SQL](https://beam.apache.org/documentation/dsls/sql/overview/) 
allows a Beam user to query PCollections with SQL statements. Currently, 
`InteractiveRunner` does not support `SqlTransform` due to 
[BEAM-10708](https://issues.apache.org/jira/browse/BEAM-10708). However, a user 
could use the `beam_sql` magic to run Beam SQL in the notebook and introspect 
the result. It's a convenient way to validate your queries locally against 
known/test data sources when prototyping a Beam pipeline with SQL, before 
productionizing it on remote cluster/services. \n",
+    "\n",
+    "First, let's load the `beam_sql` magic:"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "8928343a",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "%load_ext apache_beam.runners.interactive.sql.beam_sql_magics"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "dec39a04",
+   "metadata": {},
+   "source": [
+    "Since SQL support in Beam Python SDK is implemented through xLang 
external transform, make sure you have below prerequisites:\n",
+    "- Have `docker` installed;\n",
+    "- Have jdk8 or jdk11 installed and $JAVA_HOME set;"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "fa9cede3",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "!docker image list\n",
+    "!java --version\n",
+    "!echo $JAVA_HOME"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "640a427e",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "# Optionally sets the logging level to reduce distraction.\n",
+    "import logging\n",
+    "\n",
+    "logging.root.setLevel(logging.ERROR)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "3ab6438a",
+   "metadata": {},
+   "source": [
+    "**Important**: if you're using Beam built from your local source code, 
additionally:\n",
+    "\n",
+    "- Have the Java expansion service shadowjar built. Go to the root 
directory of your local beam repo and then execute:\n",
+    "  `./gradlew :sdks:java:extensions:sql:expansion-service:shadowJar`;\n",
+    "- Based on your jdk version, pull the docker image `docker pull 
apache/beam_java11_sdk` or `docker pull apache/beam_java8_sdk`.\n",
+    "- Then tag the image with your current Beam dev version.  You can check 
the dev version under `apache_beam.version.__version__`. For example, if you're 
using jdk11 and dev version is `x.x.x.dev`, execute `docker image tag 
apache/beam_java11_sdk:latest apache/beam_java11_sdk:x.x.x.dev`."
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "bcf28852",
+   "metadata": {},
+   "source": [
+    "## Query#1 - A simple static query\n",
+    "`beam_sql` is an IPython [custom 
magic](https://ipython.readthedocs.io/en/stable/config/custommagics.html). If 
you're not familiar with magics, here are some [built-in 
examples](https://ipython.readthedocs.io/en/stable/interactive/magics.html).\n",
+    "\n",
+    "The `beam_sql` magic can be used as either a line magic or a cell 
magic.\n",
+    "We can check its usage by running:"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "5777a84e",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "%beam_sql -h"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "20f88c5d",
+   "metadata": {},
+   "source": [
+    "We can run a simple SQL query (in Apache Calcite SQL 
[syntax](https://beam.apache.org/documentation/dsls/sql/calcite/query-syntax/)) 
to create a [schema-aware 
PCollection](https://beam.apache.org/documentation/programming-guide/#schemas) 
from static values."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "49c70de3",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "%%beam_sql -o query1_data\n",
+    "SELECT CAST(5 AS INT) AS `id`, CAST('foo' AS VARCHAR) AS `str`, CAST(3.14 
AS DOUBLE) AS `flt`"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "6a1c5e9e",
+   "metadata": {},
+   "source": [
+    "The `beam_sql` magic shows you the result of the SQL query.\n",
+    "\n",
+    "It also creates and outputs a PCollection named `query1_data` with 
element_type like `BeamSchema_...(id: int32, str: str)`.\n",
+    "\n",
+    "Note that we have **not** explicitly created a Beam pipeline. We get a 
PCollection because the `beam_sql` magic always **implicitly creates** a 
pipeline to execute your SQL query. To hold the elements with each field's type 
info, Beam automatically creates a schema as the element_type for the created 
PCollection."
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "05d2b688",
+   "metadata": {},
+   "source": [
+    "To introspect the data again with more knobs, we can use `show`."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "89220b13",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "from apache_beam.runners.interactive import interactive_beam as ib\n",
+    "ib.show(query1_data)\n",
+    "# Uncomment below to set more args.\n",
+    "# ib.show(query1_data, visualize_data=True, include_window_info=True)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "6faa7df8",
+   "metadata": {},
+   "source": [
+    "To materialize the PCollection into a pandas DataFrame object, we can use 
`collect`."

Review comment:
       Maybe add a link to pandas DataFrame object? I have no idea what is that.




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to