The previous blog introduces the grouping and sorting section in the basic query operations of streaminsight. This articleArticleThis section describes how to sort the clientworker in a streaminsight query ).
Test data preparation
To facilitate test query, we first prepare a static test data source:
VaR Sourcedata = New [] { New {Name = "" , Count = 2, tiebreaker = 1 }, New {Name = "" , Count = 3, tiebreaker = 2 }, New {Name = "" , Count = 1, tiebreaker = 3 }, New {Name ="" , Count = 2, tiebreaker = 4 }, New {Name = "B" , Count = 3, tiebreaker = 1 }, New {Name = "B" , Count = 3, tiebreaker = 2 }, New {Name = "B" , Count = 1, tiebreaker = 3 }, New {Name = "C" , Count = 2, tiebreaker = 1 }, New {Name = "D" , Count = 1, tiebreaker = 1 }};
Next, we will change sourcedata to a complex event stream of point type:
VaRInputstream = sourcedata. topointstream (application, t =>Pointevent. Createinsert (NewDatetime(2011, 8, 24, 23, 0, 0,Datetimekind. UTC), T ),Advancetimesettings. Increasingstarttime );
Winning sorting
Question 1: how to find the two events with the highest count value in the event with the same name? If multiple events with the same Count value exist in the result, select smaller ones based on tiebreaker.
Similar to grouping sorting described in the previous section, we need to change the orderby condition based on grouping sorting to solve this problem:
var top2withouttiequery = from E in inputstream group E by E. name into G from E in ( from win in G. snapshotwindow ( snapshotwindowoutputpolicy . clip) from E in win orderby E. count descending , E. tiebreaker select E ). take (2) select E;
Export the top2withouttiequery content as follows:
Question 2: How to find the two events with the same name (count) as the maximum value of all events, if the number of results events is less than 2, the output is only one.
Problem 2 is a little more complicated. First, we need to find all the events with the maximum number. However, in these events, we need to find the two event groups with the minimum tiebreaker.
VaR Top2withtie = From E In Inputstream Group E By E. Name Into G From E In ( From WinIn ( From Win In G. snapshotwindow ( Snapshotwindowoutputpolicy . Clip) From E In Win Orderby E. Count Descending select E). Take (1). snapshotwindow ( Snapshotwindowoutputpolicy . Clip) From E In Win Orderby E. tiebreaker Select E). Take (2) Select E;
AboveCodeMedium,
FromWinInG. snapshotwindow (Snapshotwindowoutputpolicy. Clip)FromEInWinOrderbyE. CountDescendingselectE). Take (1)
All the events in each event group whose count is equal to the maximum number. Although it is take (1), the result shows that more than one event in the same event group is taken out, for example, two events (name = B, Count = 3, tiebreaker = 1 and name = B, Count = 3, tiebreaker = 2) are extracted from the event group with name 'B ).
FromEInWinOrderbyE. tiebreakerSelectE). Take (2)
The minimum number of tiebreaker events is retrieved from the events with the maximum number of identical events.
The final result is as follows:
The next article describes the last section in the basic query operations of streaminsight-join section.