blob: dcff67dba4be80a1c22d3372c97557bfd81e44c9 [file] [log] [blame]
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>apache_beam.transforms.sql &mdash; Apache Beam 2.47.0 documentation</title>
<script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
<script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
<script type="text/javascript" src="../../../_static/jquery.js"></script>
<script type="text/javascript" src="../../../_static/underscore.js"></script>
<script type="text/javascript" src="../../../_static/doctools.js"></script>
<script type="text/javascript" src="../../../_static/language_data.js"></script>
<script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/javascript" src="../../../_static/js/theme.js"></script>
<link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="../../../index.html" class="icon icon-home"> Apache Beam
</a>
<div class="version">
2.47.0
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.coders.html">apache_beam.coders package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.dataframe.html">apache_beam.dataframe package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.io.html">apache_beam.io package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.metrics.html">apache_beam.metrics package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.ml.html">apache_beam.ml package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.options.html">apache_beam.options package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.portability.html">apache_beam.portability package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.runners.html">apache_beam.runners package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.testing.html">apache_beam.testing package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.transforms.html">apache_beam.transforms package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.typehints.html">apache_beam.typehints package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.utils.html">apache_beam.utils package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.yaml.html">apache_beam.yaml package</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.error.html">apache_beam.error module</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.pipeline.html">apache_beam.pipeline module</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.pvalue.html">apache_beam.pvalue module</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../../../index.html">Apache Beam</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../../../index.html">Docs</a> &raquo;</li>
<li><a href="../../index.html">Module code</a> &raquo;</li>
<li>apache_beam.transforms.sql</li>
<li class="wy-breadcrumbs-aside">
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<h1>Source code for apache_beam.transforms.sql</h1><div class="highlight"><pre>
<span></span><span class="c1">#</span>
<span class="c1"># Licensed to the Apache Software Foundation (ASF) under one or more</span>
<span class="c1"># contributor license agreements. See the NOTICE file distributed with</span>
<span class="c1"># this work for additional information regarding copyright ownership.</span>
<span class="c1"># The ASF licenses this file to You under the Apache License, Version 2.0</span>
<span class="c1"># (the &quot;License&quot;); you may not use this file except in compliance with</span>
<span class="c1"># the License. You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1"># http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1">#</span>
<span class="sd">&quot;&quot;&quot;Package for SqlTransform and related classes.&quot;&quot;&quot;</span>
<span class="c1"># pytype: skip-file</span>
<span class="kn">import</span> <span class="nn">typing</span>
<span class="kn">from</span> <span class="nn">apache_beam.transforms.external</span> <span class="kn">import</span> <span class="n">BeamJarExpansionService</span>
<span class="kn">from</span> <span class="nn">apache_beam.transforms.external</span> <span class="kn">import</span> <span class="n">ExternalTransform</span>
<span class="kn">from</span> <span class="nn">apache_beam.transforms.external</span> <span class="kn">import</span> <span class="n">NamedTupleBasedPayloadBuilder</span>
<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;SqlTransform&#39;</span><span class="p">]</span>
<span class="n">SqlTransformSchema</span> <span class="o">=</span> <span class="n">typing</span><span class="o">.</span><span class="n">NamedTuple</span><span class="p">(</span>
<span class="s1">&#39;SqlTransformSchema&#39;</span><span class="p">,</span> <span class="p">[(</span><span class="s1">&#39;query&#39;</span><span class="p">,</span> <span class="nb">str</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;dialect&#39;</span><span class="p">,</span> <span class="n">typing</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">])])</span>
<div class="viewcode-block" id="SqlTransform"><a class="viewcode-back" href="../../../apache_beam.transforms.sql.html#apache_beam.transforms.sql.SqlTransform">[docs]</a><span class="k">class</span> <span class="nc">SqlTransform</span><span class="p">(</span><span class="n">ExternalTransform</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;A transform that can translate a SQL query into PTransforms.</span>
<span class="sd"> Input PCollections must have a schema. Currently, there are two ways to define</span>
<span class="sd"> a schema for a PCollection:</span>
<span class="sd"> 1) Register a `typing.NamedTuple` type to use RowCoder, and specify it as the</span>
<span class="sd"> output type. For example::</span>
<span class="sd"> Purchase = typing.NamedTuple(&#39;Purchase&#39;,</span>
<span class="sd"> [(&#39;item_name&#39;, unicode), (&#39;price&#39;, float)])</span>
<span class="sd"> coders.registry.register_coder(Purchase, coders.RowCoder)</span>
<span class="sd"> with Pipeline() as p:</span>
<span class="sd"> purchases = (p | beam.io...</span>
<span class="sd"> | beam.Map(..).with_output_types(Purchase))</span>
<span class="sd"> 2) Produce `beam.Row` instances. Note this option will fail if Beam is unable</span>
<span class="sd"> to infer data types for any of the fields. For example::</span>
<span class="sd"> with Pipeline() as p:</span>
<span class="sd"> purchases = (p | beam.io...</span>
<span class="sd"> | beam.Map(lambda x: beam.Row(item_name=unicode(..),</span>
<span class="sd"> price=float(..))))</span>
<span class="sd"> Similarly, the output of SqlTransform is a PCollection with a schema.</span>
<span class="sd"> The columns produced by the query can be accessed as attributes. For example::</span>
<span class="sd"> purchases | SqlTransform(\&quot;\&quot;\&quot;</span>
<span class="sd"> SELECT item_name, COUNT(*) AS `count`</span>
<span class="sd"> FROM PCOLLECTION GROUP BY item_name\&quot;\&quot;\&quot;)</span>
<span class="sd"> | beam.Map(lambda row: &quot;We&#39;ve sold %d %ss!&quot; % (row.count,</span>
<span class="sd"> row.item_name))</span>
<span class="sd"> Additional examples can be found in</span>
<span class="sd"> `apache_beam.examples.wordcount_xlang_sql`, `apache_beam.examples.sql_taxi`,</span>
<span class="sd"> and `apache_beam.transforms.sql_test`.</span>
<span class="sd"> For more details about Beam SQL in general see the `Java transform</span>
<span class="sd"> &lt;https://beam.apache.org/releases/javadoc/current/org/apache/beam/sdk/extensions/sql/SqlTransform.html&gt;`_,</span>
<span class="sd"> and the `documentation</span>
<span class="sd"> &lt;https://beam.apache.org/documentation/dsls/sql/overview/&gt;`_.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">URN</span> <span class="o">=</span> <span class="s1">&#39;beam:external:java:sql:v1&#39;</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">query</span><span class="p">,</span> <span class="n">dialect</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">expansion_service</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Creates a SqlTransform which will be expanded to Java&#39;s SqlTransform.</span>
<span class="sd"> (See class docs).</span>
<span class="sd"> :param query: The SQL query.</span>
<span class="sd"> :param dialect: (optional) The dialect, e.g. use &#39;zetasql&#39; for ZetaSQL.</span>
<span class="sd"> :param expansion_service: (optional) The URL of the expansion service to use</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">expansion_service</span> <span class="o">=</span> <span class="n">expansion_service</span> <span class="ow">or</span> <span class="n">BeamJarExpansionService</span><span class="p">(</span>
<span class="s1">&#39;:sdks:java:extensions:sql:expansion-service:shadowJar&#39;</span><span class="p">)</span>
<span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span>
<span class="bp">self</span><span class="o">.</span><span class="n">URN</span><span class="p">,</span>
<span class="n">NamedTupleBasedPayloadBuilder</span><span class="p">(</span>
<span class="n">SqlTransformSchema</span><span class="p">(</span><span class="n">query</span><span class="o">=</span><span class="n">query</span><span class="p">,</span> <span class="n">dialect</span><span class="o">=</span><span class="n">dialect</span><span class="p">)),</span>
<span class="n">expansion_service</span><span class="o">=</span><span class="n">expansion_service</span><span class="p">)</span></div>
</pre></div>
</div>
</div>
<footer>
<hr/>
<div role="contentinfo">
<p>
&copy; Copyright
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>