Exforsys

Home arrow Reviews arrow Oracle Coherence

Oracle Coherence: Built-in Aggregators

Author: Packt Publishing     Published on: 9th Jul 2010

Just as with filters, Coherence ships with a number of useful built-in aggregators, and an equivalent of the AverageAggregator is one of them.

Ads

Actually, there are two average aggregators built-in, as you can see in the following table:

Sample Code
  1. ----------------------------------------------------------------------------------
  2.  Filter Class               Description
  3. ----------------------------------------------------------------------------------
  4. BigDecimalAverage       Calculates the average for a set of numeric values extracted from the cache entries and returns the result as a BigDecimal
  5. BigDecimalMax   Returns the maximum value, as a BigDecimal, for a set of numeric values extracted from the cache entries.
  6. BigDecimalMin   Returns the minimum value, as a BigDecimal, for a set of numeric values extracted from the cache entries.
  7. BigDecimalSum   Calculates the sum for a set of numeric values extracted from the cache entries and returns the result as a BigDecimal.
  8. DoubleAverage   Calculates the average for a set of numeric values extracted from the cache entries and returns the result as a Double.
  9. DoubleMax               Returns the maximum value, as a Double, for a set of numeric values extracted from the cache entries.
  10. DoubleMin               Returns the minimum value, as a Double, for a set of numeric values extracted from the cache entries.
  11. DoubleSum               Calculates the sum for a set of numeric values extracted from the cache entries and returns the result as a Double.
  12. LongMax         Returns the maximum value, as a Long, for a set of numeric values extracted from the cache entries.
  13. LongMin         Returns the minimum value, as a Long, for a set of numeric values extracted from the cache entries.
  14. LongSum         Calculates the sum for a set of numeric values extracted from the cache entries and returns the result as a Long.
  15. ComparableMax   Returns the maximum value for a set of Comparable values extracted from the cache entries.
  16. ComparableMin   Returns the minimum value for a set of Comparable values extracted from the cache entries.
  17. Count           Returns the number of values in an entry set equivalent to SQL's "select count(*)".
  18. DistinctValues  Returns a set of unique values extracted from the cache entries equivalent to SQL's "select distinct".
  19. CompositeAggregator Executes a collection of aggregators against the same entry set and returns the list of results, one for each aggregator in the collection.
  20. GroupAggregator A wrapper aggregator that allows you to split entries in a set based on some criteria and to aggregate each subset separately and independently.
Copyright exforsys.com


The important thing to note about the various average, max, min, and sum aggregators is that they differ from each other in how they treat the numeric values they are aggregating, as well as by the type of the return value.

For example, while you can use the DoubleAverage aggregator to calculate the average for any set of java.lang.Number-derived values, you should be aware that each individual value will be converted to Double first using the Number.doubleValue method, which might lead to rounding errors. What you will typically want to do is use the most appropriate aggregator based on the actual type of the values you are aggregating, and convert the final result to the desired type if necessary.

Using aggregators

So far we have learned how to implement an aggregator and which aggregators are shipped with Coherence, but we haven't learned how to use them yet.

In order to execute an aggregator, you need to use one of the methods defined by the com.tangosol.util.InvocableMap interface:

Sample Code
  1.    public interface InvocableMap extends Map {
  2.      Object aggregate(Collection keys,
  3.                       InvocableMap.EntryAggregator aggregator)
  4.  
  5.        Object aggregate(Filter filter,
  6.                         InvocableMap.EntryAggregator aggregator)
  7.    }
  8.  
Copyright exforsys.com


There are few more methods in the InvocableMap interface that we will cover in more detail in the next chapter, but these two are all we need to execute aggregators against cache entries.

Ads

The first overload of the aggregate method accepts an explicit collection of keys for a set of entries to aggregate, while the second one uses a filter to determine the set of entries aggregation should be performed on. Both methods accept an aggregator instance as a second argument, which can be either one of the built-in aggregators or a custom aggregator you have implemented.



 
This tutorial is part of a Oracle Coherence tutorial series. Read it from the beginning and learn yourself.

Oracle Coherence

 

Comments