static void Main(string[] args) { // The StreamInsight engine is a server that can be embedded (in-memory) or remote (e.g. the Azure Service). // We first use Server.Create to create a server instance and return a handle to that instance. using (Server server = Server.Create("Default")) { Application application = server.CreateApplication("app"); // We will be building a query that takes a stream of SensorReading events. // It will work the same way on real-time data or past recorded events. IQStreamable <SensorReading> inputStream = null; Console.WriteLine("Press L for Live or H for Historic Data"); ConsoleKeyInfo key = Console.ReadKey(); Console.WriteLine(); if (key.Key == ConsoleKey.L) { inputStream = CreateStream(application, true); } else if (key.Key == ConsoleKey.H) { inputStream = CreateStream(application, false); } else { Console.WriteLine("invalid key"); return; } // The query is detecting when a threshold is crossed upwards. // See the Visio drawing TresholdCrossedUpward.vsd for the intuition. int threshold = 42; // Alter all events 1 sec in the future. var alteredForward = inputStream.AlterEventStartTime(s => s.StartTime.AddSeconds(1)); // Compare each event that occurs at input with the previous event. // Note that, this one works for strictly ordered, strictly (e.g 1 sec) regular streams. var crossedThreshold = from evt in inputStream from prev in alteredForward where prev.Value <threshold && evt.Value> threshold select new { Time = evt.Time, Low = prev.Value, High = evt.Value }; foreach (var outputSample in crossedThreshold.ToEnumerable()) { Console.WriteLine(outputSample); } Console.WriteLine("Done. Press ENTER to terminate"); Console.ReadLine(); } }
static void Main() { using (Server server = Server.Create("Default")) { Application application = server.CreateApplication("app"); IQStreamable <YahooQuote> inputStream = null; Console.WriteLine("Press L for Live or H for Historic Data"); ConsoleKeyInfo key = Console.ReadKey(); Console.WriteLine(); if (key.Key == ConsoleKey.L) { inputStream = CreateStream(application, true); } else if (key.Key == ConsoleKey.H) { inputStream = CreateStream(application, false); } else { Console.WriteLine("Invalid Key"); return; } decimal threshold = new decimal(14.00); var alteredForward = inputStream.AlterEventStartTime(s => s.StartTime.AddSeconds(1)); var crossedThreshold = from evt in inputStream from prev in alteredForward where prev.LastTradePrice <threshold && evt.LastTradePrice> threshold select new { LastUpdate = evt.LastUpdate, Low = prev.LastTradePrice, High = evt.LastTradePrice }; foreach (var outputSample in crossedThreshold.ToEnumerable()) { Console.WriteLine(outputSample); } Console.WriteLine("Done. Press ENTER to terminate"); Console.ReadLine(); } }
static void Main(string[] args) { Server server = Server.Create("MyInstance"); Application application = server.CreateApplication("app"); Console.WriteLine("server+application ready"); DateTime startTime = new DateTime(2019, 1, 1); IQStreamable <SensorReading> inputStream = application.DefineObservable(() => SimulateLiveData()).ToPointStreamable( r => PointEvent <SensorReading> .CreateInsert(startTime.AddSeconds(r.Time), r), AdvanceTimeSettings.StrictlyIncreasingStartTime); int threshold = 42; // Alter all events 1 sec in the future. var alteredForward = inputStream.AlterEventStartTime(s => s.StartTime.AddSeconds(1)); Console.WriteLine("stream ready"); // Compare each event that occurs at input with the previous event. // Note that, this one works for strictly ordered, strictly (e.g 1 sec) regular streams. var crossedThreshold = from evt in inputStream from prev in alteredForward where prev.Value <threshold && evt.Value> threshold select new { Time = evt.Time, Low = prev.Value, High = evt.Value }; Console.WriteLine("query ready"); var query = crossedThreshold.ToEnumerable(); // lazy初始化 foreach (var outputSample in query) // 迭代访问query的过程会触发inputStream的产生 { Console.WriteLine(outputSample); // 打印outputSample, 也就是select new的结果 } Console.WriteLine("Done. Press ENTER to terminate"); application.Delete(); server.Dispose(); }