All right, welcome back.
So in this lesson, we are going to dive deep
into windows in Azure Stream Analytics.
I'm going to spend some time talking to you
about what a window is and why you need it.
We're going to dive into the 5 types
of windows and how they work.
And then finally, to give you a conceptual look
at windows and the code that's used to employ them.
So with that, let's start talking about
what a window is and why we need it.
So as we talk about windows, keep in mind that windows
allow functions to be run on sets of streaming data.
That's a more technical way of explaining what I've been
talking about in the last couple of lessons.
Essentially, it allows us to get a time,
start at an end time,
and based upon that, we can do a function,
like a summation, or an average, or whatever we want.
All of our outputs always result at the end of our window.
So a good example of a window and why we need it
would be a call center.
So for instance, if we had a call center
and we have a bunch of different people calling in,
we could take all of that data and look at it
over the period of an hour that's streaming through
and we could run streaming processes on that data.
Or maybe it's every 15 minutes,
and we're taking a low look at average call length,
for example. That would be a good scenario for a window.
And with that, we can create some highly complex solutions.
So you can see here, down at the bottom in green,
we have our event sources.
So we talked about inputs
and we have multiple inputs coming in
from IoT Hubs and Event Hubs.
And then Azure Stream Analytics,
or ASA, there at the top in purple
is going to be taking that data,
as well as some data from a SQL database
and it's going to create a window
and run queries on all of that data.
And then process that through in
what we would call a hot path to Cosmos DB.
So, basically, from this
we can create some very complex solutions.
So now that we've talked a little bit
about what windows are, let's talk about
the types of windows that we can see.
There's 5 types of windows
and, yes, you need to know all of them.
And we are specifically focusing
on Azure Stream Analytics in this lesson.
There are other services in Azure that use windows
but keep in mind that right now
we're talking about Stream Analytics.
So the 5 types of windows are tumbling, hopping,
sliding, session, and snapshot.
Now with this, Microsoft says that a window
contains event data along a timeline
and enables you to perform various operations
against the events within that window.
What I'm trying to do with this
I'm trying to show you a couple of different ways to read
and hear what a window is so that you can see we're talking
about the same thing.
Whether we're talking about functions,
whether we're talking about just doing an average of time,
whether we're talking about a timeline and the enablement
of various operations, we're all talking about windows here.
So with that, let's dive in and talk about tumbling windows.
Tumbling windows are repeating windows
that are non-overlapping and events cannot belong
to more than one tumbling window.
Now I know in a lot of places in this course,
I say you don't need to memorize this
you just need to understand it.
I would suggest that with this section
you actually memorize that.
You should know that a tumbling window is repeating.
You should know it's not overlapping, and you should know
that events don't belong to more than one tumbling window.
That's going to be important.
So here is an example of a tumbling window.
So we have our green circles here
and the green circles are just seconds.
So basically we're looking at 1 minute of time,
starting from the left, which is 0, all the way up
to our 60 seconds on the right.
Now these little orange boxes, this 3, 5, 8, 6, 2,
these are just events.
We could have given them a number, a name,
it doesn't really matter.
This is just an event that's coming
into our tumbling window.
And down here at the bottom you can see
that I have dash lines for this first tumbling window
goes from 0 to 20 seconds.
And so you can see it starts at the 0 and ends at the 20,
so this is a tumbling window.
And within that window, we have the events 35862
that occur at various times within that 20 seconds.
And so those events at the top are carried down
to within that tumbling window or this blue background here.
So as we finish our first tumbling window
it's not overlapping, which means that window ends
and it's repeating, which means it tumbles
into our second space here,
which runs from 20 to 40 seconds.
And then from there, it goes from 40 to 60 seconds.
And every event appears in one of those windows.
So this is a tumbling window.
And looking at the code for this,
we're going to have our SELECT statement
that we saw last lesson
and then we're going to have our FROM statement.
So where is it coming from?
This is our input stream.
And then we're going to have this GROUP BY statement.
And in this case, we're going to group by our time zone
and we're going to create a tumbling window
with a duration of 20 seconds.
And we're going to offset that by one millisecond
or negative one millisecond.
So you can create an offset
and just kind of play with those windows moving forward
or backward in time a little bit,
depending upon what you're trying to do.
But the key is tumbling window, duration 20 seconds.
All right, so that is a tumbling window.
Up next, we're going to talk about a hopping window.
Now, a hopping window can hop forward in time
by a fixed period.
And hopping windows also can overlap, okay.
So what we have here is we have an example
of a 10-second hopping window with a 5-second hop, okay.
So our very first window here starts at 0 seconds
and it goes to 10 seconds.
Again, those green circles are seconds.
So from 0 to 10 seconds is our first window.
So like our last window, you can see here
that events 75943
appear in that window.
Now, unlike tumbling, windows can overlap.
And so we're going to hop forward 5 seconds.
So our next window actually begins at 5 seconds
and it's going to go from 5 seconds to 15 seconds
which is our 10 second hopping window.
So window #2 here contains events 5943
and then it captures the very end event number 2 over here.
So we can have more than one event
appear in more than one window.
So that is our second hopping window.
And then our third window kicks off 5 seconds
from when the second one started.
So it's going to start at 10 seconds
and run all the way to 20 seconds.
Now in this window the only events
that we're going to capture is event 2 and event 6.
So you can see here, this is a hopping window.
And so then when we look at the code here
you can see that we have our GROUP BY statement again,
and I didn't include the rest of it for the SELECT
and the FROM because we're just talking
about the code for the window itself.
So we have our group by statement.
X is just topic.
It could be whatever you want.
It could be a timestamp, a topic, a whatever,
how we're going to group our data.
Then we're going to group it using a hopping window.
And this has a duration of 20 minutes.
So each window will be 20 minutes in length
and it's going to hop 5 minutes.
So if the first window starts at minute 0
the second window will start at minute 5.
And then again, we can also define an offset
if we so choose.
So as we wrap up our discussion on hopping windows,
I'm actually going to take us back
to the 5 types of windows,
and I am going to end this first session,
this first lesson on windows.
The next couple of windows are going to be a very different
kind of window than the tumbling and hopping window
that we've just looked at.
And so this makes kind of a natural break point.
So take a breath, take a pause, grab a coffee
and I'll see you in the second half, the next lesson,
where we about sliding session and snapshot windows.
See you then.