title: Replace a value of a field in a tuple in Pig
body: <p>I have two data sets to join. Here is what they look like:</p>
  
  <pre><code> Dataset 1
   ---#----#----#------------#----#-----#-----
   F1 | F2 | F3 | UID        | F4 | ... | F32
   ---#----#----#------------#----#-----#-----
   a1 | b1 | c1 | MD5 value1 | d1 | ... | foo1
   a2 | b2 | c2 | MD5 value2 | d2 | ... | foo2
   a3 | b3 | c3 | MD5 value3 | d3 | ... | foo3
   a4 | b4 | c4 | MD5 value4 | d4 | ... | foo4
  
   Dataset 2
   ----#-----#--------------#-----#-----#-----
   Z1  | Z2  | UID+Constant | Z3  | ... | Z62
   ----#-----#--------------#-----#-----#-----
   p1  | q1  | MD5 value1+C | s1  | ... | t1
   p2  | q2  | MD5 value2+C | s2  | ... | t2
   p3  | q3  | MD5 value3+C | s3  | ... | t3
   p4  | q4  | MD5 value4+C | s4  | ... | t4
  </code></pre>
  
  <p>Now I used explicit Field names to load both the datasets:</p>
  
  <pre><code>D1 = LOAD '/dataset1' USING PigStorage(',')
       AS (F1:chararray, F2:chararray, F3:chararray, UID:chararray, ... , F32:chararray);
  
  D2 = LOAD '/dataset2' USING PigStorage(',')
       AS (Z1:chararray, Z2:chararray, UID_C:chararray, ... , Z62:chararray);
  </code></pre>
  
  <p>Now, I want to get the combined output from both the datasets when I join on UID value. So, I split the <code>UID_C</code> field in <code>D2</code> using <code>REGEX_EXTRACT_ALL</code>. Here is how I did that.</p>
  
  <pre><code>D2_SPLIT_UID_C = FOREACH D2 GENERATE *,
                      FLATTEN(REGEX_EXTRACT_ALL(UID_C, '^(.*)\+(.*)$'))
                          AS (UID:chararray, C:chararray);
  </code></pre>
  
  <p>And then I join on <code>UID</code> on both the datasets.</p>
  
  <pre><code>JOINED_DATA = JOIN D1 BY UID, D2_SPLIT_UID_C BY UID;
  </code></pre>
  
  <p>All good till here, but now, I have to generate the output without the <code>UID_C</code> field. Instead, I need to replace it with the value of <code>UID</code>. Also, I do not need the <code>Constant(C)</code> in my final output.</p>
  
  <p>So, I need to generate the data first from <code>D1</code> and then from <code>D2</code>. So here is the output header of my final output</p>
  
  <pre><code>---#----#----#------------#----#-----#-----#----#----#-----#----#-----#----
  F1 | F2 | F3 | UID        | F4 | ... | F32 | Z1 | Z2 | UID | Z3 | ... | Z62
  ---#----#----#------------#----#-----#-----#----#----#-----#----#-----#----
  </code></pre>
  
  <p>This is where it gets ugly. I need to put all the fields from dataset1 and dataset2 again for my final <code>FOREACH GENERATE F1, F2, F3, UID, ...., F32, Z1, Z2, UID, Z3, ...., Z62</code>. This way, if my schema changes tomorrow, I will have to update it here as well.</p>
  
  <p>Can't I replace the <code>UID_C</code> column value with the UID value and then I would just need to use <code>FOREACH GENERATE *</code> and I will be done?</p>