Writing Your First EventFlow Application

7 minute read

StreamBase DocumentationGetting Started

Writing Your First EventFlow Application

첫 이벤트플로우 어플리케이션

Purpose 목표

In this topic, you will develop your new MyFirstApp.sbapp EventFlow application.

여기서는 MyFirstApp.sbapp EventFlow application을 만들어보자.

Let’s briefly preview the steps in our application design process. First you will provide streams for events to enter and exit the application. You will define a schema that specifies what field names and data types your input stream expects. Then, you will add one or more operators to process the data. Finally, you will connect these components to determine how data flows through the application.

 

Preliminary Step

Before building your application:

  1. In the StreamBase Studio menu, click Window → Preferences. Expand the StreamBase Studio menu and open the Authoring page. Verify that the default setting, Automatically typecheck components, is enabled.To learn about typechecking, see About Typechecking in this topic.
  2. If your application is not already open in an EventFlow Editor, double-click MyFirstApp.sbapp in the Package Explorer to open it. You can have multiple editors open in this area of your perspective, each with its own tab labeled with the file name.Notice the Palette view under the Package Explorer. Whenever an EventFlow is open and its canvas is displayed, the palette is loaded with icons that represent the components you will use to design your EventFlow application. The icons are organized in drawers of different component types, as shown in the figure below.The Palette view is empty when no EventFlow canvas is displayed.

Building Your First EventFlow Application

Follow this procedure to develop the MyFirstApp.sbapp application.

  1. If your application is not already open in an EventFlow Editor, double-click its name (MyFirstApp.sbapp) in the Package Explorer to open it. You can have multiple editors open in this area of your perspective, each with its own tab labeled with the file name.
  2. Add the input stream and define its schema:

    1. In the Palette view, open the Streams category, then drag an Input Stream icon from the palette into your EventFlow canvas:

      Notice the yellow background of the InputStream1 icon: this color is a signal to you that the component does not typecheck yet — in this case, because there is no schema associated with it.

    2. Click the input stream icon in the canvas. When you click any component in an EventFlow Editor, StreamBase Studio opens its Properties view. In the following example, the Properties view is in the upper left:

    3. In the General tab, change the name of the input stream to TradesIn.
    4. Notice that the Edit Schema tab contains has a red  typecheck error icon. This icon should indicate to you that the tab needs attention, so open the Edit Schema tab. It contains an error message (notice the same red error icon) that attempts to help you fix the problem. In this case, it advises you to add fields to the schema.Click the  Add button twice to add a row for each field in the schema. Enter these values in the rows:

      Field Name Type Description
      symbol string Stock symbol
      quantity int Number of shares

      (The descriptions are optional.)

    5. Apply your changes: click a blank area on the canvas, or click the  Apply and Typecheck button near the top-right area of the Properties View. This forces Studio to typecheck your application now, instead of waiting for it to happen automatically. Note that typechecking does not save the application. You can write the application file to disk at any time with File → Save or Ctrl+S or by clicking the Save button in the main toolbar.When you apply your changes, notice that the background color of your TradesIn icon changes from yellow to white. That means the typecheck problem has been cleared, and that Studio is now aware of the schema for the TradesIn component.
  3. Add and configure a Filter operator:

    1. Open the Operators section of the Palette tab. Drag and drop a Filter operator from the palette onto the canvas. Its default name is Filter1. The canvas should now look similar to this:

      Notice that the Filter operator’s background color is yellow. This is because its typecheck status is unknown: it has not been connected yet to a component with a known and valid schema.

    2. If the Outline view is not open, click Window → Show View and choose Outline. This view provides a tree of all the components in your application. It can be helpful for navigating applications with many components. When your application was empty, its outline contained no instances, only a list of component types. Now, if you expand the Operators and Streams categories you should see your current components:

    3. Double-click the Filter1 instance in the Outline view: this selects the component in your canvas and opens its Properties view.
    4. In the General tab, change the name of the operator to TradeFilter.
    5. Open the Predicate Settings tab. Notice that a row in the Predicates table already exists for output port 1, with predicate expression true. Edit this expression in the Predicate column, and specify:quantity >= 10000predicate is an expression that resolves to either true or false for the values in a tuple. Here, the predicate is true for tuples containing trades of 10000 or more; all other tuples are false. The tuple is passed through the Filter operator only if it evaluates to true; all others are ignored.
    6. Now, let’s change the operation slightly: Instead of throwing away tuples with smaller trades, we want to make it possible to check them. To do this, we can make the Filter operators dequeue those tuples to a separate output stream.To make this change, select the option, Create output port for non-matching tuples.

    At this point, your Filter operator’s Properties view should look like this:

    At run time, the Filter operator will evaluate each tuple as it arrives from the input stream. If you had defined multiple predicates, they would be evaluated in order. The first predicate that returns truecauses the filter operation to occur immediately, and any later predicates are ignored.

    We have only one predicate, so each tuple is dequeued either on output port 1 if true, or on output port 2 (the port created for non-matching tuples) if false.

  4. Connect the input stream to the filter operator.The canvas shows each output port as a small black square on the right side of the component. If there are multiple output ports, the upper-most square represents Port 1; the next square down represents port 2, and so on. Your Filter operator now has an extra output port, which you configured in the preceding step.

    Find the output port on the TradesIn icon. Drag your cursor from that square to the input port (the square on the left side) of the filter operator. StreamBase Studio draws an arc connecting the two components. Arcs represent internal streams within EventFlow applications. When you connect the two components, the input stream and the filter operator backgrounds turn white, indicating that they typechecked successfully. For example:

  5. Add the output streams:

    1. Drag and drop an Output Stream icon from the palette into the canvas. Select the output stream on the canvas to open its Properties view.
    2. In the General tab, change the stream’s name to BigTrades.
    3. In the canvas, draw an arc from the upper output port of the Filter operator to the input port of the output stream. Your EventFlow application should look similar to this:

    4. Add a second output stream. Select the output stream, and in its Properties view, change its name to AllTheRest.
    5. Connect the lower output port of the Filter operator to the AllTheRest output stream.

      ### Tip

      If your canvas looks a little disorganized, try one or both of these adjustments:

      • To automatically arrange the components neatly on the canvas, click Diagram → Layout Application or press Ctrl+L.
      • To rearrange the arcs around components (for example, if an arc overlaps a component’s name), click Diagram → Reroute Connections.

      The final layout of your EventFlow application should look similar to the following:

  6. Use File → Save, Ctrl+S, or click the Save button to save your StreamBase Application.

