title: Calculate count of distinct values of a field using pig script
body: <p>For a file of the form</p>
  
  <pre><code>A B user1
  C D user2
  A D user3
  A D user1
  </code></pre>
  
  <p>I want to calculate the count of distinct values of field 3 i.e. <code>count(distinct(user1, user2,user2,user1)) = 3</code></p>
  
  <p>I am doing this using the following pig script</p>
  
  <pre><code>A =  load 'myTestData' using PigStorage('\t') as (a1,a2,a3); 
  
  user_list = foreach A GENERATE $2;
  unique_users = DISTINCT user_list;
  unique_users_group = GROUP unique_users ALL;
  uu_count = FOREACH unique_users_group GENERATE COUNT(unique_users);
  store uu_count into 'output';
  </code></pre>
  
  <p>Is there a better way to get count of distinct values of a field?</p>