title: Loading data with Hive, S3, EMR, and Recover Partitions
body: <p><strong>SOLVED:</strong> See Update #2 below for the 'solution' to this issue.</p>
  
  <p>~~~~~~~</p>
  
  <p>In s3, I have some log*.gz files stored in a nested directory structure like:</p>
  
  <pre><code>s3://($BUCKET)/y=2012/m=11/d=09/H=10/
  </code></pre>
  
  <p>I'm attempting to load these into Hive on Elastic Map Reduce (EMR), using a multi-level partition spec like:</p>
  
  <pre><code>create external table logs (content string)
  partitioned by (y string, m string, d string, h string)
  location 's3://($BUCKET)';
  </code></pre>
  
  <p>Creation of the table works. I then attempt to recover all of the existing partitions:</p>
  
  <pre><code>alter table logs recover partitions;
  </code></pre>
  
  <p>This seems to work and it does drill down through my s3 structure and add all the various levels of directories:</p>
  
  <pre><code>hive&gt; show partitions logs;
  OK
  y=2012/m=11/d=06/h=08
  y=2012/m=11/d=06/h=09
  y=2012/m=11/d=06/h=10
  y=2012/m=11/d=06/h=11
  y=2012/m=11/d=06/h=12
  y=2012/m=11/d=06/h=13
  y=2012/m=11/d=06/h=14
  y=2012/m=11/d=06/h=15
  y=2012/m=11/d=06/h=16
  ...
  </code></pre>
  
  <p>So it seems that Hive can see and interpret my file layout successfully. However, no actual data ever gets loaded. If I try to do a simple count or select *, I get nothing:</p>
  
  <pre><code>hive&gt; select count(*) from logs;
  ...
  OK
  0
  
  hive&gt; select * from logs limit 10;
  OK
  
  hive&gt; select * from logs where y = '2012' and m = '11' and d = '06' and h='16' limit 10;
  OK
  </code></pre>
  
  <p>Thoughts? Am I missing some additional command to load data beyond recovering the partitions? </p>
  
  <p>If I manually add a partition with an explicit location, then that works:</p>
  
  <pre><code>alter table logs2 add partition (y='2012', m='11', d='09', h='10') location 's3://($BUCKET)/y=2012/m=11/d=09/H=10/'
  </code></pre>
  
  <p>I can just write a script to do this, but it feels like I'm missing something fundamental w.r.t 'recover partitions'.</p>
  
  <p><strong>UPDATE #1</strong></p>
  
  <p>Thanks to a brilliant and keen observation by Joe K in a comment below, I think that case sensitivity issues might be involved here.</p>
  
  <p>The files are definitely organized like the following path spec, with a capitalized H (I think  this might be some nod to iso8601 formatting):</p>
  
  <pre><code>s3://($BUCKET)/y=2012/m=11/d=09/H=10/
  </code></pre>
  
  <p>I create my external table with a partition spec that does the proper capitalization:</p>
  
  <pre><code>partitioned by (y string, m string, d string, H string)
  </code></pre>
  
  <p>(Notice the 'H'). I do a recover partitions, which does seem to recurse through the directories and find the partitions appropriately, but somehow (despite using 'H' in all instructive places so far), it indeed seems that Hive saves it as a lower case 'h':</p>
  
  <pre><code>hive&gt; show partitions logs;
  OK
  y=2012/m=11/d=06/h=08
  </code></pre>
  
  <p>(Note the 'h'). So it seems that Hive is able to discover the partitions, but then stores them in a lowercase form ... Later when it goes to look for data, these paths are (of course) empty because S3 is case sensitive.</p>
  
  <p>I am going to move my data into an all-lowercase directory structure and see if that works... </p>
  
  <p><strong>UPDATE #2</strong></p>
  
  <p>Indeed, I have confirmed that the capitalized 'H' as a partition name (in the s3 file layout) was the problem here. As far as I can tell, this is what was happening:</p>
  
  <ul>
  <li>My layout on S3 had a case-sensitive partition name (H=)</li>
  <li>Running RECOVER PARTITIONS correctly discovers these partitions...</li>
  <li>But then they are stored internally as lowercase (h)</li>
  </ul>
  
  <p>The 'recover partitions' command is an extension of Hive authored by Amazon. I strongly suspect that the bug is in this component. To my knowledge native Hive has no concept of exploring a file root for partition discovery...</p>