http://git-wip-us.apache.org/repos/asf/incubator-airflow-site/blob/11437c14/_modules/airflow/hooks/S3_hook.html ---------------------------------------------------------------------- diff --git a/_modules/airflow/hooks/S3_hook.html b/_modules/airflow/hooks/S3_hook.html index f1c0617..8aa4ecd 100644 --- a/_modules/airflow/hooks/S3_hook.html +++ b/_modules/airflow/hooks/S3_hook.html @@ -91,7 +91,7 @@ <li class="toctree-l1"><a class="reference internal" href="../../../start.html">Quick Start</a></li> <li class="toctree-l1"><a class="reference internal" href="../../../installation.html">Installation</a></li> <li class="toctree-l1"><a class="reference internal" href="../../../tutorial.html">Tutorial</a></li> -<li class="toctree-l1"><a class="reference internal" href="../../../configuration.html">Configuration</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../howto/index.html">How-to Guides</a></li> <li class="toctree-l1"><a class="reference internal" href="../../../ui.html">UI / Screenshots</a></li> <li class="toctree-l1"><a class="reference internal" href="../../../concepts.html">Concepts</a></li> <li class="toctree-l1"><a class="reference internal" href="../../../profiling.html">Data Profiling</a></li> @@ -99,8 +99,10 @@ <li class="toctree-l1"><a class="reference internal" href="../../../scheduler.html">Scheduling & Triggers</a></li> <li class="toctree-l1"><a class="reference internal" href="../../../plugins.html">Plugins</a></li> <li class="toctree-l1"><a class="reference internal" href="../../../security.html">Security</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../timezone.html">Time zones</a></li> <li class="toctree-l1"><a class="reference internal" href="../../../api.html">Experimental Rest API</a></li> <li class="toctree-l1"><a class="reference internal" href="../../../integration.html">Integration</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../lineage.html">Lineage</a></li> <li class="toctree-l1"><a class="reference internal" href="../../../faq.html">FAQ</a></li> <li class="toctree-l1"><a class="reference internal" href="../../../code.html">API Reference</a></li> </ul> @@ -169,17 +171,22 @@ <h1>Source code for airflow.hooks.S3_hook</h1><div class="highlight"><pre> <span></span><span class="c1"># -*- coding: utf-8 -*-</span> <span class="c1">#</span> -<span class="c1"># Licensed under the Apache License, Version 2.0 (the "License");</span> -<span class="c1"># you may not use this file except in compliance with the License.</span> -<span class="c1"># 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 "AS IS" 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"># Licensed to the Apache Software Foundation (ASF) under one</span> +<span class="c1"># or more contributor license agreements. See the NOTICE file</span> +<span class="c1"># distributed with this work for additional information</span> +<span class="c1"># regarding copyright ownership. The ASF licenses this file</span> +<span class="c1"># to you under the Apache License, Version 2.0 (the</span> +<span class="c1"># "License"); you may not use this file except in compliance</span> +<span class="c1"># with 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,</span> +<span class="c1"># software distributed under the License is distributed on an</span> +<span class="c1"># "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY</span> +<span class="c1"># KIND, either express or implied. See the License for the</span> +<span class="c1"># specific language governing permissions and limitations</span> +<span class="c1"># under the License.</span> <span class="kn">from</span> <span class="nn">airflow.exceptions</span> <span class="k">import</span> <span class="n">AirflowException</span> <span class="kn">from</span> <span class="nn">airflow.contrib.hooks.aws_hook</span> <span class="k">import</span> <span class="n">AwsHook</span> @@ -201,13 +208,13 @@ <span class="k">def</span> <span class="nf">parse_s3_url</span><span class="p">(</span><span class="n">s3url</span><span class="p">):</span> <span class="n">parsed_url</span> <span class="o">=</span> <span class="n">urlparse</span><span class="p">(</span><span class="n">s3url</span><span class="p">)</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">parsed_url</span><span class="o">.</span><span class="n">netloc</span><span class="p">:</span> - <span class="k">raise</span> <span class="n">AirflowException</span><span class="p">(</span><span class="s1">'Please provide a bucket_name'</span><span class="p">)</span> + <span class="k">raise</span> <span class="n">AirflowException</span><span class="p">(</span><span class="s1">'Please provide a bucket_name instead of "</span><span class="si">%s</span><span class="s1">"'</span> <span class="o">%</span> <span class="n">s3url</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="n">bucket_name</span> <span class="o">=</span> <span class="n">parsed_url</span><span class="o">.</span><span class="n">netloc</span> <span class="n">key</span> <span class="o">=</span> <span class="n">parsed_url</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s1">'/'</span><span class="p">)</span> <span class="k">return</span> <span class="p">(</span><span class="n">bucket_name</span><span class="p">,</span> <span class="n">key</span><span class="p">)</span> - <span class="k">def</span> <span class="nf">check_for_bucket</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">bucket_name</span><span class="p">):</span> +<div class="viewcode-block" id="S3Hook.check_for_bucket"><a class="viewcode-back" href="../../../integration.html#airflow.hooks.S3_hook.S3Hook.check_for_bucket">[docs]</a> <span class="k">def</span> <span class="nf">check_for_bucket</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">bucket_name</span><span class="p">):</span> <span class="sd">"""</span> <span class="sd"> Check if bucket_name exists.</span> @@ -218,9 +225,9 @@ <span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">()</span><span class="o">.</span><span class="n">head_bucket</span><span class="p">(</span><span class="n">Bucket</span><span class="o">=</span><span class="n">bucket_name</span><span class="p">)</span> <span class="k">return</span> <span class="kc">True</span> <span class="k">except</span><span class="p">:</span> - <span class="k">return</span> <span class="kc">False</span> + <span class="k">return</span> <span class="kc">False</span></div> - <span class="k">def</span> <span class="nf">get_bucket</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">bucket_name</span><span class="p">):</span> +<div class="viewcode-block" id="S3Hook.get_bucket"><a class="viewcode-back" href="../../../integration.html#airflow.hooks.S3_hook.S3Hook.get_bucket">[docs]</a> <span class="k">def</span> <span class="nf">get_bucket</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">bucket_name</span><span class="p">):</span> <span class="sd">"""</span> <span class="sd"> Returns a boto3.S3.Bucket object</span> @@ -228,9 +235,9 @@ <span class="sd"> :type bucket_name: str</span> <span class="sd"> """</span> <span class="n">s3</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_resource_type</span><span class="p">(</span><span class="s1">'s3'</span><span class="p">)</span> - <span class="k">return</span> <span class="n">s3</span><span class="o">.</span><span class="n">Bucket</span><span class="p">(</span><span class="n">bucket_name</span><span class="p">)</span> + <span class="k">return</span> <span class="n">s3</span><span class="o">.</span><span class="n">Bucket</span><span class="p">(</span><span class="n">bucket_name</span><span class="p">)</span></div> - <span class="k">def</span> <span class="nf">check_for_prefix</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">bucket_name</span><span class="p">,</span> <span class="n">prefix</span><span class="p">,</span> <span class="n">delimiter</span><span class="p">):</span> +<div class="viewcode-block" id="S3Hook.check_for_prefix"><a class="viewcode-back" href="../../../integration.html#airflow.hooks.S3_hook.S3Hook.check_for_prefix">[docs]</a> <span class="k">def</span> <span class="nf">check_for_prefix</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">bucket_name</span><span class="p">,</span> <span class="n">prefix</span><span class="p">,</span> <span class="n">delimiter</span><span class="p">):</span> <span class="sd">"""</span> <span class="sd"> Checks that a prefix exists in a bucket</span> <span class="sd"> """</span> @@ -238,9 +245,9 @@ <span class="n">prefix_split</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="sa">r</span><span class="s1">'(\w+[</span><span class="si">{d}</span><span class="s1">])$'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">d</span><span class="o">=</span><span class="n">delimiter</span><span class="p">),</span> <span class="n">prefix</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="n">previous_level</span> <span class="o">=</span> <span class="n">prefix_split</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="n">plist</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">list_prefixes</span><span class="p">(</span><span class="n">bucket_name</span><span class="p">,</span> <span class="n">previous_level</span><span class="p">,</span> <span class="n">delimiter</span><span class="p">)</span> - <span class="k">return</span> <span class="kc">False</span> <span class="k">if</span> <span class="n">plist</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">prefix</span> <span class="ow">in</span> <span class="n">plist</span> + <span class="k">return</span> <span class="kc">False</span> <span class="k">if</span> <span class="n">plist</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">prefix</span> <span class="ow">in</span> <span class="n">plist</span></div> - <span class="k">def</span> <span class="nf">list_prefixes</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">bucket_name</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="s1">''</span><span class="p">,</span> <span class="n">delimiter</span><span class="o">=</span><span class="s1">''</span><span class="p">):</span> +<div class="viewcode-block" id="S3Hook.list_prefixes"><a class="viewcode-back" href="../../../integration.html#airflow.hooks.S3_hook.S3Hook.list_prefixes">[docs]</a> <span class="k">def</span> <span class="nf">list_prefixes</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">bucket_name</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="s1">''</span><span class="p">,</span> <span class="n">delimiter</span><span class="o">=</span><span class="s1">''</span><span class="p">):</span> <span class="sd">"""</span> <span class="sd"> Lists prefixes in a bucket under prefix</span> @@ -251,12 +258,23 @@ <span class="sd"> :param delimiter: the delimiter marks key hierarchy.</span> <span class="sd"> :type delimiter: str</span> <span class="sd"> """</span> - <span class="n">response</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">()</span><span class="o">.</span><span class="n">list_objects_v2</span><span class="p">(</span><span class="n">Bucket</span><span class="o">=</span><span class="n">bucket_name</span><span class="p">,</span> - <span class="n">Prefix</span><span class="o">=</span><span class="n">prefix</span><span class="p">,</span> - <span class="n">Delimiter</span><span class="o">=</span><span class="n">delimiter</span><span class="p">)</span> - <span class="k">return</span> <span class="p">[</span><span class="n">p</span><span class="p">[</span><span class="s1">'Prefix'</span><span class="p">]</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">response</span><span class="p">[</span><span class="s1">'CommonPrefixes'</span><span class="p">]]</span> <span class="k">if</span> <span class="n">response</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'CommonPrefixes'</span><span class="p">)</span> <span class="k">else</span> <span class="kc">None</span> - - <span class="k">def</span> <span class="nf">list_keys</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">bucket_name</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="s1">''</span><span class="p">,</span> <span class="n">delimiter</span><span class="o">=</span><span class="s1">''</span><span class="p">):</span> + <span class="n">paginator</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">()</span><span class="o">.</span><span class="n">get_paginator</span><span class="p">(</span><span class="s1">'list_objects_v2'</span><span class="p">)</span> + <span class="n">response</span> <span class="o">=</span> <span class="n">paginator</span><span class="o">.</span><span class="n">paginate</span><span class="p">(</span><span class="n">Bucket</span><span class="o">=</span><span class="n">bucket_name</span><span class="p">,</span> + <span class="n">Prefix</span><span class="o">=</span><span class="n">prefix</span><span class="p">,</span> + <span class="n">Delimiter</span><span class="o">=</span><span class="n">delimiter</span><span class="p">)</span> + + <span class="n">has_results</span> <span class="o">=</span> <span class="kc">False</span> + <span class="n">prefixes</span> <span class="o">=</span> <span class="p">[]</span> + <span class="k">for</span> <span class="n">page</span> <span class="ow">in</span> <span class="n">response</span><span class="p">:</span> + <span class="k">if</span> <span class="s1">'CommonPrefixes'</span> <span class="ow">in</span> <span class="n">page</span><span class="p">:</span> + <span class="n">has_results</span> <span class="o">=</span> <span class="kc">True</span> + <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">page</span><span class="p">[</span><span class="s1">'CommonPrefixes'</span><span class="p">]:</span> + <span class="n">prefixes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="s1">'Prefix'</span><span class="p">])</span> + + <span class="k">if</span> <span class="n">has_results</span><span class="p">:</span> + <span class="k">return</span> <span class="n">prefixes</span></div> + +<div class="viewcode-block" id="S3Hook.list_keys"><a class="viewcode-back" href="../../../integration.html#airflow.hooks.S3_hook.S3Hook.list_keys">[docs]</a> <span class="k">def</span> <span class="nf">list_keys</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">bucket_name</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="s1">''</span><span class="p">,</span> <span class="n">delimiter</span><span class="o">=</span><span class="s1">''</span><span class="p">):</span> <span class="sd">"""</span> <span class="sd"> Lists keys in a bucket under prefix and not containing delimiter</span> @@ -267,12 +285,23 @@ <span class="sd"> :param delimiter: the delimiter marks key hierarchy.</span> <span class="sd"> :type delimiter: str</span> <span class="sd"> """</span> - <span class="n">response</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">()</span><span class="o">.</span><span class="n">list_objects_v2</span><span class="p">(</span><span class="n">Bucket</span><span class="o">=</span><span class="n">bucket_name</span><span class="p">,</span> - <span class="n">Prefix</span><span class="o">=</span><span class="n">prefix</span><span class="p">,</span> - <span class="n">Delimiter</span><span class="o">=</span><span class="n">delimiter</span><span class="p">)</span> - <span class="k">return</span> <span class="p">[</span><span class="n">k</span><span class="p">[</span><span class="s1">'Key'</span><span class="p">]</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">response</span><span class="p">[</span><span class="s1">'Contents'</span><span class="p">]]</span> <span class="k">if</span> <span class="n">response</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'Contents'</span><span class="p">)</span> <span class="k">else</span> <span class="kc">None</span> - - <span class="k">def</span> <span class="nf">check_for_key</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">bucket_name</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> + <span class="n">paginator</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">()</span><span class="o">.</span><span class="n">get_paginator</span><span class="p">(</span><span class="s1">'list_objects_v2'</span><span class="p">)</span> + <span class="n">response</span> <span class="o">=</span> <span class="n">paginator</span><span class="o">.</span><span class="n">paginate</span><span class="p">(</span><span class="n">Bucket</span><span class="o">=</span><span class="n">bucket_name</span><span class="p">,</span> + <span class="n">Prefix</span><span class="o">=</span><span class="n">prefix</span><span class="p">,</span> + <span class="n">Delimiter</span><span class="o">=</span><span class="n">delimiter</span><span class="p">)</span> + + <span class="n">has_results</span> <span class="o">=</span> <span class="kc">False</span> + <span class="n">keys</span> <span class="o">=</span> <span class="p">[]</span> + <span class="k">for</span> <span class="n">page</span> <span class="ow">in</span> <span class="n">response</span><span class="p">:</span> + <span class="k">if</span> <span class="s1">'Contents'</span> <span class="ow">in</span> <span class="n">page</span><span class="p">:</span> + <span class="n">has_results</span> <span class="o">=</span> <span class="kc">True</span> + <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">page</span><span class="p">[</span><span class="s1">'Contents'</span><span class="p">]:</span> + <span class="n">keys</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">k</span><span class="p">[</span><span class="s1">'Key'</span><span class="p">])</span> + + <span class="k">if</span> <span class="n">has_results</span><span class="p">:</span> + <span class="k">return</span> <span class="n">keys</span></div> + +<div class="viewcode-block" id="S3Hook.check_for_key"><a class="viewcode-back" href="../../../integration.html#airflow.hooks.S3_hook.S3Hook.check_for_key">[docs]</a> <span class="k">def</span> <span class="nf">check_for_key</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">bucket_name</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> <span class="sd">"""</span> <span class="sd"> Checks if a key exists in a bucket</span> @@ -288,9 +317,9 @@ <span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">()</span><span class="o">.</span><span class="n">head_object</span><span class="p">(</span><span class="n">Bucket</span><span class="o">=</span><span class="n">bucket_name</span><span class="p">,</span> <span class="n">Key</span><span class="o">=</span><span class="n">key</span><span class="p">)</span> <span class="k">return</span> <span class="kc">True</span> <span class="k">except</span><span class="p">:</span> - <span class="k">return</span> <span class="kc">False</span> + <span class="k">return</span> <span class="kc">False</span></div> - <span class="k">def</span> <span class="nf">get_key</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">bucket_name</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> +<div class="viewcode-block" id="S3Hook.get_key"><a class="viewcode-back" href="../../../integration.html#airflow.hooks.S3_hook.S3Hook.get_key">[docs]</a> <span class="k">def</span> <span class="nf">get_key</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">bucket_name</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> <span class="sd">"""</span> <span class="sd"> Returns a boto3.s3.Object</span> @@ -304,9 +333,9 @@ <span class="n">obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_resource_type</span><span class="p">(</span><span class="s1">'s3'</span><span class="p">)</span><span class="o">.</span><span class="n">Object</span><span class="p">(</span><span class="n">bucket_name</span><span class="p">,</span> <span class="n">key</span><span class="p">)</span> <span class="n">obj</span><span class="o">.</span><span class="n">load</span><span class="p">()</span> - <span class="k">return</span> <span class="n">obj</span> + <span class="k">return</span> <span class="n">obj</span></div> - <span class="k">def</span> <span class="nf">read_key</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">bucket_name</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> +<div class="viewcode-block" id="S3Hook.read_key"><a class="viewcode-back" href="../../../integration.html#airflow.hooks.S3_hook.S3Hook.read_key">[docs]</a> <span class="k">def</span> <span class="nf">read_key</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">bucket_name</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> <span class="sd">"""</span> <span class="sd"> Reads a key from S3</span> @@ -317,23 +346,65 @@ <span class="sd"> """</span> <span class="n">obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_key</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">bucket_name</span><span class="p">)</span> - <span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">get</span><span class="p">()[</span><span class="s1">'Body'</span><span class="p">]</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">'utf-8'</span><span class="p">)</span> + <span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">get</span><span class="p">()[</span><span class="s1">'Body'</span><span class="p">]</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">'utf-8'</span><span class="p">)</span></div> + +<div class="viewcode-block" id="S3Hook.select_key"><a class="viewcode-back" href="../../../integration.html#airflow.hooks.S3_hook.S3Hook.select_key">[docs]</a> <span class="k">def</span> <span class="nf">select_key</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">bucket_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> + <span class="n">expression</span><span class="o">=</span><span class="s1">'SELECT * FROM S3Object'</span><span class="p">,</span> + <span class="n">expression_type</span><span class="o">=</span><span class="s1">'SQL'</span><span class="p">,</span> + <span class="n">input_serialization</span><span class="o">=</span><span class="p">{</span><span class="s1">'CSV'</span><span class="p">:</span> <span class="p">{}},</span> + <span class="n">output_serialization</span><span class="o">=</span><span class="p">{</span><span class="s1">'CSV'</span><span class="p">:</span> <span class="p">{}}):</span> + <span class="sd">"""</span> +<span class="sd"> Reads a key with S3 Select.</span> + +<span class="sd"> :param key: S3 key that will point to the file</span> +<span class="sd"> :type key: str</span> +<span class="sd"> :param bucket_name: Name of the bucket in which the file is stored</span> +<span class="sd"> :type bucket_name: str</span> +<span class="sd"> :param expression: S3 Select expression</span> +<span class="sd"> :type expression: str</span> +<span class="sd"> :param expression_type: S3 Select expression type</span> +<span class="sd"> :type expression_type: str</span> +<span class="sd"> :param input_serialization: S3 Select input data serialization format</span> +<span class="sd"> :type input_serialization: dict</span> +<span class="sd"> :param output_serialization: S3 Select output data serialization format</span> +<span class="sd"> :type output_serialization: dict</span> +<span class="sd"> :return: retrieved subset of original data by S3 Select</span> +<span class="sd"> :rtype: str</span> + +<span class="sd"> .. seealso::</span> +<span class="sd"> For more details about S3 Select parameters:</span> +<span class="sd"> http://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.select_object_content</span> +<span class="sd"> """</span> + <span class="k">if</span> <span class="ow">not</span> <span class="n">bucket_name</span><span class="p">:</span> + <span class="p">(</span><span class="n">bucket_name</span><span class="p">,</span> <span class="n">key</span><span class="p">)</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">parse_s3_url</span><span class="p">(</span><span class="n">key</span><span class="p">)</span> + + <span class="n">response</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">()</span><span class="o">.</span><span class="n">select_object_content</span><span class="p">(</span> + <span class="n">Bucket</span><span class="o">=</span><span class="n">bucket_name</span><span class="p">,</span> + <span class="n">Key</span><span class="o">=</span><span class="n">key</span><span class="p">,</span> + <span class="n">Expression</span><span class="o">=</span><span class="n">expression</span><span class="p">,</span> + <span class="n">ExpressionType</span><span class="o">=</span><span class="n">expression_type</span><span class="p">,</span> + <span class="n">InputSerialization</span><span class="o">=</span><span class="n">input_serialization</span><span class="p">,</span> + <span class="n">OutputSerialization</span><span class="o">=</span><span class="n">output_serialization</span><span class="p">)</span> + + <span class="k">return</span> <span class="s1">''</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">event</span><span class="p">[</span><span class="s1">'Records'</span><span class="p">][</span><span class="s1">'Payload'</span><span class="p">]</span> + <span class="k">for</span> <span class="n">event</span> <span class="ow">in</span> <span class="n">response</span><span class="p">[</span><span class="s1">'Payload'</span><span class="p">]</span> + <span class="k">if</span> <span class="s1">'Records'</span> <span class="ow">in</span> <span class="n">event</span><span class="p">)</span></div> - <span class="k">def</span> <span class="nf">check_for_wildcard_key</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> +<div class="viewcode-block" id="S3Hook.check_for_wildcard_key"><a class="viewcode-back" href="../../../integration.html#airflow.hooks.S3_hook.S3Hook.check_for_wildcard_key">[docs]</a> <span class="k">def</span> <span class="nf">check_for_wildcard_key</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">wildcard_key</span><span class="p">,</span> <span class="n">bucket_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">delimiter</span><span class="o">=</span><span class="s1">''</span><span class="p">):</span> <span class="sd">"""</span> <span class="sd"> Checks that a key matching a wildcard expression exists in a bucket</span> <span class="sd"> """</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_wildcard_key</span><span class="p">(</span><span class="n">wildcard_key</span><span class="o">=</span><span class="n">wildcard_key</span><span class="p">,</span> <span class="n">bucket_name</span><span class="o">=</span><span class="n">bucket_name</span><span class="p">,</span> - <span class="n">delimiter</span><span class="o">=</span><span class="n">delimiter</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> + <span class="n">delimiter</span><span class="o">=</span><span class="n">delimiter</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span></div> - <span class="k">def</span> <span class="nf">get_wildcard_key</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">wildcard_key</span><span class="p">,</span> <span class="n">bucket_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">delimiter</span><span class="o">=</span><span class="s1">''</span><span class="p">):</span> +<div class="viewcode-block" id="S3Hook.get_wildcard_key"><a class="viewcode-back" href="../../../integration.html#airflow.hooks.S3_hook.S3Hook.get_wildcard_key">[docs]</a> <span class="k">def</span> <span class="nf">get_wildcard_key</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">wildcard_key</span><span class="p">,</span> <span class="n">bucket_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">delimiter</span><span class="o">=</span><span class="s1">''</span><span class="p">):</span> <span class="sd">"""</span> -<span class="sd"> Returns a boto3.s3.Object object matching the regular expression</span> +<span class="sd"> Returns a boto3.s3.Object object matching the wildcard expression</span> -<span class="sd"> :param regex_key: the path to the key</span> -<span class="sd"> :type regex_key: str</span> +<span class="sd"> :param wildcard_key: the path to the key</span> +<span class="sd"> :type wildcard_key: str</span> <span class="sd"> :param bucket_name: the name of the bucket</span> <span class="sd"> :type bucket_name: str</span> <span class="sd"> """</span> @@ -345,9 +416,9 @@ <span class="k">if</span> <span class="n">klist</span><span class="p">:</span> <span class="n">key_matches</span> <span class="o">=</span> <span class="p">[</span><span class="n">k</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">klist</span> <span class="k">if</span> <span class="n">fnmatch</span><span class="o">.</span><span class="n">fnmatch</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">wildcard_key</span><span class="p">)]</span> <span class="k">if</span> <span class="n">key_matches</span><span class="p">:</span> - <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_key</span><span class="p">(</span><span class="n">key_matches</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">bucket_name</span><span class="p">)</span> + <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_key</span><span class="p">(</span><span class="n">key_matches</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">bucket_name</span><span class="p">)</span></div> - <span class="k">def</span> <span class="nf">load_file</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> +<div class="viewcode-block" id="S3Hook.load_file"><a class="viewcode-back" href="../../../integration.html#airflow.hooks.S3_hook.S3Hook.load_file">[docs]</a> <span class="k">def</span> <span class="nf">load_file</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">bucket_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> @@ -381,9 +452,9 @@ <span class="n">extra_args</span><span class="p">[</span><span class="s1">'ServerSideEncryption'</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"AES256"</span> <span class="n">client</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">()</span> - <span class="n">client</span><span class="o">.</span><span class="n">upload_file</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">bucket_name</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">ExtraArgs</span><span class="o">=</span><span class="n">extra_args</span><span class="p">)</span> + <span class="n">client</span><span class="o">.</span><span class="n">upload_file</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">bucket_name</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">ExtraArgs</span><span class="o">=</span><span class="n">extra_args</span><span class="p">)</span></div> - <span class="k">def</span> <span class="nf">load_string</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> +<div class="viewcode-block" id="S3Hook.load_string"><a class="viewcode-back" href="../../../integration.html#airflow.hooks.S3_hook.S3Hook.load_string">[docs]</a> <span class="k">def</span> <span class="nf">load_string</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">string_data</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">bucket_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> @@ -409,6 +480,37 @@ <span class="sd"> by S3 and will be stored in an encrypted form while at rest in S3.</span> <span class="sd"> :type encrypt: bool</span> <span class="sd"> """</span> + <span class="bp">self</span><span class="o">.</span><span class="n">load_bytes</span><span class="p">(</span><span class="n">string_data</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="n">encoding</span><span class="p">),</span> + <span class="n">key</span><span class="o">=</span><span class="n">key</span><span class="p">,</span> + <span class="n">bucket_name</span><span class="o">=</span><span class="n">bucket_name</span><span class="p">,</span> + <span class="n">replace</span><span class="o">=</span><span class="n">replace</span><span class="p">,</span> + <span class="n">encrypt</span><span class="o">=</span><span class="n">encrypt</span><span class="p">)</span></div> + +<div class="viewcode-block" id="S3Hook.load_bytes"><a class="viewcode-back" href="../../../integration.html#airflow.hooks.S3_hook.S3Hook.load_bytes">[docs]</a> <span class="k">def</span> <span class="nf">load_bytes</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> + <span class="n">bytes_data</span><span class="p">,</span> + <span class="n">key</span><span class="p">,</span> + <span class="n">bucket_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> + <span class="n">replace</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> + <span class="n">encrypt</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Loads bytes to S3</span> + +<span class="sd"> This is provided as a convenience to drop a string in S3. It uses the</span> +<span class="sd"> boto infrastructure to ship a file to s3.</span> + +<span class="sd"> :param bytes_data: bytes to set as content for the key.</span> +<span class="sd"> :type bytes_data: bytes</span> +<span class="sd"> :param key: S3 key that will point to the file</span> +<span class="sd"> :type key: str</span> +<span class="sd"> :param bucket_name: Name of the bucket in which to store the file</span> +<span class="sd"> :type bucket_name: str</span> +<span class="sd"> :param replace: A flag to decide whether or not to overwrite the key</span> +<span class="sd"> if it already exists</span> +<span class="sd"> :type replace: bool</span> +<span class="sd"> :param encrypt: If True, the file will be encrypted on the server-side</span> +<span class="sd"> by S3 and will be stored in an encrypted form while at rest in S3.</span> +<span class="sd"> :type encrypt: bool</span> +<span class="sd"> """</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">bucket_name</span><span class="p">:</span> <span class="p">(</span><span class="n">bucket_name</span><span class="p">,</span> <span class="n">key</span><span class="p">)</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">parse_s3_url</span><span class="p">(</span><span class="n">key</span><span class="p">)</span> @@ -419,10 +521,10 @@ <span class="k">if</span> <span class="n">encrypt</span><span class="p">:</span> <span class="n">extra_args</span><span class="p">[</span><span class="s1">'ServerSideEncryption'</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"AES256"</span> - <span class="n">filelike_buffer</span> <span class="o">=</span> <span class="n">BytesIO</span><span class="p">(</span><span class="n">string_data</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="n">encoding</span><span class="p">))</span> + <span class="n">filelike_buffer</span> <span class="o">=</span> <span class="n">BytesIO</span><span class="p">(</span><span class="n">bytes_data</span><span class="p">)</span> <span class="n">client</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">()</span> - <span class="n">client</span><span class="o">.</span><span class="n">upload_fileobj</span><span class="p">(</span><span class="n">filelike_buffer</span><span class="p">,</span> <span class="n">bucket_name</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">ExtraArgs</span><span class="o">=</span><span class="n">extra_args</span><span class="p">)</span></div> + <span class="n">client</span><span class="o">.</span><span class="n">upload_fileobj</span><span class="p">(</span><span class="n">filelike_buffer</span><span class="p">,</span> <span class="n">bucket_name</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">ExtraArgs</span><span class="o">=</span><span class="n">extra_args</span><span class="p">)</span></div></div> </pre></div> </div>
http://git-wip-us.apache.org/repos/asf/incubator-airflow-site/blob/11437c14/_modules/airflow/hooks/dbapi_hook.html ---------------------------------------------------------------------- diff --git a/_modules/airflow/hooks/dbapi_hook.html b/_modules/airflow/hooks/dbapi_hook.html new file mode 100644 index 0000000..b8b8403 --- /dev/null +++ b/_modules/airflow/hooks/dbapi_hook.html @@ -0,0 +1,515 @@ + + +<!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>airflow.hooks.dbapi_hook — Airflow Documentation</title> + + + + + + + + + + + + + + + + + + <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" /> + + + + + + <link rel="index" title="Index" + href="../../../genindex.html"/> + <link rel="search" title="Search" href="../../../search.html"/> + <link rel="top" title="Airflow Documentation" href="../../../index.html"/> + <link rel="up" title="Module code" href="../../index.html"/> + + + <script src="../../../_static/js/modernizr.min.js"></script> + +</head> + +<body class="wy-body-for-nav" role="document"> + + + <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"> Airflow + + + + </a> + + + + + + + +<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="../../../project.html">Project</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../license.html">License</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../start.html">Quick Start</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../installation.html">Installation</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../tutorial.html">Tutorial</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../howto/index.html">How-to Guides</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../ui.html">UI / Screenshots</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../concepts.html">Concepts</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../profiling.html">Data Profiling</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../cli.html">Command Line Interface</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../scheduler.html">Scheduling & Triggers</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../plugins.html">Plugins</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../security.html">Security</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../timezone.html">Time zones</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../api.html">Experimental Rest API</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../integration.html">Integration</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../lineage.html">Lineage</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../faq.html">FAQ</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../code.html">API Reference</a></li> +</ul> + + + + </div> + </div> + </nav> + + <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"> + + + <nav class="wy-nav-top" role="navigation" aria-label="top navigation"> + + <i data-toggle="wy-nav-top" class="fa fa-bars"></i> + <a href="../../../index.html">Airflow</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> »</li> + + <li><a href="../../index.html">Module code</a> »</li> + + <li>airflow.hooks.dbapi_hook</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 airflow.hooks.dbapi_hook</h1><div class="highlight"><pre> +<span></span><span class="c1"># -*- coding: utf-8 -*-</span> +<span class="c1">#</span> +<span class="c1"># Licensed to the Apache Software Foundation (ASF) under one</span> +<span class="c1"># or more contributor license agreements. See the NOTICE file</span> +<span class="c1"># distributed with this work for additional information</span> +<span class="c1"># regarding copyright ownership. The ASF licenses this file</span> +<span class="c1"># to you under the Apache License, Version 2.0 (the</span> +<span class="c1"># "License"); you may not use this file except in compliance</span> +<span class="c1"># with 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,</span> +<span class="c1"># software distributed under the License is distributed on an</span> +<span class="c1"># "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY</span> +<span class="c1"># KIND, either express or implied. See the License for the</span> +<span class="c1"># specific language governing permissions and limitations</span> +<span class="c1"># under the License.</span> + +<span class="kn">from</span> <span class="nn">builtins</span> <span class="k">import</span> <span class="nb">str</span> +<span class="kn">from</span> <span class="nn">past.builtins</span> <span class="k">import</span> <span class="n">basestring</span> +<span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">datetime</span> +<span class="kn">from</span> <span class="nn">contextlib</span> <span class="k">import</span> <span class="n">closing</span> +<span class="kn">import</span> <span class="nn">sys</span> + +<span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="k">import</span> <span class="n">create_engine</span> + +<span class="kn">from</span> <span class="nn">airflow.hooks.base_hook</span> <span class="k">import</span> <span class="n">BaseHook</span> +<span class="kn">from</span> <span class="nn">airflow.exceptions</span> <span class="k">import</span> <span class="n">AirflowException</span> + + +<div class="viewcode-block" id="DbApiHook"><a class="viewcode-back" href="../../../code.html#airflow.hooks.dbapi_hook.DbApiHook">[docs]</a><span class="k">class</span> <span class="nc">DbApiHook</span><span class="p">(</span><span class="n">BaseHook</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Abstract base class for sql hooks.</span> +<span class="sd"> """</span> + <span class="c1"># Override to provide the connection name.</span> + <span class="n">conn_name_attr</span> <span class="o">=</span> <span class="kc">None</span> + <span class="c1"># Override to have a default connection id for a particular dbHook</span> + <span class="n">default_conn_name</span> <span class="o">=</span> <span class="s1">'default_conn_id'</span> + <span class="c1"># Override if this db supports autocommit.</span> + <span class="n">supports_autocommit</span> <span class="o">=</span> <span class="kc">False</span> + <span class="c1"># Override with the object that exposes the connect method</span> + <span class="n">connector</span> <span class="o">=</span> <span class="kc">None</span> + + <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span> + <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">conn_name_attr</span><span class="p">:</span> + <span class="k">raise</span> <span class="n">AirflowException</span><span class="p">(</span><span class="s2">"conn_name_attr is not defined"</span><span class="p">)</span> + <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span> + <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">conn_name_attr</span><span class="p">,</span> <span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> + <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">conn_name_attr</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span> + <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">conn_name_attr</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">default_conn_name</span><span class="p">)</span> + <span class="k">else</span><span class="p">:</span> + <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">conn_name_attr</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">conn_name_attr</span><span class="p">])</span> + +<div class="viewcode-block" id="DbApiHook.get_conn"><a class="viewcode-back" href="../../../code.html#airflow.hooks.dbapi_hook.DbApiHook.get_conn">[docs]</a> <span class="k">def</span> <span class="nf">get_conn</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> + <span class="sd">"""Returns a connection object</span> +<span class="sd"> """</span> + <span class="n">db</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_connection</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">conn_name_attr</span><span class="p">))</span> + <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">connector</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span> + <span class="n">host</span><span class="o">=</span><span class="n">db</span><span class="o">.</span><span class="n">host</span><span class="p">,</span> + <span class="n">port</span><span class="o">=</span><span class="n">db</span><span class="o">.</span><span class="n">port</span><span class="p">,</span> + <span class="n">username</span><span class="o">=</span><span class="n">db</span><span class="o">.</span><span class="n">login</span><span class="p">,</span> + <span class="n">schema</span><span class="o">=</span><span class="n">db</span><span class="o">.</span><span class="n">schema</span><span class="p">)</span></div> + + <span class="k">def</span> <span class="nf">get_uri</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> + <span class="n">conn</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_connection</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">conn_name_attr</span><span class="p">))</span> + <span class="n">login</span> <span class="o">=</span> <span class="s1">''</span> + <span class="k">if</span> <span class="n">conn</span><span class="o">.</span><span class="n">login</span><span class="p">:</span> + <span class="n">login</span> <span class="o">=</span> <span class="s1">'</span><span class="si">{conn.login}</span><span class="s1">:</span><span class="si">{conn.password}</span><span class="s1">@'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">conn</span><span class="o">=</span><span class="n">conn</span><span class="p">)</span> + <span class="n">host</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">host</span> + <span class="k">if</span> <span class="n">conn</span><span class="o">.</span><span class="n">port</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span> + <span class="n">host</span> <span class="o">+=</span> <span class="s1">':</span><span class="si">{port}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">port</span><span class="o">=</span><span class="n">conn</span><span class="o">.</span><span class="n">port</span><span class="p">)</span> + <span class="k">return</span> <span class="s1">'</span><span class="si">{conn.conn_type}</span><span class="s1">://</span><span class="si">{login}{host}</span><span class="s1">/</span><span class="si">{conn.schema}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span> + <span class="n">conn</span><span class="o">=</span><span class="n">conn</span><span class="p">,</span> <span class="n">login</span><span class="o">=</span><span class="n">login</span><span class="p">,</span> <span class="n">host</span><span class="o">=</span><span class="n">host</span><span class="p">)</span> + + <span class="k">def</span> <span class="nf">get_sqlalchemy_engine</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">engine_kwargs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> + <span class="k">if</span> <span class="n">engine_kwargs</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> + <span class="n">engine_kwargs</span> <span class="o">=</span> <span class="p">{}</span> + <span class="k">return</span> <span class="n">create_engine</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_uri</span><span class="p">(),</span> <span class="o">**</span><span class="n">engine_kwargs</span><span class="p">)</span> + +<div class="viewcode-block" id="DbApiHook.get_pandas_df"><a class="viewcode-back" href="../../../code.html#airflow.hooks.dbapi_hook.DbApiHook.get_pandas_df">[docs]</a> <span class="k">def</span> <span class="nf">get_pandas_df</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sql</span><span class="p">,</span> <span class="n">parameters</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Executes the sql and returns a pandas dataframe</span> + +<span class="sd"> :param sql: the sql statement to be executed (str) or a list of</span> +<span class="sd"> sql statements to execute</span> +<span class="sd"> :type sql: str or list</span> +<span class="sd"> :param parameters: The parameters to render the SQL query with.</span> +<span class="sd"> :type parameters: mapping or iterable</span> +<span class="sd"> """</span> + <span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o"><</span> <span class="mi">3</span><span class="p">:</span> + <span class="n">sql</span> <span class="o">=</span> <span class="n">sql</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">'utf-8'</span><span class="p">)</span> + <span class="kn">import</span> <span class="nn">pandas.io.sql</span> <span class="k">as</span> <span class="nn">psql</span> + + <span class="k">with</span> <span class="n">closing</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">())</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span> + <span class="k">return</span> <span class="n">psql</span><span class="o">.</span><span class="n">read_sql</span><span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">con</span><span class="o">=</span><span class="n">conn</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="n">parameters</span><span class="p">)</span></div> + +<div class="viewcode-block" id="DbApiHook.get_records"><a class="viewcode-back" href="../../../code.html#airflow.hooks.dbapi_hook.DbApiHook.get_records">[docs]</a> <span class="k">def</span> <span class="nf">get_records</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sql</span><span class="p">,</span> <span class="n">parameters</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Executes the sql and returns a set of records.</span> + +<span class="sd"> :param sql: the sql statement to be executed (str) or a list of</span> +<span class="sd"> sql statements to execute</span> +<span class="sd"> :type sql: str or list</span> +<span class="sd"> :param parameters: The parameters to render the SQL query with.</span> +<span class="sd"> :type parameters: mapping or iterable</span> +<span class="sd"> """</span> + <span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o"><</span> <span class="mi">3</span><span class="p">:</span> + <span class="n">sql</span> <span class="o">=</span> <span class="n">sql</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">'utf-8'</span><span class="p">)</span> + + <span class="k">with</span> <span class="n">closing</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">())</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span> + <span class="k">with</span> <span class="n">closing</span><span class="p">(</span><span class="n">conn</span><span class="o">.</span><span class="n">cursor</span><span class="p">())</span> <span class="k">as</span> <span class="n">cur</span><span class="p">:</span> + <span class="k">if</span> <span class="n">parameters</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span> + <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">parameters</span><span class="p">)</span> + <span class="k">else</span><span class="p">:</span> + <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span> + <span class="k">return</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchall</span><span class="p">()</span></div> + +<div class="viewcode-block" id="DbApiHook.get_first"><a class="viewcode-back" href="../../../code.html#airflow.hooks.dbapi_hook.DbApiHook.get_first">[docs]</a> <span class="k">def</span> <span class="nf">get_first</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sql</span><span class="p">,</span> <span class="n">parameters</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Executes the sql and returns the first resulting row.</span> + +<span class="sd"> :param sql: the sql statement to be executed (str) or a list of</span> +<span class="sd"> sql statements to execute</span> +<span class="sd"> :type sql: str or list</span> +<span class="sd"> :param parameters: The parameters to render the SQL query with.</span> +<span class="sd"> :type parameters: mapping or iterable</span> +<span class="sd"> """</span> + <span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o"><</span> <span class="mi">3</span><span class="p">:</span> + <span class="n">sql</span> <span class="o">=</span> <span class="n">sql</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">'utf-8'</span><span class="p">)</span> + + <span class="k">with</span> <span class="n">closing</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">())</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span> + <span class="k">with</span> <span class="n">closing</span><span class="p">(</span><span class="n">conn</span><span class="o">.</span><span class="n">cursor</span><span class="p">())</span> <span class="k">as</span> <span class="n">cur</span><span class="p">:</span> + <span class="k">if</span> <span class="n">parameters</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span> + <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">parameters</span><span class="p">)</span> + <span class="k">else</span><span class="p">:</span> + <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span> + <span class="k">return</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span></div> + +<div class="viewcode-block" id="DbApiHook.run"><a class="viewcode-back" href="../../../code.html#airflow.hooks.dbapi_hook.DbApiHook.run">[docs]</a> <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sql</span><span class="p">,</span> <span class="n">autocommit</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">parameters</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Runs a command or a list of commands. Pass a list of sql</span> +<span class="sd"> statements to the sql parameter to get them to execute</span> +<span class="sd"> sequentially</span> + +<span class="sd"> :param sql: the sql statement to be executed (str) or a list of</span> +<span class="sd"> sql statements to execute</span> +<span class="sd"> :type sql: str or list</span> +<span class="sd"> :param autocommit: What to set the connection's autocommit setting to</span> +<span class="sd"> before executing the query.</span> +<span class="sd"> :type autocommit: bool</span> +<span class="sd"> :param parameters: The parameters to render the SQL query with.</span> +<span class="sd"> :type parameters: mapping or iterable</span> +<span class="sd"> """</span> + <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">basestring</span><span class="p">):</span> + <span class="n">sql</span> <span class="o">=</span> <span class="p">[</span><span class="n">sql</span><span class="p">]</span> + + <span class="k">with</span> <span class="n">closing</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">())</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span> + <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">supports_autocommit</span><span class="p">:</span> + <span class="bp">self</span><span class="o">.</span><span class="n">set_autocommit</span><span class="p">(</span><span class="n">conn</span><span class="p">,</span> <span class="n">autocommit</span><span class="p">)</span> + + <span class="k">with</span> <span class="n">closing</span><span class="p">(</span><span class="n">conn</span><span class="o">.</span><span class="n">cursor</span><span class="p">())</span> <span class="k">as</span> <span class="n">cur</span><span class="p">:</span> + <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">sql</span><span class="p">:</span> + <span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o"><</span> <span class="mi">3</span><span class="p">:</span> + <span class="n">s</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">'utf-8'</span><span class="p">)</span> + <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> + <span class="k">if</span> <span class="n">parameters</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span> + <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">parameters</span><span class="p">)</span> + <span class="k">else</span><span class="p">:</span> + <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> + + <span class="k">if</span> <span class="ow">not</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">conn</span><span class="p">,</span> <span class="s1">'autocommit'</span><span class="p">,</span> <span class="kc">False</span><span class="p">):</span> + <span class="n">conn</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span></div> + +<div class="viewcode-block" id="DbApiHook.set_autocommit"><a class="viewcode-back" href="../../../code.html#airflow.hooks.dbapi_hook.DbApiHook.set_autocommit">[docs]</a> <span class="k">def</span> <span class="nf">set_autocommit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">conn</span><span class="p">,</span> <span class="n">autocommit</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Sets the autocommit flag on the connection</span> +<span class="sd"> """</span> + <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">supports_autocommit</span> <span class="ow">and</span> <span class="n">autocommit</span><span class="p">:</span> + <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span> + <span class="p">(</span><span class="s2">"</span><span class="si">%s</span><span class="s2"> connection doesn't support "</span> + <span class="s2">"autocommit but autocommit activated."</span><span class="p">),</span> + <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">conn_name_attr</span><span class="p">))</span> + <span class="n">conn</span><span class="o">.</span><span class="n">autocommit</span> <span class="o">=</span> <span class="n">autocommit</span></div> + +<div class="viewcode-block" id="DbApiHook.get_cursor"><a class="viewcode-back" href="../../../code.html#airflow.hooks.dbapi_hook.DbApiHook.get_cursor">[docs]</a> <span class="k">def</span> <span class="nf">get_cursor</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Returns a cursor</span> +<span class="sd"> """</span> + <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">()</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span></div> + +<div class="viewcode-block" id="DbApiHook.insert_rows"><a class="viewcode-back" href="../../../code.html#airflow.hooks.dbapi_hook.DbApiHook.insert_rows">[docs]</a> <span class="k">def</span> <span class="nf">insert_rows</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">table</span><span class="p">,</span> <span class="n">rows</span><span class="p">,</span> <span class="n">target_fields</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">commit_every</span><span class="o">=</span><span class="mi">1000</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> A generic way to insert a set of tuples into a table,</span> +<span class="sd"> a new transaction is created every commit_every rows</span> + +<span class="sd"> :param table: Name of the target table</span> +<span class="sd"> :type table: str</span> +<span class="sd"> :param rows: The rows to insert into the table</span> +<span class="sd"> :type rows: iterable of tuples</span> +<span class="sd"> :param target_fields: The names of the columns to fill in the table</span> +<span class="sd"> :type target_fields: iterable of strings</span> +<span class="sd"> :param commit_every: The maximum number of rows to insert in one</span> +<span class="sd"> transaction. Set to 0 to insert all rows in one transaction.</span> +<span class="sd"> :type commit_every: int</span> +<span class="sd"> """</span> + <span class="k">if</span> <span class="n">target_fields</span><span class="p">:</span> + <span class="n">target_fields</span> <span class="o">=</span> <span class="s2">", "</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">target_fields</span><span class="p">)</span> + <span class="n">target_fields</span> <span class="o">=</span> <span class="s2">"(</span><span class="si">{}</span><span class="s2">)"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">target_fields</span><span class="p">)</span> + <span class="k">else</span><span class="p">:</span> + <span class="n">target_fields</span> <span class="o">=</span> <span class="s1">''</span> + <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span> + <span class="k">with</span> <span class="n">closing</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">())</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span> + <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">supports_autocommit</span><span class="p">:</span> + <span class="bp">self</span><span class="o">.</span><span class="n">set_autocommit</span><span class="p">(</span><span class="n">conn</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span> + + <span class="n">conn</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span> + + <span class="k">with</span> <span class="n">closing</span><span class="p">(</span><span class="n">conn</span><span class="o">.</span><span class="n">cursor</span><span class="p">())</span> <span class="k">as</span> <span class="n">cur</span><span class="p">:</span> + <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">row</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">rows</span><span class="p">,</span> <span class="mi">1</span><span class="p">):</span> + <span class="n">l</span> <span class="o">=</span> <span class="p">[]</span> + <span class="k">for</span> <span class="n">cell</span> <span class="ow">in</span> <span class="n">row</span><span class="p">:</span> + <span class="n">l</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_serialize_cell</span><span class="p">(</span><span class="n">cell</span><span class="p">,</span> <span class="n">conn</span><span class="p">))</span> + <span class="n">values</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">l</span><span class="p">)</span> + <span class="n">placeholders</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"</span><span class="si">%s</span><span class="s2">"</span><span class="p">,</span> <span class="p">]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">values</span><span class="p">)</span> + <span class="n">sql</span> <span class="o">=</span> <span class="s2">"INSERT INTO </span><span class="si">{0}</span><span class="s2"> </span><span class="si">{1}</span><span class="s2"> VALUES (</span><span class="si">{2}</span><span class="s2">)"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span> + <span class="n">table</span><span class="p">,</span> + <span class="n">target_fields</span><span class="p">,</span> + <span class="s2">","</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">placeholders</span><span class="p">))</span> + <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">values</span><span class="p">)</span> + <span class="k">if</span> <span class="n">commit_every</span> <span class="ow">and</span> <span class="n">i</span> <span class="o">%</span> <span class="n">commit_every</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span> + <span class="n">conn</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span> + <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span> + <span class="s2">"Loaded </span><span class="si">{i}</span><span class="s2"> into </span><span class="si">{table}</span><span class="s2"> rows so far"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="o">**</span><span class="nb">locals</span><span class="p">())</span> + <span class="p">)</span> + + <span class="n">conn</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span> + <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span> + <span class="s2">"Done loading. Loaded a total of </span><span class="si">{i}</span><span class="s2"> rows"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="o">**</span><span class="nb">locals</span><span class="p">()))</span></div> + + <span class="nd">@staticmethod</span> + <span class="k">def</span> <span class="nf">_serialize_cell</span><span class="p">(</span><span class="n">cell</span><span class="p">,</span> <span class="n">conn</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Returns the SQL literal of the cell as a string.</span> + +<span class="sd"> :param cell: The cell to insert into the table</span> +<span class="sd"> :type cell: object</span> +<span class="sd"> :param conn: The database connection</span> +<span class="sd"> :type conn: connection object</span> +<span class="sd"> :return: The serialized cell</span> +<span class="sd"> :rtype: str</span> +<span class="sd"> """</span> + + <span class="k">if</span> <span class="n">cell</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> + <span class="k">return</span> <span class="kc">None</span> + <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">cell</span><span class="p">,</span> <span class="n">datetime</span><span class="p">):</span> + <span class="k">return</span> <span class="n">cell</span><span class="o">.</span><span class="n">isoformat</span><span class="p">()</span> + <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">cell</span><span class="p">)</span> + +<div class="viewcode-block" id="DbApiHook.bulk_dump"><a class="viewcode-back" href="../../../code.html#airflow.hooks.dbapi_hook.DbApiHook.bulk_dump">[docs]</a> <span class="k">def</span> <span class="nf">bulk_dump</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">table</span><span class="p">,</span> <span class="n">tmp_file</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Dumps a database table into a tab-delimited file</span> + +<span class="sd"> :param table: The name of the source table</span> +<span class="sd"> :type table: str</span> +<span class="sd"> :param tmp_file: The path of the target file</span> +<span class="sd"> :type tmp_file: str</span> +<span class="sd"> """</span> + <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span></div> + +<div class="viewcode-block" id="DbApiHook.bulk_load"><a class="viewcode-back" href="../../../code.html#airflow.hooks.dbapi_hook.DbApiHook.bulk_load">[docs]</a> <span class="k">def</span> <span class="nf">bulk_load</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">table</span><span class="p">,</span> <span class="n">tmp_file</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Loads a tab-delimited file into a database table</span> + +<span class="sd"> :param table: The name of the target table</span> +<span class="sd"> :type table: str</span> +<span class="sd"> :param tmp_file: The path of the file to load into the table</span> +<span class="sd"> :type tmp_file: str</span> +<span class="sd"> """</span> + <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span></div></div> +</pre></div> + + </div> + <div class="articleComments"> + + </div> + </div> + <footer> + + + <hr/> + + <div role="contentinfo"> + <p> + + </p> + </div> + Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/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"> + var DOCUMENTATION_OPTIONS = { + URL_ROOT:'../../../', + VERSION:'', + COLLAPSE_INDEX:false, + FILE_SUFFIX:'.html', + HAS_SOURCE: true, + SOURCELINK_SUFFIX: '.txt' + }; + </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/js/theme.js"></script> + + + + + <script type="text/javascript"> + jQuery(function () { + SphinxRtdTheme.StickyNav.enable(); + }); + </script> + + +</body> +</html> \ No newline at end of file