Database Reference
In-Depth Information
This section describes the implementation of the first feature as a generic
class that can be used in a variety of situations. It then implements the
second piece for the Redis key-value store discussed in Chapter 6, “Storing
Streaming Data.” Redis was chosen because it implements a variety of
operations as well as supporting expiration in a single back end.
Defining Aggregates
The core of the
Aggregator
class is the
Aggregate
class. This class
defines the resolution of the aggregate as well as its retention time. The
class itself is fairly simple, consisting mostly of convenience functions that
are in the complete source. The part presented here is the functional part
of the class. The first part of the class defines formatters for the various
aggregation resolutions. These formats are chosen over simple millisecond
times because they are a bit more readable without sacrificing the numerical
and lexicographical ordering:
public class
Aggregate {
private static final
SimpleDateFormat
millisecondFormatter
=
new
SimpleDateFormat("yyyyMMddHHmmssSSS");
private static final
SimpleDateFormat
secondFormatter
=
new
SimpleDateFormat("yyyyMMddHHmmss");
private static final
SimpleDateFormat
minuteFormatter
=
new
SimpleDateFormat("yyyyMMddHHmm");
private static final
SimpleDateFormat
hourFormatter
=
new
SimpleDateFormat("yyyyMMddHH");
private static final
SimpleDateFormat
dayFormatter
=
new
SimpleDateFormat("yyyyMMdd");
private static
SimpleDateFormat formatForMillis(
long
millis) {
if
(millis < 1000)
return
millisecondFormatter
;
if
(millis < 60*1000)
return
secondFormatter
;
if
(millis < 3600*1000)
return
minuteFormatter
;
if
(millis < 86400*1000)
return
hourFormatter
;
return
dayFormatter
;
}