Five-step quick start with linqpad streaminsight

Source: Internet
Author: User
Tags time zones microsoft download center

Link: http://seroter.wordpress.com/2010/12/23/5-quick-steps-for-trying-out-streaminsight-with-linqpad/

Sometimes I want to quickly experiment with a technical idea, instead of building a complete solution. Streaminsight, as a new product, has such problems. Ideally, we should be able to test a product, run it in a specific scenario, and make a quick assessment.Now with linqpad, streaminsight's quick test becomes possible.. This blog step by step describes how to install components and test streaminsight queries using a variety of data sources. If you can continue to read the data, as a reward, I will tell you how to use odata and execute the LINQ query on it.

Step 1: Install streaminsight 1.1

You need to install the second release of streaminsight to use the linqpad integration function. You can download it from the Microsoft download center to streaminsight 1.1. When installing streaminsight, you can select an evaluation version or use the SQL Server 2008 R2 license key (which can be found in SQL Server installation media x86 \ defaultsetup. INI) for activation.

Step 2: Install linqpad 4.0

You can select the free version of linqpad (download from here) or the paid version with built-in smart sensing function.

Step 3: add the linqpad driver of streaminsight

When you run linqpad, you can see an option called "add connection.

Later, you will see some built-in drivers, including LINQ-to-SQL and odata.

Select"View more drivers..."Button, you will see the latest streaminsight driver created by Microsoft.

The installation time of this driver is about 200 ms, and you can see it in the linqpad driver list.

Step 4: Create a new connection with the streaminsight driver

If the window is still open, select the driver. Otherwise, return to linqpad and select"Add connectionNext,Choose data context"Click on the Wizard PageNextButton. Now you will see a pop-up window titled "streaminsight context chooser" in which you can select a dataset provided by Microsoft or a new context. Here I selectDefault context.

Step 5: compile a simple query

Now we connect to the context of streaminsight. before writing a query, make sure that"Language"Value:"C # statementsAndDatabase"Value:"Streaminsight: default context".

Because there is no input data source in the context by default, we create a simple vertex event set and convert it to a stream for processing. Our first query obtains all events with a count greater than 4.

 

//define event collectionvar source = new[]{   PointEvent.CreateInsert(new DateTime(2010, 12, 1), new { ID = "ABC", Type="Customer", Count=4 }),   PointEvent.CreateInsert(new DateTime(2010, 12, 2), new { ID = "DEF", Type="Customer", Count=9 }),   PointEvent.CreateInsert(new DateTime(2010, 12, 3), new { ID = "GHI", Type="Partner", Count=5 })};//convert to streamvar input = source.ToStream(Application,AdvanceTimeSettings.IncreasingStartTime);var largeCount = from i in input       where i.Count > 4       select i;//emit results to LINQPadlargeCount.Dump();

 

The output result is as follows. Note that only two records are output.

(Note: starttime is GMT, and the results will be different in different time zones)

To show more streaminsight capabilities, I created another query that stores three events on the same day so that all vertices are in the same snapshot) create a snapshot window and calculate each typeCountThe sum of values.

var source = new[]{   PointEvent.CreateInsert(new DateTime(2010, 12, 1), new { ID = "ABC", Type="Customer", Count=4 }),   PointEvent.CreateInsert(new DateTime(2010, 12, 1), new { ID = "DEF", Type="Customer", Count=9 }),   PointEvent.CreateInsert(new DateTime(2010, 12, 1), new { ID = "GHI", Type="Partner", Count=5 })};var input = source.ToStream(Application, AdvanceTimeSettings.IncreasingStartTime);var custSum = from i in input              group i by i.Type into TypeGroups              from window in TypeGroups.SnapshotWindow(SnapshotWindowOutputPolicy.Clip)              select new { Type = TypeGroups.Key, TypeSum = window.Sum(e => e.Count) };custSum.Dump();

This query also obtains two messages, but note that the new typesum value is the aggregation of all events with the same type value.

Through the above five steps (about 8 minutes), we installed the local component and successfully executed a pair of streaminsight queries.

Now I can stop writing, but let's try something more interesting. Imagine how to execute a query in an existing odata data source? The other three steps below will further demonstrate linqpad and streaminsight.

* Step 6: Compile the odata connection for the northwind entry

Click"Add connection"Button, select"WCF data services (odata)"Driver, next select odata as the provider, and in"Uri"Text box Fill in odata Source

(Http://services.odata.org/Northwind/Northwind.svc), clickOK. In linqpad, you will see all the entities contained in the northwind odata source.

Now let's execute a very simple query. This query traverses all employee records and outputs the ID, employment date, and country of each employee.

 

var emps = from e in Employees           orderby e.HireDate ascending           select new           {               HireDate = (DateTime)e.HireDate,               EmpId = e.EmployeeID,               Country = e.Country           };emps.Dump();

The output result is as follows:

 

* Step 7: add the streaminsight query capability on the northwind data

What should I do if I want to view the employment status of an employee by country in a specific window? We can use a direct LINQ query, but what is the fun? Let's get down to the truth and think about what interesting things can be done using real-time analysis on employee data. Of course, this topic is not the focus of this blog post.

Linqpad only allows one data context at a time. To use both odata source and streaminsight for query at a time, we need to make some modifications. Mark Simms has written an in-depth article to explain this problem. Here I will provide a brief version.

Right-click the linqpad query tab that contains the odata query and select"Query PropertiesTo add a reference to streaminsight DLL. Click"Additional reference"Tab"Add"Button to find and selectMicrosoft. complexeventprocessing. dllAndMicrosoft. complexeventprocessing. observable. dll(If you cannot see these two DLL files, selectShow GAC assembliesOption ).

Switch to"Additional namespace imports"Tab. Enter the namespace required for query.

Now we are going to use streaminsight LINQ for query on the odata source.

* Step 8: add the streaminsight query capability to the northwind data

I copied the previously written query to perform subsequent work on this basis.

Below the query, I instantiate a streaminsight "server" to host the query and define a streaminsight application to include the query. Next, I converted the odata results into a CEP stream and created a streaminsight query using the tumbling window to output the number of employees in each country in each of the 60 skylights, finally, output the result to linqpad.

 

var emps = from e in Employees           orderby e.HireDate ascending           select new           {               HireDate = (DateTime)e.HireDate,               EmpId = e.EmployeeID,               Country = e.Country           };//define StreamInsight serverusing (Server siServer = Server.Create("RSEROTERv2")){    //create StreamInsight app    Application empApp = siServer.CreateApplication("demo");    //map odata query to the StreamInsight input stream    var empStream = emps.ToPointStream(empApp, s => PointEvent.CreateInsert(s.HireDate, s), AdvanceTimeSettings.IncreasingStartTime);    var counts = from f in empStream                 group f by f.Country into CountryGroup                 from win in CountryGroup.TumblingWindow(TimeSpan.FromDays(60), HoppingWindowOutputPolicy.ClipToWindowEnd)                 select new { EmpCountry = CountryGroup.Key, Count = win.Count() };    //turn results into enumerable    var sink = from g in counts.ToPointEnumerable()               where g.EventKind == EventKind.Insert               select new { WinStart = g.StartTime, Country = g.Payload.EmpCountry, Count = g.Payload.Count };    sink.Dump();}

The query result is as follows:

 

Summary

Now you know something! The preceding five steps can be completed in about 10 minutes, and the remaining asterisks can be completed in another five minutes.

This is a fast and low-investment method to find a strong product.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.