About Typechecking

Typechecking is a kind of validation that StreamBase performs on each component of an application. A StreamBase application will not run if it has typecheck errors. One of the key advantages of designing inStreamBase Studio is that typechecking can be performed automatically as you work, so you can avoid accumulating problems.

Conceptually, typechecking occurs from the starting points of the application (usually the left side) — and proceeds to the downstream components (typically those connected on the right). When the typecheck process finds an error condition, the background color of the component turns to red, and downstream components cannot be typechecked yet. Typechecks can fail for a number of reasons, such as:

  • Incorrect operator parameters (for example, if the user edited the EventFlow file’s XML source code directly and entered an invalid parameter name or value).
  • Incorrect use of the expression language, such as an invalid character in a field name, again entered by a user who edited the sbapp XML directly.
  • An operator that does not have a schema declared yet.
  • An operator has a schema declared, but it is in conflict with the prior (upstream) connected operator.

Summary and Next Steps

In this topic you learned how to define a StreamBase schema, add components to your application, connect them together, and define functionality by editing the properties of one component. You also learned something about StreamBase typechecking. Your application doesn’t do much, but it’s finished and ready to run.

Do not close StreamBase Studio. To continue the tutorial, please click Next to proceed to one of the following topics:

  • If you would like to create a StreamSQL application, complete the next topic, Creating Your First StreamSQL Application.It will do exactly the same work as the MyFirstApp.sbapp application, and in subsequent topics, you will be able to run either version of the application with identical results.
  • If you are not interested in using StreamSQL, skip the next topic: click Next one more time to go on to Running Your First StreamBase Application .