Contents
In the Edit Dimension dialog, from the Type dropdown list, select Time to specify a time-based dimension.
With a time-based dimension, a new window is managed and evaluated based on the time a tuple arrives. For example, a tuple containing the aggregate results may be emitted, and the current window closed, when a tuple arrives outside the period allowed for the currently open window. The triggering tuple is received by a new window.
Caution
Using a time-based dimension can cause unnecessary waits in testing or operation, makes impossible any deterministic tuple-by-tuple playback in unit tests and feed simulations, and complicates high availability design patterns.
In general, it is a best practice to avoid the use of the time-based dimension for
most applications. Instead, use a field-based dimension with a timestamp field, or a
predicate dimension with an expression such as lastval(
to create a 30-second window.
time_field
) -
firstval(time_field
) >
seconds(30)
The Edit Dimension dialog for a field-based dimension has the following appearance.
The following example clarifies the behavior of the time-based dimension option:
-
An aggregate is set with the dimension for window opening and closing to 60 seconds (Advance 60, Close and Emit 60), with no intermediate output.
-
Because there must always be an open window to receive tuples, this dimension's window opens when the first tuple arrives (call this time 0).
-
Subsequent tuples arrive at 3, 8, 17, 31, 42, 49, and 58 seconds.
-
At time 60 seconds, the window emits an aggregate tuple and closes with a
lastval
of 60. -
A tuple arrives at time 62 seconds. A new window with its
firstval
set to 60 opens to receive it.
The following describes the options available in the Edit Dimension dialog for time-based dimensions.
Category | Options and Meaning |
---|---|
Opening policy |
Select one of these options:
|
Window size |
Select one of these options:
|
Emission policy |
Select one of these options:
|
Optional windows |
Select one of these options:
|
To learn more about field-based aggregation works, run the Aggregate Operator Time Dimension Sample. It contains an Aggregate operator that uses 2-second windows to compute the average price per share of stocks, grouped by the Symbol field. The following table shows output from the sample. The output fields are:
-
Symbol—The stock symbol, used to sort tuples into groups
-
Average—Time-based Average price per share, calculated with the aggregate function avg()
-
start_time—The time the window opened (its
openval()
) -
end_time—The time the window closed (its
closeval()
) -
first_time—Time at which the first tuple arrived in the window (
firstval(time)
) -
last_time—Time at which the last tuple arrived in the window (
lastval(time)
) -
n—The number of tuples over which the average price per share was computed (
count()
)
Each table row is one window's emission when it closed. Timestamps have been truncated to only show hours, minutes, and seconds.
. | Symbol | Average | Start_time | End_time | First_time | Last_time | N |
---|---|---|---|---|---|---|---|
1 | MSFT | 26.033 | 08:03:06.000 | 08:03:08.000 | 08:03:06.494 | 08:03:07.583 | 3 |
2 | AAPL | 43.966 | 08:03:06.000 | 08:03:08.000 | 08:03:06.582 | 08:03:07.583 | 3 |
3 | AAPL | 43.950 | 08:03:08.000 | 08:03:10.000 | 08:03:08.083 | 08:03:09.583 | 4 |
4 | MSFT | 26.050 | 08:03:08.000 | 08:03:10.000 | 08:03:08.283 | 08:03:09.483 | 4 |
5 | AAPL | 43.950 | 08:03:10.000 | 08:03:12.000 | 08:03:10.083 | 08:03:11.683 | 4 |
6 | MSFT | 26.050 | 08:03:10.000 | 08:03:12.000 | 08:03:10.283 | 08:03:11.583 | 4 |
7 | AAPL | 44.200 | 08:03:12.000 | 08:03:14.000 | 08:03:12.183 | 08:03:13.487 | 2 |
8 | MSFT | 26.150 | 08:03:12.000 | 08:03:14.000 | 08:03:12.383 | 08:03:13.487 | 2 |
When you run the sample with input from the feed simulation resource AggregateByTime.sbfs
, you will find that averages, first and last
times, and number of tuples per window all vary, depending on the rate of input.