@Override public Double exec(Tuple input) throws IOException { try { Double sum = sum(input); if(sum == null) { // either we were handed an empty bag or a bag // filled with nulls - return null in this case return null; } double count = count(input); Double avg = null; if (count > 0) avg = new Double(sum / count); return avg; } catch (ExecException ee) { throw ee; } }
@Override public Tuple exec(Tuple input) throws IOException { try { DataBag b = (DataBag)input.get(0); return combine(b); } catch (ExecException ee) { throw ee; } catch (Exception e) { int errCode = 2106; String msg = "Error while computing average in " + this.getClass().getSimpleName(); throw new ExecException(msg, errCode, PigException.BUG, e); } } }
@Override public Double exec(Tuple input) throws IOException { try { DataBag b = (DataBag)input.get(0); Tuple combined = combine(b); Double sum = (Double)combined.get(0); if(sum == null) { return null; } double count = (Long)combined.get(1); Double avg = null; if (count > 0) { avg = new Double(sum / count); } return avg; } catch (ExecException ee) { throw ee; } catch (Exception e) { int errCode = 2106; String msg = "Error while computing average in " + this.getClass().getSimpleName(); throw new ExecException(msg, errCode, PigException.BUG, e); } } }
@Override public void accumulate(Tuple b) throws IOException { try { Double sum = sum(b); if(sum == null) { return; } // set default values if (intermediateSum == null || intermediateCount == null) { intermediateSum = 0.0; intermediateCount = 0.0; } double count = (Long)count(b); if (count > 0) { intermediateCount += count; intermediateSum += sum; } } catch (ExecException ee) { throw ee; } catch (Exception e) { int errCode = 2106; String msg = "Error while computing average in " + this.getClass().getSimpleName(); throw new ExecException(msg, errCode, PigException.BUG, e); } }