title: Spark lazy asynchronous count in scala
body: <p>I am currently building a Spark application and I'd like to log some stats from my intermediary RDDs. All I need is the size of the RDDs at different steps of the transformations.</p>
  
  <p>The transformations are linear, therefore I don't need to cache anything, except to call <code>.count()</code> (which doesn't need to be accurate nor synchronous).</p>
  
  <p>I could use <code>countAsync</code> to become asynchronous:</p>
  
  <pre><code>val rawData = sc.textFile(path)
  val (values, malformedRows) = parse(rawData)
  values.countAsync.onComplete(size =&gt; logger.info(s"${malformedRows.count} malformed rows while parsing ${path} (${size} successfully parsed)"))
  val result = values
      .map(operator)
      .filter(predicate)
  result.countAsync.onComplete(size =&gt; logger.info(s"${size} final rows"))
  result.saveAsTextFile(output)
  </code></pre>
  
  <p>However, my understanding is that the <code>countAsync</code> will trigger a new action (as <code>count</code> does). And since my RDDs will eventually be computed (by the last <code>saveAsTextFile</code> action), I'm wondering if it is possible to capitalize on the last action in order to log the stats without creating new actions.</p>
  
  <p>Is there any way to have an approximate asynchronous count without triggering a new action? In other word to have a lazy <code>count()</code>?</p>