title: How can I use the map datatype in Apache Pig?
body: <p>I'd like to use Apache Pig to build a large key -> value mapping, look things up in the map, and iterate over the keys.  However, there does not even seem to be syntax for doing these things; I've checked the manual, wiki, sample code, Elephant book, Google, and even tried parsing the parser source.  Every single example loads map literals from a file... and then never uses them.  How can you use Pig's maps?</p>
  
  <p>First, there doesn't seem to be a way to load a 2-column CSV file into a map directly.  If I have a simple <code>map.csv</code>:</p>
  
  <pre><code>1,2
  3,4
  5,6
  </code></pre>
  
  <p>And I try to load it as a map:</p>
  
  <pre><code>m = load 'map.csv' using PigStorage(',') as (M: []);
  dump m;
  </code></pre>
  
  <p>I get three empty tuples:</p>
  
  <pre><code>()
  ()
  ()
  </code></pre>
  
  <p>So I try to load tuples and then generate the map:</p>
  
  <pre><code>m = load 'map.csv' using PigStorage(',') as (key:chararray, val:chararray);
  b = foreach m generate [key#val];
  ERROR 1000: Error during parsing. Encountered " "[" "[ "" at line 1, column 24.
  ...
  </code></pre>
  
  <p>Many variations on the syntax also fail (e.g., <code>generate [$0#$1]</code>).  </p>
  
  <p>OK, so I munge my map into Pig's map literal format as <code>map.pig</code>:</p>
  
  <pre><code>[1#2]
  [3#4]
  [5#6]
  </code></pre>
  
  <p>And load it up:</p>
  
  <pre><code>m = load 'map.pig' as (M: []);
  </code></pre>
  
  <p>Now let's load up some keys and try lookups:</p>
  
  <pre><code>k = load 'keys.csv' as (key);
  dump k;
  3
  5
  1
  
  c = foreach k generate m#key;  /* Or m[key], or... what? */
  ERROR 1000: Error during parsing.  Invalid alias: m in {M: map[ ]}
  </code></pre>
  
  <p>Hrm, OK, maybe since there are two relations involved, we need a join:</p>
  
  <pre><code>c = join k by key, m by /* ...um, what? */ $0;
  dump c;
  ERROR 1068: Using Map as key not supported.
  c = join k by key, m by m#key;
  dump c;
  Error 1000: Error during parsing. Invalid alias: m in {M: map[ ]}
  </code></pre>
  
  <p>Fail.  How do I refer to the key (or value) of a map?  The map schema syntax doesn't seem to let you even name the key and value (the mailing list says there's no way to assign types).</p>
  
  <p>Finally, I'd just like to be able to find all they keys in my map:</p>
  
  <pre><code>d = foreach m generate ...oh, forget it.
  </code></pre>
  
  <p>Is Pig's map type half-baked? What am I missing?</p>