title: Counting elements for each group using Pig
body: <p>I'm trying to group and count the frequency of terms for each group in PigLatin, but I'm having some troubles to figure it out how to do it. </p>
  
  <p>I have a collection of objects with the following schema:</p>
  
  <pre><code>{cluster_id: bytearray,terms: chararray}
  </code></pre>
  
  <p>And here is some samples</p>
  
  <pre><code>(10, smerter)
  (10, graviditeten)
  (10, smerter)
  (10, smerter)
  (10, udemærket)    
  (20, eis feuer)
  (20, herunterladen schau)
  (20, download gratis)
  (20, download gratis) 
  (30, anschauen kinofilm)
  (30, kauf rechnung)
  (30, kauf rechnung)
  (30, versandkostenfreie lieferung)
  (30, kostenlose)
  (30, kostenlose)
  (30, kostenlose) 
  </code></pre>
  
  <p>the result I m trying to get is something like this</p>
  
  <pre><code>(10, smerter, 3)
  (10, graviditeten, 2)
  (10, udemærket, 1)
  (20, download gratis, 2)
  (20, eis feuer, 1)
  (20, herunterladen schau, 1)    
  (30, kostenlose, 3)
  (30, kauf rechnung, 2)
  (30, anschauen kinofilm, 1)
  (30, versandkostenfreie lieferung, 1)
  </code></pre>
  
  <p>What would be the best way to do that?
  The following code groups by id and count the terms, but I wanted to count the terms for each group.</p>
  
  <pre><code>by_clusters = GROUP sample_data by cluster_id;
  by_clusters_terms_count = FOREACH by_clusters GENERATE group as cluster_id, COUNT($1);
  </code></pre>
  
  <p>I make the grouping like this I end up with an object with the following schema</p>
  
  <pre><code>by_clusters: {group: bytearray,sample_data: {(cluster_id: bytearray,terms: chararray)}}
  </code></pre>
  
  <p>Now, I get to the point to actually count the terms inside the 'sample_data' tuple.
  I'm thinking about nested foreach, but I still didn't get it how could I apply it in this case.
  The code would be something like the following:</p>
  
  <pre><code>result = FOREACH by_clusters {
  
  --count terms here, I don't know how
  
  -- compiler gives me an error here
  c = GROUP $1 BY terms; -- 
  d = FOREACH c GENERATE COUNT(b), group;
  
  GENERATE cluster_id, d;
  }
  </code></pre>
  
  <p>Error I get:</p>
  
  <blockquote>
    <p>ERROR 1200:   Syntax error, unexpected symbol at or near '$1</p>
  </blockquote>
  
  <p>Finally, I think I'm close, but I'm unable to solve it. 
  I don't believe I'll have to write an UDF in this case.</p>