title: Conditional sum over data using Apache Pig Latin
body: <p>I'm trying to do some log processing using Apache Pig Latin, and I was wondering if there was an easier way to do this:</p>
  
  <pre><code>filtered_logs = FOREACH logs GENERATE numDay, reqSize, optimizedSize, origSize, compressionPct, cacheStatus;
  
  grouped_logs = GROUP filtered_logs BY numDay;
  
  results = FOREACH grouped_logs GENERATE group,
  (SUM(filtered_logs.reqSize) + SUM(filtered_logs.optimizedSize)) / 1048576.00 AS     ClientThroughputMB,
  (SUM(filtered_logs.reqSize) + SUM(filtered_logs.origSize)) / 1048576.00 AS ServerThroughputMB,
  SUM(filtered_logs.origSize) / 1048576.00 AS OrigMB,
  SUM(filtered_logs.optimizedSize) / 1048576.00 AS OptMB,
  SUM(filtered_logs.reqSize) / 1048576.00 AS SentMB,
  AVG(filtered_logs.compressionPct) AS CompressionAvg,
  COUNT(filtered_logs) AS NumLogs;
  
  cache_hit_logs = FILTER filtered_logs BY cacheStatus MATCHES '.*HIT.*';
  
  grouped_cache_hit_logs = GROUP cache_hit_logs BY numDay;
  
  cache_hits = FOREACH grouped_cache_hit_logs GENERATE group,
  COUNT(cache_hit_logs) AS cnt;
  
  final_results = JOIN results BY group, cache_hits BY group;
  DUMP final_results;
  </code></pre>
  
  <p>(logs is defined, it's basically reading in a pipe delimited log file and assigning the fields)</p>
  
  <p>What I'm trying to do here is calculate the number of instances the field cacheStatus contains "HIT", and also calculating other data, such as OrigMB, CompressionAvg, NumLogs etc. This current code works, but it seems to have a huge performance overhead. Is there a way in Pig Latin to do something along the lines of this (in MSSQL)?</p>
  
  <pre><code>SUM(CASE CacheStatus WHEN 'HIT' THEN 1 else 0 END) as CacheHit
  </code></pre>
  
  <p>(basically, I don't want to process the log multiple times, I'd rather do it together all at once)</p>
  
  <p>Sorry if my question is worded confusingly, I'm pretty new to Pig Latin.</p>