In the previous blog, we introduced the winning sorting section in the basic query operations of streaminsight. This article describes how to connect to the last basic query of streaminsight.
Test data preparation
To facilitate test query, we first prepare some static test data sources. The following input1 and input2 are complex event streams that contain timestamps and numbers in the [1, 5] range:
// Create an event stream that contains consecutive numbers in the [1, 5] interval var now = datetime. now; var input1 = enumerable. range (1, 5 ). topointstream (application, E => pointevent. createinsert (now. addseconds (E), new {value = e}), advancetimesettings. strictlyincreasingstarttime); var input2 = enumerable. range (1, 5 ). topointstream (application, E => pointevent. createinsert (now. addseconds (e ). addmilliseconds (1), new {value = e}), advancetimesettings. strictlyincreasingstarttime );
Use the following statement to output input1 and input2 respectively:
// Note that input1 and input2 start time are different (from I in input1.topointenumerable () Where I. eventkind = eventkind. insert select new {starttime = I. starttime. tostring ("HH: mm: Ss. fff "), value = I. payload. value }). dump ("input stream 1"); (from I in input2.topointenumerable () Where I. eventkind = eventkind. insert select new {starttime = I. starttime. tostring ("HH: mm: Ss. fff "), value = I. payload. value }). dump ("input stream 2 ");
In linqpad, you can see the following results (because the data is related to the current time of the machine, the reader may run differently on the local machine ):
Join
The Join Operation of streaminsight is slightly different from that of the LINQ to object operation. Generally, two streaminsight event streams must meet the following conditions:
- Time overlap
- Meet join conditions
Question 1: how to connect two event streams?
Let's take a look at the results after input1 and input2 are joined:
// Connect input1 and input2 with the same value (although the two do not overlap in time) var joinquery1 = from I in input1 join J in input2 on I. value equals J. value select I;
Export joinquery1 results:
(from p in joinQuery1.ToPointEnumerable() where p.EventKind == EventKind.Insert select new { StartTime = p.StartTime.ToString("hh:mm:ss.fff"), Value = p.Payload.Value }) .Dump();
You will find that there is no result output. This is because this join operation does not meet the time overlapping condition: each event of input2 is 1 millisecond slower than input1. So how can we get the desired results?
Essentially, as long as we can overlap the input1 time with the input2 event, we should be able to correctly output the result. Therefore, we can use altereventduration to extend the life cycle of the input1 event, modify joinquery1 as follows to see the join result:
var joinQuery1 = from i in input1.AlterEventDuration(e => TimeSpan.FromMilliseconds(5)) join j in input2 on i.Value equals j.Value select i;
Question 2: How to connect multiple event streams?
Question 1 shows how to connect two streams. Let's see how to connect three event streams.
First create 3rd complex event streams:
var moreData = new[] { new { Key = 1, Description = "One Tag Event" }, new { Key = 2, Description = "Two Tag Event" }, new { Key = 3, Description = "Three Tag Event" }, new { Key = 4, Description = "Four Tag Event" }, new { Key = 5, Description = "Five Tag Event" }, };var moreStream = moreData.ToIntervalStream(Application, ev => IntervalEvent.CreateInsert(now, now.AddDays(1), ev), AdvanceTimeSettings.IncreasingStartTime);
Join the join result joinquery1 of input1 and input2 with morestream:
var joinQuery2 = from i1 in joinQuery1 join i2 in moreStream on i1.Value equals i2.Key select new { Value = i1.Value, Description = i2.Description };
Export the joinquery2 result as follows (because the data is related to the current time of the machine, the running result on the machine may be different ):
In the next article, we will begin to introduce the streaminsight query mode in article 1st-window alignment.