<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>pyspark.streaming.dstream — PySpark master documentation</title>
<link rel="stylesheet" href="../../../_static/nature.css" type="text/css" />
<link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../../../',
VERSION: 'master',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</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>
<link rel="top" title="PySpark master documentation" href="../../../index.html" />
<link rel="up" title="Module code" href="../../index.html" />
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="nav-item nav-item-0"><a href="../../../index.html">PySpark master documentation</a> »</li>
<li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyspark.streaming.dstream</h1><div class="highlight"><pre>
<span class="c">#</span>
<span class="c"># Licensed to the Apache Software Foundation (ASF) under one or more</span>
<span class="c"># contributor license agreements. See the NOTICE file distributed with</span>
<span class="c"># this work for additional information regarding copyright ownership.</span>
<span class="c"># The ASF licenses this file to You under the Apache License, Version 2.0</span>
<span class="c"># (the "License"); you may not use this file except in compliance with</span>
<span class="c"># the License. You may obtain a copy of the License at</span>
<span class="c">#</span>
<span class="c"># http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c">#</span>
<span class="c"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c"># distributed under the License is distributed on an "AS IS" BASIS,</span>
<span class="c"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c"># See the License for the specific language governing permissions and</span>
<span class="c"># limitations under the License.</span>
<span class="c">#</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">operator</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">chain</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
<span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version</span> <span class="o"><</span> <span class="s">"3"</span><span class="p">:</span>
<span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">imap</span> <span class="k">as</span> <span class="nb">map</span><span class="p">,</span> <span class="n">ifilter</span> <span class="k">as</span> <span class="nb">filter</span>
<span class="kn">from</span> <span class="nn">py4j.protocol</span> <span class="kn">import</span> <span class="n">Py4JJavaError</span>
<span class="kn">from</span> <span class="nn">pyspark</span> <span class="kn">import</span> <span class="n">RDD</span>
<span class="kn">from</span> <span class="nn">pyspark.storagelevel</span> <span class="kn">import</span> <span class="n">StorageLevel</span>
<span class="kn">from</span> <span class="nn">pyspark.streaming.util</span> <span class="kn">import</span> <span class="n">rddToFileName</span><span class="p">,</span> <span class="n">TransformFunction</span>
<span class="kn">from</span> <span class="nn">pyspark.rdd</span> <span class="kn">import</span> <span class="n">portable_hash</span>
<span class="kn">from</span> <span class="nn">pyspark.resultiterable</span> <span class="kn">import</span> <span class="n">ResultIterable</span>
<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s">"DStream"</span><span class="p">]</span>
<div class="viewcode-block" id="DStream"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream">[docs]</a><span class="k">class</span> <span class="nc">DStream</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> A Discretized Stream (DStream), the basic abstraction in Spark Streaming,</span>
<span class="sd"> is a continuous sequence of RDDs (of the same type) representing a</span>
<span class="sd"> continuous stream of data (see L{RDD} in the Spark core documentation</span>
<span class="sd"> for more details on RDDs).</span>
<span class="sd"> DStreams can either be created from live data (such as, data from TCP</span>
<span class="sd"> sockets, Kafka, Flume, etc.) using a L{StreamingContext} or it can be</span>
<span class="sd"> generated by transforming existing DStreams using operations such as</span>
<span class="sd"> `map`, `window` and `reduceByKeyAndWindow`. While a Spark Streaming</span>
<span class="sd"> program is running, each DStream periodically generates a RDD, either</span>
<span class="sd"> from live data or by transforming the RDD generated by a parent DStream.</span>
<span class="sd"> DStreams internally is characterized by a few basic properties:</span>
<span class="sd"> - A list of other DStreams that the DStream depends on</span>
<span class="sd"> - A time interval at which the DStream generates an RDD</span>
<span class="sd"> - A function that is used to generate an RDD after each time interval</span>
<span class="sd"> """</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="n">jdstream</span><span class="p">,</span> <span class="n">ssc</span><span class="p">,</span> <span class="n">jrdd_deserializer</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_jdstream</span> <span class="o">=</span> <span class="n">jdstream</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_ssc</span> <span class="o">=</span> <span class="n">ssc</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_sc</span> <span class="o">=</span> <span class="n">ssc</span><span class="o">.</span><span class="n">_sc</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_jrdd_deserializer</span> <span class="o">=</span> <span class="n">jrdd_deserializer</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_cached</span> <span class="o">=</span> <span class="bp">False</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_checkpointed</span> <span class="o">=</span> <span class="bp">False</span>
<div class="viewcode-block" id="DStream.context"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.context">[docs]</a> <span class="k">def</span> <span class="nf">context</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return the StreamingContext associated with this DStream</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ssc</span>
</div>
<div class="viewcode-block" id="DStream.count"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.count">[docs]</a> <span class="k">def</span> <span class="nf">count</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream in which each RDD has a single element</span>
<span class="sd"> generated by counting each RDD of this DStream.</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">mapPartitions</span><span class="p">(</span><span class="k">lambda</span> <span class="n">i</span><span class="p">:</span> <span class="p">[</span><span class="nb">sum</span><span class="p">(</span><span class="mi">1</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="n">i</span><span class="p">)])</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">add</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="DStream.filter"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.filter">[docs]</a> <span class="k">def</span> <span class="nf">filter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">f</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream containing only the elements that satisfy predicate.</span>
<span class="sd"> """</span>
<span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="n">iterator</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">filter</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">iterator</span><span class="p">)</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">mapPartitions</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="bp">True</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="DStream.flatMap"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.flatMap">[docs]</a> <span class="k">def</span> <span class="nf">flatMap</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">preservesPartitioning</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream by applying a function to all elements of</span>
<span class="sd"> this DStream, and then flattening the results</span>
<span class="sd"> """</span>
<span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">iterator</span><span class="p">):</span>
<span class="k">return</span> <span class="n">chain</span><span class="o">.</span><span class="n">from_iterable</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">iterator</span><span class="p">))</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">mapPartitionsWithIndex</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">preservesPartitioning</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="DStream.map"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.map">[docs]</a> <span class="k">def</span> <span class="nf">map</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">preservesPartitioning</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream by applying a function to each element of DStream.</span>
<span class="sd"> """</span>
<span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="n">iterator</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">map</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">iterator</span><span class="p">)</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">mapPartitions</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">preservesPartitioning</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="DStream.mapPartitions"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.mapPartitions">[docs]</a> <span class="k">def</span> <span class="nf">mapPartitions</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">preservesPartitioning</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream in which each RDD is generated by applying</span>
<span class="sd"> mapPartitions() to each RDDs of this DStream.</span>
<span class="sd"> """</span>
<span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">iterator</span><span class="p">):</span>
<span class="k">return</span> <span class="n">f</span><span class="p">(</span><span class="n">iterator</span><span class="p">)</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">mapPartitionsWithIndex</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">preservesPartitioning</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="DStream.mapPartitionsWithIndex"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.mapPartitionsWithIndex">[docs]</a> <span class="k">def</span> <span class="nf">mapPartitionsWithIndex</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">preservesPartitioning</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream in which each RDD is generated by applying</span>
<span class="sd"> mapPartitionsWithIndex() to each RDDs of this DStream.</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="k">lambda</span> <span class="n">rdd</span><span class="p">:</span> <span class="n">rdd</span><span class="o">.</span><span class="n">mapPartitionsWithIndex</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">preservesPartitioning</span><span class="p">))</span>
</div>
<div class="viewcode-block" id="DStream.reduce"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.reduce">[docs]</a> <span class="k">def</span> <span class="nf">reduce</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">func</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream in which each RDD has a single element</span>
<span class="sd"> generated by reducing each RDD of this DStream.</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="p">(</span><span class="bp">None</span><span class="p">,</span> <span class="n">x</span><span class="p">))</span><span class="o">.</span><span class="n">reduceByKey</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
</div>
<div class="viewcode-block" id="DStream.reduceByKey"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.reduceByKey">[docs]</a> <span class="k">def</span> <span class="nf">reduceByKey</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="n">numPartitions</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream by applying reduceByKey to each RDD.</span>
<span class="sd"> """</span>
<span class="k">if</span> <span class="n">numPartitions</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">numPartitions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="o">.</span><span class="n">defaultParallelism</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">combineByKey</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="n">numPartitions</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="DStream.combineByKey"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.combineByKey">[docs]</a> <span class="k">def</span> <span class="nf">combineByKey</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">createCombiner</span><span class="p">,</span> <span class="n">mergeValue</span><span class="p">,</span> <span class="n">mergeCombiners</span><span class="p">,</span>
<span class="n">numPartitions</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream by applying combineByKey to each RDD.</span>
<span class="sd"> """</span>
<span class="k">if</span> <span class="n">numPartitions</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">numPartitions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="o">.</span><span class="n">defaultParallelism</span>
<span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="n">rdd</span><span class="p">):</span>
<span class="k">return</span> <span class="n">rdd</span><span class="o">.</span><span class="n">combineByKey</span><span class="p">(</span><span class="n">createCombiner</span><span class="p">,</span> <span class="n">mergeValue</span><span class="p">,</span> <span class="n">mergeCombiners</span><span class="p">,</span> <span class="n">numPartitions</span><span class="p">)</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">func</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="DStream.partitionBy"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.partitionBy">[docs]</a> <span class="k">def</span> <span class="nf">partitionBy</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">numPartitions</span><span class="p">,</span> <span class="n">partitionFunc</span><span class="o">=</span><span class="n">portable_hash</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a copy of the DStream in which each RDD are partitioned</span>
<span class="sd"> using the specified partitioner.</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="k">lambda</span> <span class="n">rdd</span><span class="p">:</span> <span class="n">rdd</span><span class="o">.</span><span class="n">partitionBy</span><span class="p">(</span><span class="n">numPartitions</span><span class="p">,</span> <span class="n">partitionFunc</span><span class="p">))</span>
</div>
<div class="viewcode-block" id="DStream.foreachRDD"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.foreachRDD">[docs]</a> <span class="k">def</span> <span class="nf">foreachRDD</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">func</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Apply a function to each RDD in this DStream.</span>
<span class="sd"> """</span>
<span class="k">if</span> <span class="n">func</span><span class="o">.</span><span class="n">__code__</span><span class="o">.</span><span class="n">co_argcount</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
<span class="n">old_func</span> <span class="o">=</span> <span class="n">func</span>
<span class="n">func</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">t</span><span class="p">,</span> <span class="n">rdd</span><span class="p">:</span> <span class="n">old_func</span><span class="p">(</span><span class="n">rdd</span><span class="p">)</span>
<span class="n">jfunc</span> <span class="o">=</span> <span class="n">TransformFunction</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jrdd_deserializer</span><span class="p">)</span>
<span class="n">api</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ssc</span><span class="o">.</span><span class="n">_jvm</span><span class="o">.</span><span class="n">PythonDStream</span>
<span class="n">api</span><span class="o">.</span><span class="n">callForeachRDD</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_jdstream</span><span class="p">,</span> <span class="n">jfunc</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="DStream.pprint"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.pprint">[docs]</a> <span class="k">def</span> <span class="nf">pprint</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">num</span><span class="o">=</span><span class="mi">10</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Print the first num elements of each RDD generated in this DStream.</span>
<span class="sd"> @param num: the number of elements from the first will be printed.</span>
<span class="sd"> """</span>
<span class="k">def</span> <span class="nf">takeAndPrint</span><span class="p">(</span><span class="n">time</span><span class="p">,</span> <span class="n">rdd</span><span class="p">):</span>
<span class="n">taken</span> <span class="o">=</span> <span class="n">rdd</span><span class="o">.</span><span class="n">take</span><span class="p">(</span><span class="n">num</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">"-------------------------------------------"</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">"Time: </span><span class="si">%s</span><span class="s">"</span> <span class="o">%</span> <span class="n">time</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">"-------------------------------------------"</span><span class="p">)</span>
<span class="k">for</span> <span class="n">record</span> <span class="ow">in</span> <span class="n">taken</span><span class="p">[:</span><span class="n">num</span><span class="p">]:</span>
<span class="k">print</span><span class="p">(</span><span class="n">record</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">taken</span><span class="p">)</span> <span class="o">></span> <span class="n">num</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">"..."</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">""</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">foreachRDD</span><span class="p">(</span><span class="n">takeAndPrint</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="DStream.mapValues"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.mapValues">[docs]</a> <span class="k">def</span> <span class="nf">mapValues</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">f</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream by applying a map function to the value of</span>
<span class="sd"> each key-value pairs in this DStream without changing the key.</span>
<span class="sd"> """</span>
<span class="n">map_values_fn</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">kv</span><span class="p">:</span> <span class="p">(</span><span class="n">kv</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">f</span><span class="p">(</span><span class="n">kv</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="n">map_values_fn</span><span class="p">,</span> <span class="n">preservesPartitioning</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="DStream.flatMapValues"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.flatMapValues">[docs]</a> <span class="k">def</span> <span class="nf">flatMapValues</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">f</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream by applying a flatmap function to the value</span>
<span class="sd"> of each key-value pairs in this DStream without changing the key.</span>
<span class="sd"> """</span>
<span class="n">flat_map_fn</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">kv</span><span class="p">:</span> <span class="p">((</span><span class="n">kv</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">f</span><span class="p">(</span><span class="n">kv</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">flatMap</span><span class="p">(</span><span class="n">flat_map_fn</span><span class="p">,</span> <span class="n">preservesPartitioning</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="DStream.glom"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.glom">[docs]</a> <span class="k">def</span> <span class="nf">glom</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream in which RDD is generated by applying glom()</span>
<span class="sd"> to RDD of this DStream.</span>
<span class="sd"> """</span>
<span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="n">iterator</span><span class="p">):</span>
<span class="k">yield</span> <span class="nb">list</span><span class="p">(</span><span class="n">iterator</span><span class="p">)</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">mapPartitions</span><span class="p">(</span><span class="n">func</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="DStream.cache"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.cache">[docs]</a> <span class="k">def</span> <span class="nf">cache</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Persist the RDDs of this DStream with the default storage level</span>
<span class="sd"> (C{MEMORY_ONLY_SER}).</span>
<span class="sd"> """</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_cached</span> <span class="o">=</span> <span class="bp">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">persist</span><span class="p">(</span><span class="n">StorageLevel</span><span class="o">.</span><span class="n">MEMORY_ONLY_SER</span><span class="p">)</span>
<span class="k">return</span> <span class="bp">self</span>
</div>
<div class="viewcode-block" id="DStream.persist"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.persist">[docs]</a> <span class="k">def</span> <span class="nf">persist</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">storageLevel</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Persist the RDDs of this DStream with the given storage level</span>
<span class="sd"> """</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_cached</span> <span class="o">=</span> <span class="bp">True</span>
<span class="n">javaStorageLevel</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="o">.</span><span class="n">_getJavaStorageLevel</span><span class="p">(</span><span class="n">storageLevel</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_jdstream</span><span class="o">.</span><span class="n">persist</span><span class="p">(</span><span class="n">javaStorageLevel</span><span class="p">)</span>
<span class="k">return</span> <span class="bp">self</span>
</div>
<div class="viewcode-block" id="DStream.checkpoint"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.checkpoint">[docs]</a> <span class="k">def</span> <span class="nf">checkpoint</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">interval</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Enable periodic checkpointing of RDDs of this DStream</span>
<span class="sd"> @param interval: time in seconds, after each period of that, generated</span>
<span class="sd"> RDD will be checkpointed</span>
<span class="sd"> """</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_checkpointed</span> <span class="o">=</span> <span class="bp">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_jdstream</span><span class="o">.</span><span class="n">checkpoint</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_ssc</span><span class="o">.</span><span class="n">_jduration</span><span class="p">(</span><span class="n">interval</span><span class="p">))</span>
<span class="k">return</span> <span class="bp">self</span>
</div>
<div class="viewcode-block" id="DStream.groupByKey"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.groupByKey">[docs]</a> <span class="k">def</span> <span class="nf">groupByKey</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">numPartitions</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream by applying groupByKey on each RDD.</span>
<span class="sd"> """</span>
<span class="k">if</span> <span class="n">numPartitions</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">numPartitions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="o">.</span><span class="n">defaultParallelism</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="k">lambda</span> <span class="n">rdd</span><span class="p">:</span> <span class="n">rdd</span><span class="o">.</span><span class="n">groupByKey</span><span class="p">(</span><span class="n">numPartitions</span><span class="p">))</span>
</div>
<div class="viewcode-block" id="DStream.countByValue"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.countByValue">[docs]</a> <span class="k">def</span> <span class="nf">countByValue</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream in which each RDD contains the counts of each</span>
<span class="sd"> distinct value in each RDD of this DStream.</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="bp">None</span><span class="p">))</span><span class="o">.</span><span class="n">reduceByKey</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">:</span> <span class="bp">None</span><span class="p">)</span><span class="o">.</span><span class="n">count</span><span class="p">()</span>
</div>
<div class="viewcode-block" id="DStream.saveAsTextFiles"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.saveAsTextFiles">[docs]</a> <span class="k">def</span> <span class="nf">saveAsTextFiles</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">prefix</span><span class="p">,</span> <span class="n">suffix</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Save each RDD in this DStream as at text file, using string</span>
<span class="sd"> representation of elements.</span>
<span class="sd"> """</span>
<span class="k">def</span> <span class="nf">saveAsTextFile</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">rdd</span><span class="p">):</span>
<span class="n">path</span> <span class="o">=</span> <span class="n">rddToFileName</span><span class="p">(</span><span class="n">prefix</span><span class="p">,</span> <span class="n">suffix</span><span class="p">,</span> <span class="n">t</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">rdd</span><span class="o">.</span><span class="n">saveAsTextFile</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
<span class="k">except</span> <span class="n">Py4JJavaError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
<span class="c"># after recovered from checkpointing, the foreachRDD may</span>
<span class="c"># be called twice</span>
<span class="k">if</span> <span class="s">'FileAlreadyExistsException'</span> <span class="ow">not</span> <span class="ow">in</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">):</span>
<span class="k">raise</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">foreachRDD</span><span class="p">(</span><span class="n">saveAsTextFile</span><span class="p">)</span>
<span class="c"># TODO: uncomment this until we have ssc.pickleFileStream()</span>
<span class="c"># def saveAsPickleFiles(self, prefix, suffix=None):</span>
<span class="c"># """</span>
<span class="c"># Save each RDD in this DStream as at binary file, the elements are</span>
<span class="c"># serialized by pickle.</span>
<span class="c"># """</span>
<span class="c"># def saveAsPickleFile(t, rdd):</span>
<span class="c"># path = rddToFileName(prefix, suffix, t)</span>
<span class="c"># try:</span>
<span class="c"># rdd.saveAsPickleFile(path)</span>
<span class="c"># except Py4JJavaError as e:</span>
<span class="c"># # after recovered from checkpointing, the foreachRDD may</span>
<span class="c"># # be called twice</span>
<span class="c"># if 'FileAlreadyExistsException' not in str(e):</span>
<span class="c"># raise</span>
<span class="c"># return self.foreachRDD(saveAsPickleFile)</span>
</div>
<div class="viewcode-block" id="DStream.transform"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.transform">[docs]</a> <span class="k">def</span> <span class="nf">transform</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">func</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream in which each RDD is generated by applying a function</span>
<span class="sd"> on each RDD of this DStream.</span>
<span class="sd"> `func` can have one argument of `rdd`, or have two arguments of</span>
<span class="sd"> (`time`, `rdd`)</span>
<span class="sd"> """</span>
<span class="k">if</span> <span class="n">func</span><span class="o">.</span><span class="n">__code__</span><span class="o">.</span><span class="n">co_argcount</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
<span class="n">oldfunc</span> <span class="o">=</span> <span class="n">func</span>
<span class="n">func</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">t</span><span class="p">,</span> <span class="n">rdd</span><span class="p">:</span> <span class="n">oldfunc</span><span class="p">(</span><span class="n">rdd</span><span class="p">)</span>
<span class="k">assert</span> <span class="n">func</span><span class="o">.</span><span class="n">__code__</span><span class="o">.</span><span class="n">co_argcount</span> <span class="o">==</span> <span class="mi">2</span><span class="p">,</span> <span class="s">"func should take one or two arguments"</span>
<span class="k">return</span> <span class="n">TransformedDStream</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">func</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="DStream.transformWith"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.transformWith">[docs]</a> <span class="k">def</span> <span class="nf">transformWith</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="n">other</span><span class="p">,</span> <span class="n">keepSerializer</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream in which each RDD is generated by applying a function</span>
<span class="sd"> on each RDD of this DStream and 'other' DStream.</span>
<span class="sd"> `func` can have two arguments of (`rdd_a`, `rdd_b`) or have three</span>
<span class="sd"> arguments of (`time`, `rdd_a`, `rdd_b`)</span>
<span class="sd"> """</span>
<span class="k">if</span> <span class="n">func</span><span class="o">.</span><span class="n">__code__</span><span class="o">.</span><span class="n">co_argcount</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
<span class="n">oldfunc</span> <span class="o">=</span> <span class="n">func</span>
<span class="n">func</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">t</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">oldfunc</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
<span class="k">assert</span> <span class="n">func</span><span class="o">.</span><span class="n">__code__</span><span class="o">.</span><span class="n">co_argcount</span> <span class="o">==</span> <span class="mi">3</span><span class="p">,</span> <span class="s">"func should take two or three arguments"</span>
<span class="n">jfunc</span> <span class="o">=</span> <span class="n">TransformFunction</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jrdd_deserializer</span><span class="p">,</span> <span class="n">other</span><span class="o">.</span><span class="n">_jrdd_deserializer</span><span class="p">)</span>
<span class="n">dstream</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="o">.</span><span class="n">_jvm</span><span class="o">.</span><span class="n">PythonTransformed2DStream</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_jdstream</span><span class="o">.</span><span class="n">dstream</span><span class="p">(),</span>
<span class="n">other</span><span class="o">.</span><span class="n">_jdstream</span><span class="o">.</span><span class="n">dstream</span><span class="p">(),</span> <span class="n">jfunc</span><span class="p">)</span>
<span class="n">jrdd_serializer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jrdd_deserializer</span> <span class="k">if</span> <span class="n">keepSerializer</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="o">.</span><span class="n">serializer</span>
<span class="k">return</span> <span class="n">DStream</span><span class="p">(</span><span class="n">dstream</span><span class="o">.</span><span class="n">asJavaDStream</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ssc</span><span class="p">,</span> <span class="n">jrdd_serializer</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="DStream.repartition"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.repartition">[docs]</a> <span class="k">def</span> <span class="nf">repartition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">numPartitions</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream with an increased or decreased level of parallelism.</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="k">lambda</span> <span class="n">rdd</span><span class="p">:</span> <span class="n">rdd</span><span class="o">.</span><span class="n">repartition</span><span class="p">(</span><span class="n">numPartitions</span><span class="p">))</span>
</div>
<span class="nd">@property</span>
<span class="k">def</span> <span class="nf">_slideDuration</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return the slideDuration in seconds of this DStream</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jdstream</span><span class="o">.</span><span class="n">dstream</span><span class="p">()</span><span class="o">.</span><span class="n">slideDuration</span><span class="p">()</span><span class="o">.</span><span class="n">milliseconds</span><span class="p">()</span> <span class="o">/</span> <span class="mf">1000.0</span>
<div class="viewcode-block" id="DStream.union"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.union">[docs]</a> <span class="k">def</span> <span class="nf">union</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream by unifying data of another DStream with this DStream.</span>
<span class="sd"> @param other: Another DStream having the same interval (i.e., slideDuration)</span>
<span class="sd"> as this DStream.</span>
<span class="sd"> """</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_slideDuration</span> <span class="o">!=</span> <span class="n">other</span><span class="o">.</span><span class="n">_slideDuration</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">"the two DStream should have same slide duration"</span><span class="p">)</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">transformWith</span><span class="p">(</span><span class="k">lambda</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">a</span><span class="o">.</span><span class="n">union</span><span class="p">(</span><span class="n">b</span><span class="p">),</span> <span class="n">other</span><span class="p">,</span> <span class="bp">True</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="DStream.cogroup"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.cogroup">[docs]</a> <span class="k">def</span> <span class="nf">cogroup</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">,</span> <span class="n">numPartitions</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream by applying 'cogroup' between RDDs of this</span>
<span class="sd"> DStream and `other` DStream.</span>
<span class="sd"> Hash partitioning is used to generate the RDDs with `numPartitions` partitions.</span>
<span class="sd"> """</span>
<span class="k">if</span> <span class="n">numPartitions</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">numPartitions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="o">.</span><span class="n">defaultParallelism</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">transformWith</span><span class="p">(</span><span class="k">lambda</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">a</span><span class="o">.</span><span class="n">cogroup</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">numPartitions</span><span class="p">),</span> <span class="n">other</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="DStream.join"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.join">[docs]</a> <span class="k">def</span> <span class="nf">join</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">,</span> <span class="n">numPartitions</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream by applying 'join' between RDDs of this DStream and</span>
<span class="sd"> `other` DStream.</span>
<span class="sd"> Hash partitioning is used to generate the RDDs with `numPartitions`</span>
<span class="sd"> partitions.</span>
<span class="sd"> """</span>
<span class="k">if</span> <span class="n">numPartitions</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">numPartitions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="o">.</span><span class="n">defaultParallelism</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">transformWith</span><span class="p">(</span><span class="k">lambda</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">a</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">numPartitions</span><span class="p">),</span> <span class="n">other</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="DStream.leftOuterJoin"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.leftOuterJoin">[docs]</a> <span class="k">def</span> <span class="nf">leftOuterJoin</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">,</span> <span class="n">numPartitions</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream by applying 'left outer join' between RDDs of this DStream and</span>
<span class="sd"> `other` DStream.</span>
<span class="sd"> Hash partitioning is used to generate the RDDs with `numPartitions`</span>
<span class="sd"> partitions.</span>
<span class="sd"> """</span>
<span class="k">if</span> <span class="n">numPartitions</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">numPartitions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="o">.</span><span class="n">defaultParallelism</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">transformWith</span><span class="p">(</span><span class="k">lambda</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">a</span><span class="o">.</span><span class="n">leftOuterJoin</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">numPartitions</span><span class="p">),</span> <span class="n">other</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="DStream.rightOuterJoin"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.rightOuterJoin">[docs]</a> <span class="k">def</span> <span class="nf">rightOuterJoin</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">,</span> <span class="n">numPartitions</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream by applying 'right outer join' between RDDs of this DStream and</span>
<span class="sd"> `other` DStream.</span>
<span class="sd"> Hash partitioning is used to generate the RDDs with `numPartitions`</span>
<span class="sd"> partitions.</span>
<span class="sd"> """</span>
<span class="k">if</span> <span class="n">numPartitions</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">numPartitions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="o">.</span><span class="n">defaultParallelism</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">transformWith</span><span class="p">(</span><span class="k">lambda</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">a</span><span class="o">.</span><span class="n">rightOuterJoin</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">numPartitions</span><span class="p">),</span> <span class="n">other</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="DStream.fullOuterJoin"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.fullOuterJoin">[docs]</a> <span class="k">def</span> <span class="nf">fullOuterJoin</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">,</span> <span class="n">numPartitions</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream by applying 'full outer join' between RDDs of this DStream and</span>
<span class="sd"> `other` DStream.</span>
<span class="sd"> Hash partitioning is used to generate the RDDs with `numPartitions`</span>
<span class="sd"> partitions.</span>
<span class="sd"> """</span>
<span class="k">if</span> <span class="n">numPartitions</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">numPartitions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="o">.</span><span class="n">defaultParallelism</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">transformWith</span><span class="p">(</span><span class="k">lambda</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">a</span><span class="o">.</span><span class="n">fullOuterJoin</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">numPartitions</span><span class="p">),</span> <span class="n">other</span><span class="p">)</span>
</div>
<span class="k">def</span> <span class="nf">_jtime</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">timestamp</span><span class="p">):</span>
<span class="sd">""" Convert datetime or unix_timestamp into Time</span>
<span class="sd"> """</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">timestamp</span><span class="p">,</span> <span class="n">datetime</span><span class="p">):</span>
<span class="n">timestamp</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">mktime</span><span class="p">(</span><span class="n">timestamp</span><span class="o">.</span><span class="n">timetuple</span><span class="p">())</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="o">.</span><span class="n">_jvm</span><span class="o">.</span><span class="n">Time</span><span class="p">(</span><span class="nb">long</span><span class="p">(</span><span class="n">timestamp</span> <span class="o">*</span> <span class="mi">1000</span><span class="p">))</span>
<div class="viewcode-block" id="DStream.slice"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.slice">[docs]</a> <span class="k">def</span> <span class="nf">slice</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">begin</span><span class="p">,</span> <span class="n">end</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return all the RDDs between 'begin' to 'end' (both included)</span>
<span class="sd"> `begin`, `end` could be datetime.datetime() or unix_timestamp</span>
<span class="sd"> """</span>
<span class="n">jrdds</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jdstream</span><span class="o">.</span><span class="n">slice</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_jtime</span><span class="p">(</span><span class="n">begin</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jtime</span><span class="p">(</span><span class="n">end</span><span class="p">))</span>
<span class="k">return</span> <span class="p">[</span><span class="n">RDD</span><span class="p">(</span><span class="n">jrdd</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jrdd_deserializer</span><span class="p">)</span> <span class="k">for</span> <span class="n">jrdd</span> <span class="ow">in</span> <span class="n">jrdds</span><span class="p">]</span>
</div>
<span class="k">def</span> <span class="nf">_validate_window_param</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">window</span><span class="p">,</span> <span class="n">slide</span><span class="p">):</span>
<span class="n">duration</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jdstream</span><span class="o">.</span><span class="n">dstream</span><span class="p">()</span><span class="o">.</span><span class="n">slideDuration</span><span class="p">()</span><span class="o">.</span><span class="n">milliseconds</span><span class="p">()</span>
<span class="k">if</span> <span class="nb">int</span><span class="p">(</span><span class="n">window</span> <span class="o">*</span> <span class="mi">1000</span><span class="p">)</span> <span class="o">%</span> <span class="n">duration</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">"windowDuration must be multiple of the slide duration (</span><span class="si">%d</span><span class="s"> ms)"</span>
<span class="o">%</span> <span class="n">duration</span><span class="p">)</span>
<span class="k">if</span> <span class="n">slide</span> <span class="ow">and</span> <span class="nb">int</span><span class="p">(</span><span class="n">slide</span> <span class="o">*</span> <span class="mi">1000</span><span class="p">)</span> <span class="o">%</span> <span class="n">duration</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">"slideDuration must be multiple of the slide duration (</span><span class="si">%d</span><span class="s"> ms)"</span>
<span class="o">%</span> <span class="n">duration</span><span class="p">)</span>
<div class="viewcode-block" id="DStream.window"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.window">[docs]</a> <span class="k">def</span> <span class="nf">window</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">windowDuration</span><span class="p">,</span> <span class="n">slideDuration</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream in which each RDD contains all the elements in seen in a</span>
<span class="sd"> sliding window of time over this DStream.</span>
<span class="sd"> @param windowDuration: width of the window; must be a multiple of this DStream's</span>
<span class="sd"> batching interval</span>
<span class="sd"> @param slideDuration: sliding interval of the window (i.e., the interval after which</span>
<span class="sd"> the new DStream will generate RDDs); must be a multiple of this</span>
<span class="sd"> DStream's batching interval</span>
<span class="sd"> """</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_validate_window_param</span><span class="p">(</span><span class="n">windowDuration</span><span class="p">,</span> <span class="n">slideDuration</span><span class="p">)</span>
<span class="n">d</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ssc</span><span class="o">.</span><span class="n">_jduration</span><span class="p">(</span><span class="n">windowDuration</span><span class="p">)</span>
<span class="k">if</span> <span class="n">slideDuration</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="k">return</span> <span class="n">DStream</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_jdstream</span><span class="o">.</span><span class="n">window</span><span class="p">(</span><span class="n">d</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ssc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jrdd_deserializer</span><span class="p">)</span>
<span class="n">s</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ssc</span><span class="o">.</span><span class="n">_jduration</span><span class="p">(</span><span class="n">slideDuration</span><span class="p">)</span>
<span class="k">return</span> <span class="n">DStream</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_jdstream</span><span class="o">.</span><span class="n">window</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">s</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ssc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jrdd_deserializer</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="DStream.reduceByWindow"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.reduceByWindow">[docs]</a> <span class="k">def</span> <span class="nf">reduceByWindow</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reduceFunc</span><span class="p">,</span> <span class="n">invReduceFunc</span><span class="p">,</span> <span class="n">windowDuration</span><span class="p">,</span> <span class="n">slideDuration</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream in which each RDD has a single element generated by reducing all</span>
<span class="sd"> elements in a sliding window over this DStream.</span>
<span class="sd"> if `invReduceFunc` is not None, the reduction is done incrementally</span>
<span class="sd"> using the old window's reduced value :</span>
<span class="sd"> 1. reduce the new values that entered the window (e.g., adding new counts)</span>
<span class="sd"> 2. "inverse reduce" the old values that left the window (e.g., subtracting old counts)</span>
<span class="sd"> This is more efficient than `invReduceFunc` is None.</span>
<span class="sd"> @param reduceFunc: associative reduce function</span>
<span class="sd"> @param invReduceFunc: inverse reduce function of `reduceFunc`</span>
<span class="sd"> @param windowDuration: width of the window; must be a multiple of this DStream's</span>
<span class="sd"> batching interval</span>
<span class="sd"> @param slideDuration: sliding interval of the window (i.e., the interval after which</span>
<span class="sd"> the new DStream will generate RDDs); must be a multiple of this</span>
<span class="sd"> DStream's batching interval</span>
<span class="sd"> """</span>
<span class="n">keyed</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">x</span><span class="p">))</span>
<span class="n">reduced</span> <span class="o">=</span> <span class="n">keyed</span><span class="o">.</span><span class="n">reduceByKeyAndWindow</span><span class="p">(</span><span class="n">reduceFunc</span><span class="p">,</span> <span class="n">invReduceFunc</span><span class="p">,</span>
<span class="n">windowDuration</span><span class="p">,</span> <span class="n">slideDuration</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="k">return</span> <span class="n">reduced</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">kv</span><span class="p">:</span> <span class="n">kv</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
</div>
<div class="viewcode-block" id="DStream.countByWindow"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.countByWindow">[docs]</a> <span class="k">def</span> <span class="nf">countByWindow</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">windowDuration</span><span class="p">,</span> <span class="n">slideDuration</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream in which each RDD has a single element generated</span>
<span class="sd"> by counting the number of elements in a window over this DStream.</span>
<span class="sd"> windowDuration and slideDuration are as defined in the window() operation.</span>
<span class="sd"> This is equivalent to window(windowDuration, slideDuration).count(),</span>
<span class="sd"> but will be more efficient if window is large.</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">reduceByWindow</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">add</span><span class="p">,</span> <span class="n">operator</span><span class="o">.</span><span class="n">sub</span><span class="p">,</span>
<span class="n">windowDuration</span><span class="p">,</span> <span class="n">slideDuration</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="DStream.countByValueAndWindow"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.countByValueAndWindow">[docs]</a> <span class="k">def</span> <span class="nf">countByValueAndWindow</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">windowDuration</span><span class="p">,</span> <span class="n">slideDuration</span><span class="p">,</span> <span class="n">numPartitions</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream in which each RDD contains the count of distinct elements in</span>
<span class="sd"> RDDs in a sliding window over this DStream.</span>
<span class="sd"> @param windowDuration: width of the window; must be a multiple of this DStream's</span>
<span class="sd"> batching interval</span>
<span class="sd"> @param slideDuration: sliding interval of the window (i.e., the interval after which</span>
<span class="sd"> the new DStream will generate RDDs); must be a multiple of this</span>
<span class="sd"> DStream's batching interval</span>
<span class="sd"> @param numPartitions: number of partitions of each RDD in the new DStream.</span>
<span class="sd"> """</span>
<span class="n">keyed</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
<span class="n">counted</span> <span class="o">=</span> <span class="n">keyed</span><span class="o">.</span><span class="n">reduceByKeyAndWindow</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">add</span><span class="p">,</span> <span class="n">operator</span><span class="o">.</span><span class="n">sub</span><span class="p">,</span>
<span class="n">windowDuration</span><span class="p">,</span> <span class="n">slideDuration</span><span class="p">,</span> <span class="n">numPartitions</span><span class="p">)</span>
<span class="k">return</span> <span class="n">counted</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">kv</span><span class="p">:</span> <span class="n">kv</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">></span> <span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">count</span><span class="p">()</span>
</div>
<div class="viewcode-block" id="DStream.groupByKeyAndWindow"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.groupByKeyAndWindow">[docs]</a> <span class="k">def</span> <span class="nf">groupByKeyAndWindow</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">windowDuration</span><span class="p">,</span> <span class="n">slideDuration</span><span class="p">,</span> <span class="n">numPartitions</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream by applying `groupByKey` over a sliding window.</span>
<span class="sd"> Similar to `DStream.groupByKey()`, but applies it over a sliding window.</span>
<span class="sd"> @param windowDuration: width of the window; must be a multiple of this DStream's</span>
<span class="sd"> batching interval</span>
<span class="sd"> @param slideDuration: sliding interval of the window (i.e., the interval after which</span>
<span class="sd"> the new DStream will generate RDDs); must be a multiple of this</span>
<span class="sd"> DStream's batching interval</span>
<span class="sd"> @param numPartitions: Number of partitions of each RDD in the new DStream.</span>
<span class="sd"> """</span>
<span class="n">ls</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">mapValues</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="p">[</span><span class="n">x</span><span class="p">])</span>
<span class="n">grouped</span> <span class="o">=</span> <span class="n">ls</span><span class="o">.</span><span class="n">reduceByKeyAndWindow</span><span class="p">(</span><span class="k">lambda</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">a</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="ow">or</span> <span class="n">a</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">a</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">b</span><span class="p">):],</span>
<span class="n">windowDuration</span><span class="p">,</span> <span class="n">slideDuration</span><span class="p">,</span> <span class="n">numPartitions</span><span class="p">)</span>
<span class="k">return</span> <span class="n">grouped</span><span class="o">.</span><span class="n">mapValues</span><span class="p">(</span><span class="n">ResultIterable</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="DStream.reduceByKeyAndWindow"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.reduceByKeyAndWindow">[docs]</a> <span class="k">def</span> <span class="nf">reduceByKeyAndWindow</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="n">invFunc</span><span class="p">,</span> <span class="n">windowDuration</span><span class="p">,</span> <span class="n">slideDuration</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
<span class="n">numPartitions</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">filterFunc</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new DStream by applying incremental `reduceByKey` over a sliding window.</span>
<span class="sd"> The reduced value of over a new window is calculated using the old window's reduce value :</span>
<span class="sd"> 1. reduce the new values that entered the window (e.g., adding new counts)</span>
<span class="sd"> 2. "inverse reduce" the old values that left the window (e.g., subtracting old counts)</span>
<span class="sd"> `invFunc` can be None, then it will reduce all the RDDs in window, could be slower</span>
<span class="sd"> than having `invFunc`.</span>
<span class="sd"> @param reduceFunc: associative reduce function</span>
<span class="sd"> @param invReduceFunc: inverse function of `reduceFunc`</span>
<span class="sd"> @param windowDuration: width of the window; must be a multiple of this DStream's</span>
<span class="sd"> batching interval</span>
<span class="sd"> @param slideDuration: sliding interval of the window (i.e., the interval after which</span>
<span class="sd"> the new DStream will generate RDDs); must be a multiple of this</span>
<span class="sd"> DStream's batching interval</span>
<span class="sd"> @param numPartitions: number of partitions of each RDD in the new DStream.</span>
<span class="sd"> @param filterFunc: function to filter expired key-value pairs;</span>
<span class="sd"> only pairs that satisfy the function are retained</span>
<span class="sd"> set this to null if you do not want to filter</span>
<span class="sd"> """</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_validate_window_param</span><span class="p">(</span><span class="n">windowDuration</span><span class="p">,</span> <span class="n">slideDuration</span><span class="p">)</span>
<span class="k">if</span> <span class="n">numPartitions</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">numPartitions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="o">.</span><span class="n">defaultParallelism</span>
<span class="n">reduced</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reduceByKey</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">numPartitions</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">reduceFunc</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="o">.</span><span class="n">reduceByKey</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">numPartitions</span><span class="p">)</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">union</span><span class="p">(</span><span class="n">b</span><span class="p">)</span><span class="o">.</span><span class="n">reduceByKey</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">numPartitions</span><span class="p">)</span> <span class="k">if</span> <span class="n">a</span> <span class="k">else</span> <span class="n">b</span>
<span class="k">if</span> <span class="n">filterFunc</span><span class="p">:</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">r</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">filterFunc</span><span class="p">)</span>
<span class="k">return</span> <span class="n">r</span>
<span class="k">def</span> <span class="nf">invReduceFunc</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="o">.</span><span class="n">reduceByKey</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">numPartitions</span><span class="p">)</span>
<span class="n">joined</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">leftOuterJoin</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">numPartitions</span><span class="p">)</span>
<span class="k">return</span> <span class="n">joined</span><span class="o">.</span><span class="n">mapValues</span><span class="p">(</span><span class="k">lambda</span> <span class="n">kv</span><span class="p">:</span> <span class="n">invFunc</span><span class="p">(</span><span class="n">kv</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">kv</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="k">if</span> <span class="n">kv</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="k">else</span> <span class="n">kv</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="n">jreduceFunc</span> <span class="o">=</span> <span class="n">TransformFunction</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="p">,</span> <span class="n">reduceFunc</span><span class="p">,</span> <span class="n">reduced</span><span class="o">.</span><span class="n">_jrdd_deserializer</span><span class="p">)</span>
<span class="k">if</span> <span class="n">invReduceFunc</span><span class="p">:</span>
<span class="n">jinvReduceFunc</span> <span class="o">=</span> <span class="n">TransformFunction</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="p">,</span> <span class="n">invReduceFunc</span><span class="p">,</span> <span class="n">reduced</span><span class="o">.</span><span class="n">_jrdd_deserializer</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">jinvReduceFunc</span> <span class="o">=</span> <span class="bp">None</span>
<span class="k">if</span> <span class="n">slideDuration</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">slideDuration</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_slideDuration</span>
<span class="n">dstream</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="o">.</span><span class="n">_jvm</span><span class="o">.</span><span class="n">PythonReducedWindowedDStream</span><span class="p">(</span><span class="n">reduced</span><span class="o">.</span><span class="n">_jdstream</span><span class="o">.</span><span class="n">dstream</span><span class="p">(),</span>
<span class="n">jreduceFunc</span><span class="p">,</span> <span class="n">jinvReduceFunc</span><span class="p">,</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_ssc</span><span class="o">.</span><span class="n">_jduration</span><span class="p">(</span><span class="n">windowDuration</span><span class="p">),</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_ssc</span><span class="o">.</span><span class="n">_jduration</span><span class="p">(</span><span class="n">slideDuration</span><span class="p">))</span>
<span class="k">return</span> <span class="n">DStream</span><span class="p">(</span><span class="n">dstream</span><span class="o">.</span><span class="n">asJavaDStream</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ssc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="o">.</span><span class="n">serializer</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="DStream.updateStateByKey"><a class="viewcode-back" href="../../../pyspark.streaming.html#pyspark.streaming.DStream.updateStateByKey">[docs]</a> <span class="k">def</span> <span class="nf">updateStateByKey</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">updateFunc</span><span class="p">,</span> <span class="n">numPartitions</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return a new "state" DStream where the state for each key is updated by applying</span>
<span class="sd"> the given function on the previous state of the key and the new values of the key.</span>
<span class="sd"> @param updateFunc: State update function. If this function returns None, then</span>
<span class="sd"> corresponding state key-value pair will be eliminated.</span>
<span class="sd"> """</span>
<span class="k">if</span> <span class="n">numPartitions</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">numPartitions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="o">.</span><span class="n">defaultParallelism</span>
<span class="k">def</span> <span class="nf">reduceFunc</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
<span class="k">if</span> <span class="n">a</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">g</span> <span class="o">=</span> <span class="n">b</span><span class="o">.</span><span class="n">groupByKey</span><span class="p">(</span><span class="n">numPartitions</span><span class="p">)</span><span class="o">.</span><span class="n">mapValues</span><span class="p">(</span><span class="k">lambda</span> <span class="n">vs</span><span class="p">:</span> <span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">vs</span><span class="p">),</span> <span class="bp">None</span><span class="p">))</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">g</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">cogroup</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">partitionBy</span><span class="p">(</span><span class="n">numPartitions</span><span class="p">),</span> <span class="n">numPartitions</span><span class="p">)</span>
<span class="n">g</span> <span class="o">=</span> <span class="n">g</span><span class="o">.</span><span class="n">mapValues</span><span class="p">(</span><span class="k">lambda</span> <span class="n">ab</span><span class="p">:</span> <span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">ab</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span> <span class="nb">list</span><span class="p">(</span><span class="n">ab</span><span class="p">[</span><span class="mi">0</span><span class="p">])[</span><span class="mi">0</span><span class="p">]</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">ab</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="k">else</span> <span class="bp">None</span><span class="p">))</span>
<span class="n">state</span> <span class="o">=</span> <span class="n">g</span><span class="o">.</span><span class="n">mapValues</span><span class="p">(</span><span class="k">lambda</span> <span class="n">vs_s</span><span class="p">:</span> <span class="n">updateFunc</span><span class="p">(</span><span class="n">vs_s</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">vs_s</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
<span class="k">return</span> <span class="n">state</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">k_v</span><span class="p">:</span> <span class="n">k_v</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">)</span>
<span class="n">jreduceFunc</span> <span class="o">=</span> <span class="n">TransformFunction</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="p">,</span> <span class="n">reduceFunc</span><span class="p">,</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="o">.</span><span class="n">serializer</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jrdd_deserializer</span><span class="p">)</span>
<span class="n">dstream</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="o">.</span><span class="n">_jvm</span><span class="o">.</span><span class="n">PythonStateDStream</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_jdstream</span><span class="o">.</span><span class="n">dstream</span><span class="p">(),</span> <span class="n">jreduceFunc</span><span class="p">)</span>
<span class="k">return</span> <span class="n">DStream</span><span class="p">(</span><span class="n">dstream</span><span class="o">.</span><span class="n">asJavaDStream</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ssc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="o">.</span><span class="n">serializer</span><span class="p">)</span>
</div></div>
<span class="k">class</span> <span class="nc">TransformedDStream</span><span class="p">(</span><span class="n">DStream</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> TransformedDStream is an DStream generated by an Python function</span>
<span class="sd"> transforming each RDD of an DStream to another RDDs.</span>
<span class="sd"> Multiple continuous transformations of DStream can be combined into</span>
<span class="sd"> one transformation.</span>
<span class="sd"> """</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="n">prev</span><span class="p">,</span> <span class="n">func</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_ssc</span> <span class="o">=</span> <span class="n">prev</span><span class="o">.</span><span class="n">_ssc</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_sc</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ssc</span><span class="o">.</span><span class="n">_sc</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_jrdd_deserializer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="o">.</span><span class="n">serializer</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_cached</span> <span class="o">=</span> <span class="bp">False</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_checkpointed</span> <span class="o">=</span> <span class="bp">False</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_jdstream_val</span> <span class="o">=</span> <span class="bp">None</span>
<span class="c"># Using type() to avoid folding the functions and compacting the DStreams which is not</span>
<span class="c"># not strictly a object of TransformedDStream.</span>
<span class="c"># Changed here is to avoid bug in KafkaTransformedDStream when calling offsetRanges().</span>
<span class="k">if</span> <span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">prev</span><span class="p">)</span> <span class="ow">is</span> <span class="n">TransformedDStream</span> <span class="ow">and</span>
<span class="ow">not</span> <span class="n">prev</span><span class="o">.</span><span class="n">is_cached</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">prev</span><span class="o">.</span><span class="n">is_checkpointed</span><span class="p">):</span>
<span class="n">prev_func</span> <span class="o">=</span> <span class="n">prev</span><span class="o">.</span><span class="n">func</span>
<span class="bp">self</span><span class="o">.</span><span class="n">func</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">t</span><span class="p">,</span> <span class="n">rdd</span><span class="p">:</span> <span class="n">func</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">prev_func</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">rdd</span><span class="p">))</span>
<span class="bp">self</span><span class="o">.</span><span class="n">prev</span> <span class="o">=</span> <span class="n">prev</span><span class="o">.</span><span class="n">prev</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">prev</span> <span class="o">=</span> <span class="n">prev</span>
<span class="bp">self</span><span class="o">.</span><span class="n">func</span> <span class="o">=</span> <span class="n">func</span>
<span class="nd">@property</span>
<span class="k">def</span> <span class="nf">_jdstream</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jdstream_val</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jdstream_val</span>
<span class="n">jfunc</span> <span class="o">=</span> <span class="n">TransformFunction</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">prev</span><span class="o">.</span><span class="n">_jrdd_deserializer</span><span class="p">)</span>
<span class="n">dstream</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sc</span><span class="o">.</span><span class="n">_jvm</span><span class="o">.</span><span class="n">PythonTransformedDStream</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">prev</span><span class="o">.</span><span class="n">_jdstream</span><span class="o">.</span><span class="n">dstream</span><span class="p">(),</span> <span class="n">jfunc</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_jdstream_val</span> <span class="o">=</span> <span class="n">dstream</span><span class="o">.</span><span class="n">asJavaDStream</span><span class="p">()</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jdstream_val</span>
</pre></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="../../../index.html">
<img class="logo" src="../../../_static/spark-logo-hd.png" alt="Logo"/>
</a></p>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="../../../search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="nav-item nav-item-0"><a href="../../../index.html">PySpark master documentation</a> »</li>
<li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright .
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.1.
</div>
</body>
</html>