blob: 708cbe45555a4c3d85ea4f8eb38ac7741695a94a [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.io.mongodbio module &mdash; Apache Beam 2.38.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" />
<link rel="next" title="apache_beam.io.parquetio module" href="apache_beam.io.parquetio.html" />
<link rel="prev" title="apache_beam.io.localfilesystem module" href="apache_beam.io.localfilesystem.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.38.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 class="current">
<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 current"><a class="reference internal" href="apache_beam.io.html">apache_beam.io package</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="apache_beam.io.html#subpackages">Subpackages</a></li>
<li class="toctree-l2 current"><a class="reference internal" href="apache_beam.io.html#submodules">Submodules</a><ul class="current">
<li class="toctree-l3"><a class="reference internal" href="apache_beam.io.avroio.html">apache_beam.io.avroio module</a></li>
<li class="toctree-l3"><a class="reference internal" href="apache_beam.io.concat_source.html">apache_beam.io.concat_source module</a></li>
<li class="toctree-l3"><a class="reference internal" href="apache_beam.io.debezium.html">apache_beam.io.debezium module</a></li>
<li class="toctree-l3"><a class="reference internal" href="apache_beam.io.filebasedsink.html">apache_beam.io.filebasedsink module</a></li>
<li class="toctree-l3"><a class="reference internal" href="apache_beam.io.filebasedsource.html">apache_beam.io.filebasedsource module</a></li>
<li class="toctree-l3"><a class="reference internal" href="apache_beam.io.fileio.html">apache_beam.io.fileio module</a></li>
<li class="toctree-l3"><a class="reference internal" href="apache_beam.io.filesystem.html">apache_beam.io.filesystem module</a></li>
<li class="toctree-l3"><a class="reference internal" href="apache_beam.io.filesystemio.html">apache_beam.io.filesystemio module</a></li>
<li class="toctree-l3"><a class="reference internal" href="apache_beam.io.filesystems.html">apache_beam.io.filesystems module</a></li>
<li class="toctree-l3"><a class="reference internal" href="apache_beam.io.hadoopfilesystem.html">apache_beam.io.hadoopfilesystem module</a></li>
<li class="toctree-l3"><a class="reference internal" href="apache_beam.io.iobase.html">apache_beam.io.iobase module</a></li>
<li class="toctree-l3"><a class="reference internal" href="apache_beam.io.jdbc.html">apache_beam.io.jdbc module</a></li>
<li class="toctree-l3"><a class="reference internal" href="apache_beam.io.kafka.html">apache_beam.io.kafka module</a></li>
<li class="toctree-l3"><a class="reference internal" href="apache_beam.io.kinesis.html">apache_beam.io.kinesis module</a></li>
<li class="toctree-l3"><a class="reference internal" href="apache_beam.io.localfilesystem.html">apache_beam.io.localfilesystem module</a></li>
<li class="toctree-l3 current"><a class="current reference internal" href="#">apache_beam.io.mongodbio module</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#read-from-mongodb">Read from MongoDB</a></li>
<li class="toctree-l4"><a class="reference internal" href="#write-to-mongodb">Write to MongoDB:</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="apache_beam.io.parquetio.html">apache_beam.io.parquetio module</a></li>
<li class="toctree-l3"><a class="reference internal" href="apache_beam.io.range_trackers.html">apache_beam.io.range_trackers module</a></li>
<li class="toctree-l3"><a class="reference internal" href="apache_beam.io.restriction_trackers.html">apache_beam.io.restriction_trackers module</a></li>
<li class="toctree-l3"><a class="reference internal" href="apache_beam.io.snowflake.html">apache_beam.io.snowflake module</a></li>
<li class="toctree-l3"><a class="reference internal" href="apache_beam.io.source_test_utils.html">apache_beam.io.source_test_utils module</a></li>
<li class="toctree-l3"><a class="reference internal" href="apache_beam.io.textio.html">apache_beam.io.textio module</a></li>
<li class="toctree-l3"><a class="reference internal" href="apache_beam.io.tfrecordio.html">apache_beam.io.tfrecordio module</a></li>
<li class="toctree-l3"><a class="reference internal" href="apache_beam.io.utils.html">apache_beam.io.utils module</a></li>
<li class="toctree-l3"><a class="reference internal" href="apache_beam.io.watermark_estimators.html">apache_beam.io.watermark_estimators module</a></li>
</ul>
</li>
</ul>
</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.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>
</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="apache_beam.io.html">apache_beam.io package</a> &raquo;</li>
<li>apache_beam.io.mongodbio module</li>
<li class="wy-breadcrumbs-aside">
<a href="_sources/apache_beam.io.mongodbio.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="module-apache_beam.io.mongodbio">
<span id="apache-beam-io-mongodbio-module"></span><h1>apache_beam.io.mongodbio module<a class="headerlink" href="#module-apache_beam.io.mongodbio" title="Permalink to this headline"></a></h1>
<p>This module implements IO classes to read and write data on MongoDB.</p>
<div class="section" id="read-from-mongodb">
<h2>Read from MongoDB<a class="headerlink" href="#read-from-mongodb" title="Permalink to this headline"></a></h2>
<p><a class="reference internal" href="#apache_beam.io.mongodbio.ReadFromMongoDB" title="apache_beam.io.mongodbio.ReadFromMongoDB"><code class="xref py py-class docutils literal notranslate"><span class="pre">ReadFromMongoDB</span></code></a> is a <code class="docutils literal notranslate"><span class="pre">PTransform</span></code> that reads from a configured
MongoDB source and returns a <code class="docutils literal notranslate"><span class="pre">PCollection</span></code> of dict representing MongoDB
documents.
To configure MongoDB source, the URI to connect to MongoDB server, database
name, collection name needs to be provided.</p>
<p>Example usage:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">pipeline</span> <span class="o">|</span> <span class="n">ReadFromMongoDB</span><span class="p">(</span><span class="n">uri</span><span class="o">=</span><span class="s1">&#39;mongodb://localhost:27017&#39;</span><span class="p">,</span>
<span class="n">db</span><span class="o">=</span><span class="s1">&#39;testdb&#39;</span><span class="p">,</span>
<span class="n">coll</span><span class="o">=</span><span class="s1">&#39;input&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>To read from MongoDB Atlas, use <code class="docutils literal notranslate"><span class="pre">bucket_auto</span></code> option to enable
<code class="docutils literal notranslate"><span class="pre">&#64;bucketAuto</span></code> MongoDB aggregation instead of <code class="docutils literal notranslate"><span class="pre">splitVector</span></code>
command which is a high-privilege function that cannot be assigned
to any user in Atlas.</p>
<p>Example usage:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">pipeline</span> <span class="o">|</span> <span class="n">ReadFromMongoDB</span><span class="p">(</span><span class="n">uri</span><span class="o">=</span><span class="s1">&#39;mongodb+srv://user:pwd@cluster0.mongodb.net&#39;</span><span class="p">,</span>
<span class="n">db</span><span class="o">=</span><span class="s1">&#39;testdb&#39;</span><span class="p">,</span>
<span class="n">coll</span><span class="o">=</span><span class="s1">&#39;input&#39;</span><span class="p">,</span>
<span class="n">bucket_auto</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="write-to-mongodb">
<h2>Write to MongoDB:<a class="headerlink" href="#write-to-mongodb" title="Permalink to this headline"></a></h2>
<p><a class="reference internal" href="#apache_beam.io.mongodbio.WriteToMongoDB" title="apache_beam.io.mongodbio.WriteToMongoDB"><code class="xref py py-class docutils literal notranslate"><span class="pre">WriteToMongoDB</span></code></a> is a <code class="docutils literal notranslate"><span class="pre">PTransform</span></code> that writes MongoDB documents to
configured sink, and the write is conducted through a mongodb bulk_write of
<code class="docutils literal notranslate"><span class="pre">ReplaceOne</span></code> operations. If the document’s _id field already existed in the
MongoDB collection, it results in an overwrite, otherwise, a new document
will be inserted.</p>
<p>Example usage:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">pipeline</span> <span class="o">|</span> <span class="n">WriteToMongoDB</span><span class="p">(</span><span class="n">uri</span><span class="o">=</span><span class="s1">&#39;mongodb://localhost:27017&#39;</span><span class="p">,</span>
<span class="n">db</span><span class="o">=</span><span class="s1">&#39;testdb&#39;</span><span class="p">,</span>
<span class="n">coll</span><span class="o">=</span><span class="s1">&#39;output&#39;</span><span class="p">,</span>
<span class="n">batch_size</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
</pre></div>
</div>
<p>No backward compatibility guarantees. Everything in this module is experimental.</p>
<dl class="function">
<dt id="apache_beam.io.mongodbio.ReadFromMongoDB">
<code class="descclassname">apache_beam.io.mongodbio.</code><code class="descname">ReadFromMongoDB</code><span class="sig-paren">(</span><em>uri='mongodb://localhost:27017'</em>, <em>db=None</em>, <em>coll=None</em>, <em>filter=None</em>, <em>projection=None</em>, <em>extra_client_params=None</em>, <em>bucket_auto=False</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/apache_beam/io/mongodbio.html#ReadFromMongoDB"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#apache_beam.io.mongodbio.ReadFromMongoDB" title="Permalink to this definition"></a></dt>
<dd><p>A <code class="docutils literal notranslate"><span class="pre">PTransform</span></code> to read MongoDB documents into a <code class="docutils literal notranslate"><span class="pre">PCollection</span></code>.</p>
</dd></dl>
<dl class="function">
<dt id="apache_beam.io.mongodbio.WriteToMongoDB">
<code class="descclassname">apache_beam.io.mongodbio.</code><code class="descname">WriteToMongoDB</code><span class="sig-paren">(</span><em>uri='mongodb://localhost:27017'</em>, <em>db=None</em>, <em>coll=None</em>, <em>batch_size=100</em>, <em>extra_client_params=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/apache_beam/io/mongodbio.html#WriteToMongoDB"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#apache_beam.io.mongodbio.WriteToMongoDB" title="Permalink to this definition"></a></dt>
<dd><p>WriteToMongoDB is a <code class="docutils literal notranslate"><span class="pre">PTransform</span></code> that writes a <code class="docutils literal notranslate"><span class="pre">PCollection</span></code> of
mongodb document to the configured MongoDB server.</p>
<p>In order to make the document writes idempotent so that the bundles are
retry-able without creating duplicates, the PTransform added 2 transformations
before final write stage:
a <code class="docutils literal notranslate"><span class="pre">GenerateId</span></code> transform and a <code class="docutils literal notranslate"><span class="pre">Reshuffle</span></code> transform.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span> <span class="o">-----------------------------------------------</span>
<span class="n">Pipeline</span> <span class="o">--&gt;</span> <span class="o">|</span><span class="n">GenerateId</span> <span class="o">--&gt;</span> <span class="n">Reshuffle</span> <span class="o">--&gt;</span> <span class="n">WriteToMongoSink</span><span class="o">|</span>
<span class="o">-----------------------------------------------</span>
<span class="p">(</span><span class="n">WriteToMongoDB</span><span class="p">)</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">GenerateId</span></code> transform adds a random and unique*_id* field to the
documents if they don’t already have one, it uses the same format as MongoDB
default. The <code class="docutils literal notranslate"><span class="pre">Reshuffle</span></code> transform makes sure that no fusion happens between
<code class="docutils literal notranslate"><span class="pre">GenerateId</span></code> and the final write stage transform,so that the set of
documents and their unique IDs are not regenerated if final write step is
retried due to a failure. This prevents duplicate writes of the same document
with different unique IDs.</p>
</dd></dl>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="apache_beam.io.parquetio.html" class="btn btn-neutral float-right" title="apache_beam.io.parquetio module" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="apache_beam.io.localfilesystem.html" class="btn btn-neutral float-left" title="apache_beam.io.localfilesystem module" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<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>