title: Scala Spark - Count occurrences of a specific string in Dataframe column
body: <p>How can I count the occurrences of a String in a <code>df</code> Column using Spark partitioned by <code>id</code>?</p>
  
  <p>e.g. Find the value <code>"test"</code> in column <code>"name"</code> of a <code>df</code> </p>
  
  <p>In SQL would be:</p>
  
  <pre><code> SELECT
      SUM(CASE WHEN name = 'test' THEN 1 else 0 END) over window AS cnt_test
    FROM
      mytable
   WINDOW window AS (PARTITION BY id)
  </code></pre>
  
  <p>I've tried using <code>map( v =&gt; match { case "test" -&gt; 1.. })</code> </p>
  
  <p>and things like:</p>
  
  <pre><code>def getCount(df: DataFrame): DataFrame = {
      val dfCnt = df.agg(
            .withColumn("cnt_test", 
              count(col("name")==lit('test'))
  )
  </code></pre>
  
  <p>Is this an expensive operation? What could be the best approach to check for occurrences of a specific string and then perform an action <code>(sum, max, min, etc)</code>?</p>
  
  <p>thanks</p>