title: Pig Mapreduce to count letters in a row
body: <p>Instead of counting words I need to count letters.
  But I have problems implementing this using Apache Pig version 0.8.1-cdh3u1</p>
  
  <p>Given the following input:</p>
  
  <pre><code>989;850;abcccc
  29;395;aabbcc
  </code></pre>
  
  <p>The ouput should be:</p>
  
  <pre><code>989;850;a;1
  989;850;b;1
  989;850;c;4
  29;395;a;2
  29;395;b;2
  29;395;c;2
  </code></pre>
  
  <p>Here is what I tried:</p>
  
  <pre><code>A = LOAD 'input' using PigStorage(';') as (x:int, y:int, content:chararray);
  B = foreach A generate x, y, FLATTEN(STRSPLIT(content, '(?&lt;=.)(?=.)', 6)) as letters;
  C = foreach B generate x, y, FLATTEN(TOBAG(*)) as letters;
  D = foreach C generate x, y, letters.letters as letter;
  E = GROUP D BY (x,y,letter);
  F = foreach E generate group.x as x, group.y as y, group.letter as letter, COUNT(D.letter) as count;
  </code></pre>
  
  <p>A, B and C can be dumped, but "dump D" results in "ERROR 2997: Unable to recreate exception from backed error: java.lang.ClassCastException: java.lang.Integer cannot be cast to org.apache.pig.data.Tuple"</p>
  
  <p>dump C displays(despite the third value being a weird tuple):</p>
  
  <pre><code>(989,850,a)
  (989,850,b)
  (989,850,c)
  (989,850,c)
  (989,850,c)
  (989,850,c)
  (29,395,a)
  (29,395,a)
  (29,395,b)
  (29,395,b)
  (29,395,c)
  (29,395,c)
  </code></pre>
  
  <p>Here are the schemas:</p>
  
  <pre><code>grunt&gt; describe A; describe B; describe C; describe D; describe E; describe F;
  A: {x: int,y: int,content: chararray}
  B: {x: int,y: int,letters: bytearray}
  C: {x: int,y: int,letters: (x: int,y: int,letters: bytearray)}
  D: {x: int,y: int,letter: bytearray}
  E: {group: (x: int,y: int,letter: bytearray),D: {x: int,y: int,letter: bytearray}}
  F: {x: int,y: int,letter: bytearray,count: long}
  </code></pre>
  
  <p>This pig version doesn't seem to support TOBAG($2..$8), hence the TOBAG(*) which also includes x and y, but that could be sorted out synactically later...
  I'd like to avoid writing a UDF, otherwise I'd simply use the Java API directly.</p>
  
  <p>But I don't really get the cast error. Can someone please explain it.</p>