public void DataQuery(IQStreamable <Crime> source, out IQStreamable <double> queryResult) { var alignment = new DateTime(TimeSpan.FromHours(0).Ticks, DateTimeKind.Utc); queryResult = from e in source.TumblingWindow(TimeSpan.FromHours(24), alignment) select e.Avg(w => w.Severity); }
public static IRemoteStreamableBinding ToBinding <TPayload>(this IQStreamable <TPayload> stream, Application cepApplication, Type consumerFactoryType, object configInfo, EventShape eventShape) { var factory = Activator.CreateInstance(consumerFactoryType) as ISinkFactory; if (factory == null) { throw new ArgumentException("Factory cannot be created or does not implement ISinkFactory"); } switch (eventShape) { case EventShape.Point: var pointObserver = cepApplication.DefineObserver(() => factory.CreatePointObserverSink <TPayload>(configInfo)); return(stream.Bind(pointObserver)); case EventShape.Interval: var intervalObserver = cepApplication.DefineObserver(() => factory.CreateIntervalObserverSink <TPayload>(configInfo)); return(stream.Bind(intervalObserver)); case EventShape.Edge: var edgeObserver = cepApplication.DefineObserver(() => factory.CreateEdgeObserverSink <TPayload>(configInfo)); return(stream.Bind(edgeObserver)); default: throw new ArgumentOutOfRangeException("eventShape"); } }
static void Main(string[] args) { while (inputQuote.FirstOrDefault().LastTradePrice == null) { Fetch(inputQuote); } using (Server server = Server.Create("default")) { Application application = server.CreateApplication("app"); IQStreamable <YahooQuote> inputStream = null; inputStream = application.DefineObservable(() => ToObservableInterval(inputQuote, TimeSpan.FromMilliseconds(1000), Scheduler.ThreadPool)).ToPointStreamable( r => PointEvent <YahooQuote> .CreateInsert(DateTimeOffset.Now, r), AdvanceTimeSettings.StrictlyIncreasingStartTime); var myQuery = from evt in inputStream select evt; foreach (var outputSample in myQuery.ToEnumerable()) { Console.WriteLine(outputSample); } Console.WriteLine("Done. Press ENTER to terminate."); Console.ReadLine(); } }
/// <summary> /// Groups the elements of a stream according to a specified key selector function. /// </summary> /// <typeparam name="TSource">The type of the elements in the source stream.</typeparam> /// <typeparam name="TKey">The type of the grouping key computed for each element in the source stream returned by <paramref name="keySelector"/>.</typeparam> /// <typeparam name="TElement">The type of the grouped elements contained in each window returned by <paramref name="elementSelector"/>.</typeparam> /// <typeparam name="TResult">The type of the value returned by the <paramref name="resultSelector"/>.</typeparam> /// <param name="source">A stream whose elements are to be grouped.</param> /// <param name="keySelector">A function to extract the key for each element.</param> /// <param name="elementSelector">A function to map each source element to an element in the group.</param> /// <param name="resultSelector">A function to create a result value from each group.</param> /// <returns>A stream of windowed groups, each of which corresponds to a unique key value, containing all projected elements that share that same key value.</returns> public static IQStreamable <IGroupedWindow <TKey, TElement> > GroupBy <TSource, TKey, TElement, TResult>( this IQStreamable <TSource> source, Expression <Func <TSource, TKey> > keySelector, Expression <Func <TSource, TElement> > elementSelector, Expression <Func <TKey, IWindow <TElement>, TResult> > resultSelector) // => source.GroupBy(keySelector, elementSelector).Select(g => resultSelector(g.Key, g.Window)); => throw new NotImplementedException();
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(); } }
private void TestMethod2() { IQStreamable <Tuple <string, int, IEnumerable <object> > > test1 = null; var test3 = from t in test1 from o in t.Item3 // join t0 in test0 on t.Item1 equals t0.Item1 where t.Item2 < 10 group o by t.Item1 into g select g.Window.Count(); }
static void DisplayIntervalResults <TPayload>(this Application app, IQStreamable <TPayload> resultStream) { // Define observer that formats arriving events as intervals to the console window. var consoleObserver = app.DefineObserver(() => Observer.Create <IntervalEvent <TPayload> >(ConsoleWriteInterval)); // Bind resultStream stream to consoleObserver. var binding = resultStream.Bind(consoleObserver); // Run example query by creating a process from the binding we've built above. using (binding.Run("ExampleProcess")) { Console.WriteLine("***Hit Return to exit after viewing query output***"); Console.WriteLine(); 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(); }
private void TestMethod() { var qc = new QueryContext(); IObservable <Tuple <string, int, long, long> > obs0 = null; IObservable <Tuple <string, int> > obs1 = null; IObservable <Tuple <string, int, long> > obs2 = null; IQStreamable <Tuple <string, int, long, long> > test0 = qc.RegisterStream(obs0, o => o.Item3, o => o.Item4); IQStreamable <Tuple <string, int> > test1 = qc.RegisterStream(obs1, o => 0, o => 0); IQStreamable <Tuple <string, int, long> > test2 = qc.RegisterStream(obs2, o => o.Item3, o => o.Item3 + 1); var test3 = from t in test1 join t1 in test0 on t.Item1 equals t1.Item1 join t2 in test2 on t.Item1 equals t2.Item1 // join t0 in test0 on t.Item1 equals t0.Item1 where t.Item2 < 10 let f = t2.Item1 group t by t.Item1 into g select g.Window.Count(); }
public void begin(Application myApp, int capacity, string processName) { string conn_string = this.CONNECTION_STRING; string sql_query = this.SQL_QUERY; System.Linq.IQueryable <SourceEvent> qSource = myApp.DefineEnumerable <SourceEvent>(() => GetEvents(conn_string, sql_query)); IQStreamable <SourceEvent> tSource = qSource.ToPointStreamable <SourceEvent, SourceEvent>( e => CreatePoint(e), AdvanceTimeSettings.IncreasingStartTime); //tSource.Deploy("serverSource"); requestHolder = new RequestManager.RequestHolder(this.TABLENAME, capacity); requestHolder.rowBody = new RequestManager.RowBody(); requestHolder.rowBody.Row = new List <RequestManager.RowElement>(); // DEFINE a simple observer SINK (writes the value to the server console) //var mySink = myApp.DefineObserver(() => Observer.Create<SourceDatabaseEvent>(x => Console.WriteLine("sinkserver: " + x.Product + " SaleDate:" + x.SaleDate + " TransTime:" + x.TransactionTime))); string hbase_row_key = this.HBASE_ROW_KEY; var mySink = myApp.DefineObserver(() => Observer.Create <SourceEvent>(x => Sink <SourceEvent>(x, hbase_row_key))); // DEPLOY the sink to the server for clients to use //mySink.Deploy("serverSink"); // Compose a QUERY over the source (return every even-numbered event) var myQuery = from e in tSource select e; // BIND the query to the sink and RUN it using (IDisposable proc = myQuery.Bind <SourceEvent>(mySink).Run(processName)) { // Wait for the user stops the server Console.WriteLine("----------------------------------------------------------------"); Console.WriteLine("MyStreamInsightServer is running, press Enter to stop the server"); Console.WriteLine("----------------------------------------------------------------"); Console.WriteLine(" "); Console.ReadLine(); } }
/// <summary> /// Projects each element of a stream into a new form. /// </summary> /// <typeparam name="TSource">The type of the elements in the source stream.</typeparam> /// <typeparam name="TResult">The type of the elements in the result stream, obtained by running the selector function for each element in the source sequence.</typeparam> /// <param name="source">A stream of elements to invoke a transform function on.</param> /// <param name="selector">A transform function to apply to each source element.</param> /// <returns>A stream whose elements are the result of invoking the transform function on each element of the source.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="source" /> or <paramref name="selector" /> is null.</exception> public static IQStreamable <TResult> Select <TSource, TResult>(this IQStreamable <TSource> source, Expression <Func <TSource, TResult> > selector) => source.Provider.CreateQuery <TResult>(
/// <summary> /// /// </summary> /// <typeparam name="TLeft"></typeparam> /// <typeparam name="TRight"></typeparam> /// <typeparam name="TResult"></typeparam> /// <param name="left"></param> /// <param name="right"></param> /// <param name="resultSelector"></param> /// <returns></returns> public static IQStreamable <TResult> SelectMany <TLeft, TRight, TResult>( this IQStreamable <TLeft> left, Func <Empty, IQStreamable <TRight> > right, Expression <Func <TLeft, TRight, TResult> > resultSelector) => Join(left, right(default), o => true, o => true, resultSelector);
/// <summary> /// Passes a truncated version of each event on the left, where the left event is truncated by the first event on the right /// whose Vs occurs after the event on the left, whose join condition is met, and where the keys for both streams match. A /// fast join key comparer is passed in for efficiency. /// </summary> /// <typeparam name="TLeft">The type of the elements in the left source stream.</typeparam> /// <typeparam name="TRight">The type of the elements in the right source stream.</typeparam> /// <typeparam name="TKey">The type of the elements forming the join key.</typeparam> /// <param name="left">The left stream to join elements for.</param> /// <param name="right">The right stream to join elements for.</param> /// <param name="leftSelector">A function to select the join key of each element of the left stream.</param> /// <param name="rightSelector">A function to select the join key of each element of the right stream.</param> /// <returns>A stream that contains result elements from the left whose timelines are truncated by elements from the right.</returns> /// <exception cref="ArgumentNullException"><paramref name="left" /> or <paramref name="right" /> or <paramref name="leftSelector" /> or <paramref name="rightSelector" /> is null.</exception> public static IQStreamable <TLeft> ClipDuration <TLeft, TRight, TKey>( this IQStreamable <TLeft> left, IQStreamable <TRight> right, Expression <Func <TLeft, TKey> > leftSelector, Expression <Func <TRight, TKey> > rightSelector) => left.Provider.CreateQuery <TLeft>(
/// <summary> /// Divides long-lasting events into smaller, repeated events of the given period with the given offset. /// </summary> /// <typeparam name="TSource">Type of payload in the stream</typeparam> /// <param name="source">Input stream</param> /// <param name="period">The new duration for each event.</param> /// <param name="offset">The offset at which to apply the temporal division.</param> /// <returns>Result (output) stream</returns> public static IQStreamable <TSource> Chop <TSource>(this IQStreamable <TSource> source, long period, long offset = 0) => source.Provider.CreateQuery <TSource>(