Exemplo n.º 1
0
        private static void BindAndRunQuery <TPayload>(string queryName, CepStream <TPayload> queryStream, EventShape outputEventShape, List <string> inputFields, List <string> outputFields)
        {
            var inputConfig = new CsvInputConfig
            {
                InputFileName        = @"..\..\..\TollInput.txt",
                Delimiter            = new char[] { ',' },
                BufferSize           = 4096,
                CtiFrequency         = 1,
                CultureName          = "en-US",
                Fields               = inputFields,
                NonPayloadFieldCount = 2,
                StartTimePos         = 1,
                EndTimePos           = 2
            };

            // The adapter recognizes empty filename as a write to console.
            var outputConfig = new CsvOutputConfig
            {
                OutputFileName = string.Empty,
                Delimiter      = new string[] { "\t" },
                CultureName    = "en-US",
                Fields         = outputFields
            };

            // Note - Please change the instance name to the one you have chosen during installation
            using (var server = Server.Create("Default"))
            {
                Application application = server.CreateApplication("TollStationApp");

                // set up input and output adapters
                InputAdapter  inputAdapter  = application.CreateInputAdapter <CsvInputFactory>("input", "Csv Input Source");
                OutputAdapter outputAdapter = application.CreateOutputAdapter <CsvOutputFactory>("output", "Csv Output");

                // set up the query template
                QueryTemplate queryTemplate = application.CreateQueryTemplate("QueryTemplate", string.Empty, queryStream);

                // set up advance time settings to enqueue CTIs
                var advanceTimeGenerationSettings = new AdvanceTimeGenerationSettings(inputConfig.CtiFrequency, TimeSpan.Zero, true);
                var advanceTimeSettings           = new AdvanceTimeSettings(advanceTimeGenerationSettings, null, AdvanceTimePolicy.Adjust);

                // Bind query template to input and output streams
                QueryBinder queryBinder = new QueryBinder(queryTemplate);
                queryBinder.BindProducer <TollReading>("TollStream", inputAdapter, inputConfig, EventShape.Interval, advanceTimeSettings);
                queryBinder.AddConsumer("outputStream", outputAdapter, outputConfig, outputEventShape, StreamEventOrder.FullyOrdered);

                // Create a runnable query by binding the query template to the input stream of interval events,
                // and to an output stream of fully ordered point events (through an output adapter instantiated
                // from the output factory class)
                Query query = application.CreateQuery(queryName, "Hello Toll Query", queryBinder);

                RunQuery(query);
            }
        }
        internal static void Main()
        {
            using (var server = Server.Create("Default"))
            {
                // If you want to publish this server, or connect to an already existing server,
                // please see the product documentation "Publishing and Connecting to a StreamInsight Server"
                // to find out how to replace, or add to the above server creation code. You must publish
                // the server for the Event Flow debugger to be able to connect to the server.
                var application = server.CreateApplication("TutorialApp");

                // Define device configuration
                var inputConfig = new CsvInputConfig
                {
                    InputFileName = @"..\..\..\TollInput.txt",
                    Delimiter     = new char[] { ',' },
                    BufferSize    = 4096,
                    CtiFrequency  = 1,
                    CultureName   = "en-US",
                    Fields        = new List <string>()
                    {
                        "TollId", "LicensePlate", "State", "Make", "Model", "VehicleType", "VehicleWeight", "Toll", "Tag"
                    },
                    NonPayloadFieldCount = 2,
                    StartTimePos         = 1,
                    EndTimePos           = 2
                };

                var outputConfigForPassThroughQuery = new CsvOutputConfig
                {
                    // The adapter recognizes empty filename as a write to console
                    OutputFileName = string.Empty,
                    Delimiter      = new string[] { "\t" },
                    CultureName    = "en-US",
                    Fields         = new List <string>()
                    {
                        "TollId", "LicensePlate", "State", "Make", "Model", "VehicleType", "VehicleWeight", "Toll", "Tag"
                    }
                };

                var outputConfigForCountQuery = new CsvOutputConfig
                {
                    OutputFileName = string.Empty,
                    Delimiter      = new string[] { "\t" },
                    CultureName    = "en-US",
                    Fields         = new List <string>()
                    {
                        "Count"
                    }
                };

                // Define console trace listener
                TraceListener tracer = new ConsoleTraceListener();

                // set up advance time settings to enqueue CTIs - to deliver output in a timely fashion
                var advanceTimeGenerationSettings = new AdvanceTimeGenerationSettings(inputConfig.CtiFrequency, TimeSpan.Zero, true);
                var advanceTimeSettings           = new AdvanceTimeSettings(advanceTimeGenerationSettings, null, AdvanceTimePolicy.Adjust);

                // Define input stream object, mapped to stream names
                // Instantiate an adapter from the input factory class
                var inputStream = CepStream <TollReading> .Create(
                    "TollStream",
                    typeof(CsvInputFactory),
                    inputConfig,
                    EventShape.Interval,
                    advanceTimeSettings);


                var passthroughQuery = from e in inputStream select e;

                var countQuery = from w in inputStream.TumblingWindow(TimeSpan.FromMinutes(3), HoppingWindowOutputPolicy.ClipToWindowEnd)
                                 select new TollCount {
                    Count = w.Count()
                };

                // Create a runnable query by binding the query template to the
                // input stream of interval events, and to an output stream of
                // fully ordered point events (through an output adapter instantiated
                // from the output factory class)
                Query query = countQuery.ToQuery(
                    application,
                    "HelloTollTutorial",
                    "Hello Toll Query",
                    typeof(CsvOutputFactory),
                    outputConfigForCountQuery,
                    EventShape.Interval,
                    StreamEventOrder.FullyOrdered);

                query.Start();

                Console.WriteLine("*** Hit Return to see Query Diagnostics after this run ***");
                Console.WriteLine();
                Console.ReadLine();

                // Retrieve  diagnostic information from the CEP server about the query.
                // See the section "Connecting to and Publishing a Server" in the StreamInsight MSDN documentation on
                // how to make the debugger client connect to the server and retrieve these diagnostics
                Console.WriteLine(string.Empty);
                RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/EventManager")), tracer);
                RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/PlanManager")), tracer);
                RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/Application/TutorialApp/Query/HelloTollTutorial")), tracer);

                DiagnosticSettings settings = new DiagnosticSettings(DiagnosticAspect.GenerateErrorReports, DiagnosticLevel.Always);
                server.SetDiagnosticSettings(new Uri("cep:/Server"), settings);

                tracer.WriteLine("Global Server Diagnostics");
                RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/EventManager")), tracer);
                RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/PlanManager")), tracer);
                RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/Query")), tracer);
                tracer.WriteLine(string.Empty);
                tracer.WriteLine("Summary Query Diagnostics for");
                RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/Application/TutorialApp/Query/HelloTollTutorial")), tracer);
                tracer.WriteLine(string.Empty);

                query.Stop();

                Console.WriteLine("*** Query Completed *** Hit Return to Exit");
                Console.WriteLine();
                Console.ReadLine();
            }

            return;
        }