Gather Operator Sample

The Gather.sbapp sample application uses a Gather operator to generate an alarm (a true value) for trades of 10,000 shares or more for a stock whose price is $200 or greater. The input stream contains the fields Symbol,Volume, and Price.

Note

This is a very simple example which might be better done using a single Map operator, but it is instructive. For an example that truly requires a Gather operator, see the Compliance sample application.

Importing This Sample into StreamBase Studio

In StreamBase Studio, import this sample with the following steps:

  • From the top menu, click FileLoad StreamBase Sample.

  • Select operator from the Applications list.

  • Click OK.

StreamBase Studio creates a single project for all the operator samples.

Sample Location

By default, the sample files are installed in:

  • On Windows: C:\Program Files\StreamBase Systems\StreamBase.n.m\sample\operator

  • On UNIX: /opt/streambase/sample/operator

When you load the sample into StreamBase Studio, Studio copies the sample project's files to your Studio workspace. StreamBase Systems recommends that you use the workspace copy of the sample, especially on UNIX, where you may not have write access to /opt/streambase. In the default installation, the path to this sample in your Studio workspace is:

UNIX:       
  ~/streambase-studio-n.m-workspace/sample_operator
Windows XP:
  C:\Documents and Settings\username\My Documents\StreamBase Studio n.m Workspace\
      sample_operator
Windows Vista:
  C:\Users\username\Documents\StreamBase Studio n.m Workspace\
      sample_operator

Running the Gather Sample

Running Gather.sbapp in StreamBase Studio

  1. In the Package Explorer, double-click to open the Gather.sbapp application. Make sure the application is the currently active tab in the EventFlow Editor.

  2. Click the Run button. This opens the SB Test/Debug perspective and starts the application.

  3. In the Application Output view, select the BigTradeAlarm output stream. No output is displayed at this point, but the dequeuer is prepared to receive output. This view will eventually show the output of the application.

  4. In the Manual Input view, select the TradesIn input stream.

  5. Enter intc, 10000, and 201 in the Symbol, Volume, and Price fields, respectively

  6. Click Send Data, and observe this data appears in the BigTradeAlarm stream:

    BigTradeAlarm=true
    
  7. Enter intc, 10000, and 199 in the Symbol, Volume, and Price fields, respectively

  8. Click Send Data, and observe this data appears in the BigTradeAlarm stream:

    BigTradeAlarm=false
    
  9. Enter intc, 1000, and 201 in the Symbol, Volume, and Price fields, respectively

  10. Click Send Data, and observe this data appears in the BigTradeAlarm stream:

    BigTradeAlarm=false
    
  11. Enter intc, 1000, and 187 in the Symbol, Volume, and Price fields, respectively

  12. Click Send Data, and observe this data appears in the BigTradeAlarm stream:

    BigTradeAlarm=false
    
  13. When done, press F9 or click the Stop Running Application button.

Running Gather.sbapp in Terminal Windows

This section describes how to run the sample in UNIX terminal windows or Windows command prompt windows. On Windows, be sure to use the StreamBase Command Prompt from the Start menu as described in the Test/Debug Guide, not the default command prompt.

  1. Open three terminal windows on UNIX, or three StreamBase Command Prompts on Windows. In each window, navigate to the directory where the sample is installed, or to your workspace copy of the sample, as described above.

  2. In window 1, type:

    sbd Gather.sbapp

    The window shows notice[StreamBaseServer] listening on port 10000.

  3. In window 2: type:

    sbc dequeue BigTradeAlarm

    No output is displayed at this point, but the dequeuer is prepared to receive output. This window will eventually show the output of the application.

  4. In window 3, type:

    sbc enqueue TradesIn

    The sbc command is now awaiting keyboard input. Then type: intc, 10000, 201

    Observe this line in the dequeue window: true

  5. Type:

    intc, 10000, 199.

    Observe this line in the dequeue window: false

  6. Type:

    intc, 1000, 201.

    Observe this line in the dequeue window: false

  7. Type:

    intc, 1000, 187.

    Observe this line in the dequeue window: false

  8. In window 2, type: Ctrl-Z (Windows) or Ctrl-D (UNIX) to exit the sbc session.

  9. In window 3, type the following command to terminate the server and dequeuer:

    sbadmin shutdown

