SQL11UPD04-TSCRIPT-08

This wiki is a transcript of a previously recorded video.

Related content assets:

  • Demo: Building Standing Queries Over .Net Collections with StreamInsight 1.2 (SQL11UPD04-DEMO-04)
  • Video: Building Standing Queries Over .Net Collections with StreamInsight 1.2 (SQL11UPD04-REC-08)

 

Building Standing Queries Over .Net Collections with StreamInsight 1.2

Hi, I’m Greg Low. I’m a SQL Server MVP and part of the Microsoft Regional Director Program. In this short demonstration, I want to show you Building Standing Queries over .NET Collections or Observable Objects Using StreamInsight 1.2.

 

So let’s start Visual Studio and I’ll load up a project that we built for SQL Server 2008 R2 to show how to use StreamInsight. And in that case, I had a highway monitor application that was dealing with toll points along a major highway. And so if I run this code just to show you what’s involved in that, as I start this we have vehicles coming past the toll points. You also get to see my absolute best graphical work here where I’ve tried to draw pictures of cars, taxis, trucks, buses, and so on. Anyway, so notice I’ve got various types of vehicles here, and so let’s stop. The beauty of this sort of application is that I can easily, with LINQ-based queries, change the sort of detail that I’m retrieving. So for example, here from e in input, select the values that I want to have shown across the output. Now I could just, for example, put a where clause in here. And to avoid you have to watching me have to type that – I won’t inflict that on you – I put a where clause in here. And I’ve said where the event vehicle type name is truck. I’ve simply added a where clause. If I run this, you’ll see that I’ve now filtered the data so that it’s just trucks that are being picked up by the code. This sort of thing is an inherent nature in StreamInsight – it’s very easy to write a code. And we confine ourselves most of the time to writing LINQ queries.

 

But one of the big issues is where the data comes from. Now in the earlier versions, I had to write input adaptors to get this to work as I wanted. But in many cases, there are .NET classes that already are generating the type of events that we need. And in these versions, we can now get access to that in a much, much simpler way. And so in this updated version, I’ve got a vehicle event generator class and the most important thing to first up notice is this has been derived from IObservable. Now this is all part of the reactive extensions to .NET, and if you need further information on the reactive extensions, Eric Meyer has generated a whole lot of materials and TechEd events, and so on, that you could sit in on and watch the webcasts on. But in this case, the thing of interest is I have a class, I want to flag it as being observable by other classes. And then other classes come and subscribe if they have an interest in the data.

 

Now here I’ve said this is an IObservable class based on TollPointEvent. TollPointEvent is the class that defines what’s in each event as it fires. Now, previously, that was just numbers and streams; it was relatively straightforward, but that payload can now have nested types inside it. And so we can now deal with much more detailed information, or trickier information in the type of payload we process. But in this case I still have numbers and streams.

 

 Now the other really important part of the code here is to have a subscribe method so that classes that have an interest in what is generated by this can subscribe. And notice all that it’s doing is adding them to a list of observers.

 

And then, the other method that’s of interest to us is OnNext. So this says when an event occurs, what do we do? And in this case we’re saying, for each observer, call that same method. And so what this does is it allows the HighwayMonitor main program to hook up to this class to extract a stream of events. The way we do that is scroll back up here, I can see inside the start code. But what I’ve got here is the server is StreamInsight, that’s the instance of it that’s running. I have an application called HighwayMonitor, and then I’ve created a new vehicle event generator class that I’ve called generator. And then, the important new addition here is that I can call to PointStream which takes the data coming out of that and turns it into a stream of events. Similarly, I can take an IEnumerable collection in .NET and turn that into a stream of events as well. The rest of the code then is just on and querying exactly as it was in earlier versions.

 

I hope you will find this a much easier way to just simply convert potentially existing .NET classes to provide streams of events or another simpler way, perhaps, of writing the code to avoid having to write detailed input adaptors in many cases.

Return to SQL Server 2012 Developer Training Kit BOM (en-US)