Hey, welcome back to part 2,
Designing and Creating Windowed Aggregates.
We are going to pick up where we left off
from the last lesson, and we are going to be talking about
the remaining 3 windows,
sliding, session, and snapshot.
So with that, let's dive in and start talking
about sliding windows.
Sliding windows output only when an event occurs,
and every window has to have at least one event.
A good way to think about this is
these pictures over here on the right,
when there's no people there, there's no event occurring,
we're not going to get a window.
We only get a window when there's an event occurring.
And unlike the other 2 windows,
sliding windows are going to continuously move forward
by an epsilon, which is just some defined period of time.
So basically instead of starting and stopping
with a distinct 10-second window,
it's going to continuously move forward in time.
Now I know what you're thinking, this is complicated.
This is definitely the most complicated of the windows,
but stick with me and we will get through this together.
So just hang on until the end and we'll get there.
So, because we're moving forward in time
with this epsilon and it's continuously moving,
we could have an infinite number of windows in theory.
And so to avoid this, Azure is only going to give us
events when something enters or exits the window.
Now, let me show you what I mean here.
So this is an example of a 10-second sliding window.
So let's start at this very first event,
and we start with 0 seconds, this green bubbles,
and we're going to go forward in time.
At basically 9 or 10 seconds,
we're going to get event #1 that occurs.
Once event number one occurs,
something entered the window, an event happened,
so we're going to take from that time
and we're going to look backwards in time, 10 seconds.
So we're basically going to take from 0 to 10 seconds
for the first window, event 1 occurred.
We're still continuing on in time,
and about 12 seconds event 5 occurs.
So we're going to go backwards 10 seconds in time, and
we're going to go from second 2 to basically second 12.
And within that window, events 1 and 5 occurred.
Time is still moving forward.
At 15 seconds, we have events 9 and 7 that occur.
So when that happens, we go from 15 seconds,
back 10 seconds in time, and we create
in a window with events, 1, 5, 9, and 7 in it.
So those are our first 3 windows.
We continue forward in time until we hit 20 seconds.
At that point, a new window occurs,
because event number one dropped off the map,
because it's been longer than 10 seconds
since that occurred.
So we have a new window that contains events
5, 9 and 7, event 1 dropped off.
And so that's going to be from 10 to 20 seconds.
Continue on in time, 22 seconds
and then event 5 drops off.
So we have a new window going from
12 seconds to 22 seconds.
We continue on in time, and once we reach 25 seconds,
we would have another window,
because events 9 and 7 dropped off,
so that's our trigger, however, there's no new events
from 15 to 25 seconds, so we don't get a window.
We had an action that occurred, but no window,
because there's no active events between 15 and 25 seconds.
Continue on in time, 26 seconds, we get event number 8.
And so we're going to go back and look 10 seconds back.
So basically from 16 to 26 seconds
we have event 8 and that's our new window.
Continuing on in time, we move forward another 10 seconds,
event 8 drops off, and we look,
but hey, there's been no new events,
so we don't get a new window at that point.
So even though there was an action
of 8 dropping off, there's no new window,
because there's no new events between 26 seconds
and whenever we look at this next, would be 36 seconds.
Hopefully that makes sense.
You may have to rewind just a little bit
and watch this a couple of times if you're still struggling,
but sliding windows are important
and this is how sliding windows work.
So jumping in and looking at the code for this,
luckily way simpler, group by, here's our topic,
sliding window, how many seconds
or how many minutes or whatever time period we want.
That's how we write code for sliding windows.
Continuing on, our next one is session windows.
Session windows produce an output only when an event occurs.
Every window has at least one event.
And like our last window, sliding windows,
it's going to continuously move forward
by that period epsilon.
So with this example, we are going to create a window
when event first occurs.
So right there at 0 seconds, we have event number one,
a window is created.
We move forward in time, 5 seconds.
If an event has occurred within 5 seconds,
we continue on, in this case,
event 6 happens within 5 seconds.
So right there at the 5 second mark, event 6 happens.
And so we continue on another 5 seconds.
5 seconds goes on and we don't have any new events,
so at 10 seconds our window closes with events one and 6.
We continue forward in time.
A new window is created as soon as the next event occurs.
Event 9 happens and so we create a window.
5 seconds moves forward, event 2 happens
within that window, so we can extend for 5 seconds.
5 seconds, event 3 occurs.
We extend 5 seconds, event 4 occurs.
We extend 5 seconds, event 8 occurs.
5 more seconds, and then nothing happens,
so we close the window with events
9, 2, 3, 4, and 8 in it.
Continuing on, our next window occurs with event 3.
5 seconds and so on and so forth.
So that's how a session window works.
So basically we're looking and then we're just extending
if we get an event that happens within that time period.
Code for this one, also very simple,
GROUP BY, topic, here's our session window,
and then our timeframe.
I have saved the easiest for last. You're welcome.
So when we think snapshot, think camera,
think moment in time.
So with a snapshot window,
we're going to get a group of events
that have the exact same timestamp.
So at 5 seconds we had events 1, 2, 3, and 4.
10 seconds, we had events 5 and 6.
At 12 seconds, event 7.
And so those are all going to be unique windows.
That's our snapshot window.
And so with our snapshot window,
code, also simple, GROUP BY, our topic, or X,
and then it's just going to be system.timestamp
and then some parentheses.
That's how we're going to do the syntax for snapshot.
All right, we have covered a ton of stuff here.
In review, there are 5 window types:
hopping, sliding, snapshot, tumbling, and session.
You need to know when to use each type.
The details are important here,
I would suggest memorizing them,
and you're going to need to understand the differences.
So a good way to think about this
is if I gave you an example of,
I need to be able to look at
5 minutes in time for a window,
and it's very important that I don't have
events in more than one window,
and I can't have the windows overlap.
What would be the best window type for me in this scenario?
The answer would be hopping.
So you need to know that level of detail
for these window types.
So make sure that you understand what they are,
how they work, and you should have
a general understanding of the syntax,
although, that's not terribly complicated.
If you understand that, you should be good to go.
Again, this lesson may be one of those
that you need to watch through more than one time
to get a good feel of how windows work.
If so, pop back in, watch it again.
It's going to take a little repetition,
because some of these concepts are rather complicated.
So I hope this has been helpful
and I will see you in our next lesson
once you are good to go.