Back to Top ^

How We Created the Gather Sample

  1. Launched StreamBase Studio

  2. Created (or subsequently used) the "sample_operator" project.

  3. From the top menu, in the SB Authoring perspective, selected File > New > EventFlow Application. Selected the sample_operator project and entered Gather for the diagram name.

  4. Created an input stream:

    • Dragged an input stream from the palette to the canvas.

    • Clicked it on the canvas, which invoked the Input Stream Properties view

    • On the General tab, Name: TradesIn

    • On the Edit Schema tab, added:

      • Field Name: Symbol, Type: string, Size: 5

      • Field Name: Volume, Type: int

      • Field Name: Price, Type: double

  5. Using the Query operator, created a trade ID (sequence number)

    1. Dragged a Query Table from the palette to the canvas

    2. On the General tab, Name: TradeIDTable

    3. On the Edit Schema tab, added

      • Field Name: TradeID, Type: int

      • Field Name: RowPointer, Type: int

    4. On the Primary Index tab, Selected Fields chose RowPointer

    5. Dragged a Query operator from the palette to the canvas

    6. Connect the TradeIDTable to the new Query operator (this will help fill in some symbol names)

    7. Clicked on Query operator.

    8. On the General tab, Name: AddTradeID

    9. On the Query Settings tab:

      • Operation: Write

      • Where: Primary Index

      • Matches:

        • Field Name: RowPointer

        • Expression: 1

    10. On the Operations Settings tab:

      • Type of Write: Update

      • Values to update:

        • Field Name: TradeID

        • Expression: TradeID+1

      • If update fails because no row was found: Insert new row using new values...

      • Values to insert:

        • Expression 0 (this initializes the counter when the application starts)

    11. On the Output Settings tab:

      • Selected the explicit option.

      • Selected all fields on the input stream and query table.

      • In Query table output, renamed the outputs to TradeID and RowPointer, respectively.

  6. Set up the VolAlarm Map operator:

    • Dragged a Map operator from the palette to the canvas.

    • On the General tab, Name: VolAlarm

    • Connected the output of the AddTradeID operator to the input of the VolAlarm operator.

    • On the Output Settings tab, chose the Output option, All input fields with specified changes, then clicked on the Fill All Rows button to fill in the output fields.

    • Added one more field to the Output Fields:

      • Output field name: VolAlarm

      • Expression: Volume >= 10000

      VolAlarm will be set to "true" when the trade volume is greater than or equal to 10,000 shares.

  7. Set up the PriceAlarm Map operator:

    • Dragged a Map operator from the palette to the canvas.

    • On the General tab, Name: PriceAlarm

    • Connected the output of the AddTradeID operator to the input of the VolAlarm operator.

    • On the Output Settings tab, chose the Output option, All input fields with specified changes, then clicked on the Fill all Rows button to fill in the output fields.

    • Add one more field to the Output Fields:

      • Output field name: PriceAlarm

      • Expression: Price >= 200

      PriceAlarm will be set to true when the price per share is greater than or equal to $200.

  8. Set up the GatherAlarms Gather operator:

    • Dragged a Gather operator from the palette to the canvas.

    • On the General tab, Name: GatherAlarms

    • Connected the outputs of both alarm operators to the input of the GatherAlarms operator.

    • On the Gather Settings tab:

      • Number of input ports: 2

      • Gather tuples on: TradeID

        Gathers the tuples from each stream that have the same value for TradeID and creates one tuple from each pair.

    • On the Output Settings tab, chose the Output option, explicitly specified fields, then added:

      • Output Field Name: BigTradeAlarm

      • Expression: VolAlarm && PriceAlarm

        BigTradeAlarm will be set to true when VolAlarm and PriceAlarm are both true.

  9. Created an output stream:

    • Dragged an output stream from the palette to the canvas.

    • On the General tab, Name: BigTradeAlarm

    • Connected the GatherAlarms operator to the BigTradeAlarm output stream.

Back to Top ^