title: Spark Accumulator vs Count
body: <p>I have a use case where I want to count types of elements in an RDD matching some filter.</p>
  
  <p>e.g. RDD.filter(F1) and RDD.filter(!F1)</p>
  
  <p>I have 2 options</p>
  
  <ol>
  <li>Use accumulators: e.g.</li>
  </ol>
  
  <blockquote>
  <pre><code>LongAccumulator l1 = sparkContext.longAccumulator("Count1")
  LongAccumulator l2 = sparkContext.longAccumulator("Count2")
  RDD.forEachPartition(f -&gt; {
      if(F1) l1.add(1)
      else l2.add(1)
  });
  </code></pre>
  </blockquote>
  
  <ol start="2">
  <li>Use Count</li>
  </ol>
  
  <blockquote>
    <p><code>RDD.filter(F1).count(); RDD.filter(!F1).count()</code></p>
  </blockquote>
  
  <p>One benefit of the first approach is that we only need to iterate data once (useful since my data set is 10s of TB)</p>
  
  <p>What is the use of count if same affect can be achieved by using Accumulators ?</p>