Here is an example of code that uses the jAgg API for aggregate operations:
The Record class is an example class that stores data to aggregate:
public class Record { private String trialClass; private float testResult; public Record(String trialClass, float result) { this.trialClass = trialClass; testResult = result; } public String getTrialClass() { return trialClass; } public float getTestResult() { return testResult; } }
The following is code that generates a List of Records and calls the jAgg API for aggregate operations:
List<Record> rawData = new ArrayList<Record>(); rawData.add(new Record("placebo", 60.5)); rawData.add(new Record("placebo", 62.5)); rawData.add(new Record("placebo", 58.5)); rawData.add(new Record("newDrug", 80.5)); rawData.add(new Record("newDrug", 83.5)); rawData.add(new Record("newDrug", 77.5)); List<String> properties = new ArrayList<String>(); properties.add("trialClass"); List<Aggregator> aggregators = new ArrayList<Aggregator>(); aggregators.add(new CountAggregator("*")); aggregators.add(new AvgAggregator("testResult")); aggregators.add(new StdDevAggregator("testResult")); List<AggregateValue<Record>> aggValues = Aggregations.groupBy( rawData, properties, aggregators); for (AggregateValue<Record> aggValue : aggValues ) { Record r = aggValue.getObject(); StringBuffer buf = new StringBuffer(); buf.append(r.getTrialClass()); buf.append(":"); for (Aggregator aggregator : aggregators) { buf.append(" "); buf.append(aggregator.toString()); buf.append("="); buf.append(aggValue.getAggregateValue(aggregator)); } System.out.println(buf.toString()); }
Alternatively, using Multiset Discrimination, one would replace the call to "groupBy" above with the following line:
List<AggregateValue<Record>> aggValues = Aggregations.groupBy( rawData, properties, aggregators, true);
This code would output the following:
newDrug: Count(*)=3 Avg(testResult)=80.5 StdDev(testResult)=3 placebo: Count(*)=3 Avg(testResult)=60.5 StdDev(testResult)=2
Here is an example of code that uses the jAgg API for analytic operations:
The Sales class is an example class that stores sales data to analyze:
public class Sales { private String departmentName; private int quarterNumber; private long sales; public Sales(String departmentName, int quarterNumber, long sales) { this.departmentName = departmentName; this.quarterNumber = quarterNumber; this.sales = sales; } public String getDepartmentName() { return departmentName; } public int getQuarter() { return quarterNumber; } public long getSales() { return sales; } }
The following is code that generates a List of Sales and calls the jAgg API for analytic operations:
List<Sales> rawData = new ArrayList<Sales>(); rawData.add(new Sales("Widgets", 1, 100000)); rawData.add(new Sales("Widgets", 2, 115000)); rawData.add(new Sales("Widgets", 3, 95000)); rawData.add(new Sales("Widgets", 4, 125000)); rawData.add(new Sales("Gizmos" , 1, 50000)); rawData.add(new Sales("Gizmos" , 2, 80000)); rawData.add(new Sales("Gizmos" , 3, 90000)); rawData.add(new Sales("Gizmos" , 4, 140000)); List<AnalyticAggregator> analytics = new ArrayList<AnalyticAggregator>(); analytics.add(new AnalyticAggregator.Builder() .setAnalyticFunction(new SumAggregator("sales")) .setPartition(new PartitionClause(Arrays.asList("departmentName"))) .setOrderBy(new OrderByClause( Arrays.asList(new OrderByElement("quarter")))) .build()); analytics.add(new AnalyticAggregator.Builder() .setAnalyticFunction(new LagAnalytic("sales", 1, 0L)) .setPartition(new PartitionClause(Arrays.asList("departmentName"))) .setOrderBy(new OrderByClause( Arrays.asList(new OrderByElement("quarter")))) .build()); Analytic ana = new Analytic.Builder().setAnalytics(analytics).build(); List<AnalyticValue<Sales>> anaValues = ana.analyze(rawData); for (AnalyticValue<Sales> anaValue : anaValues ) { Sales s = anaValue.getObject(); StringBuffer buf = new StringBuffer(); buf.append(s.getDepartmentName()); buf.append(", Q"); buf.append(s.getQuarter()); buf.append(", $"); buf.append(s.getSales()); buf.append(", running total: $"); buf.append(anaValue.getAnalyzedValue(0)); buf.append(", change: $"); buf.append(s.getSales() - ((Number) anaValue.getAnalyzedValue(1)).longValue()); System.out.println(buf.toString()); }
This code would output the following:
Widgets, Q1, $100000, running total: $100000.0, change: $100000 Widgets, Q2, $115000, running total: $215000.0, change: $15000 Widgets, Q3, $95000, running total: $310000.0, change: $-20000 Widgets, Q4, $125000, running total: $435000.0, change: $30000 Gizmos, Q1, $50000, running total: $50000.0, change: $50000 Gizmos, Q2, $80000, running total: $130000.0, change: $30000 Gizmos, Q3, $90000, running total: $220000.0, change: $10000 Gizmos, Q4, $140000, running total: $360000.0, change: $50000