Query Operator Sample

Introduction

This sample contains a Query Table data construct that stores information about member companies comprising the NASDAQ-100 stock index. Each company was assigned an arbitrary color value from the choices {red, yellow, blue}, stored in the table's Color field.

Also see the Sequence operator sample, which illustrates how to use unique IDs generated by a Sequence operator as a Query Table's primary index.

This sample includes the following Query operators:

Query Operator Name Description
LoadNasdaq100Table Accepts input from a CSV File Reader adapter that reads from a CSV data file. The adapter runs automatically when the sample starts to automatically load the Query Table.
ReadNasdaq100Table Accepts a stream with a single string field named Symbol; the Symbol field can be entered with any case letters. Enter a stock symbol to return that symbol's entry in the Query Table, if the symbol is a member of the NASDAQ-100, or an error message if not.
QueryPriceColor Accepts a stream with fields MaxPrice (double) and Color (string). Enter values to return one tuple for each stock that matches the specified color and whose price is less than or equal to the specified price.
QueryPrice Accepts a stream with maximum and minimum price values. Enter a stock price range to return a tuple for each stock whose price falls within the specified range.
UpdatePriceQuery Accepts a stream with Symbol and Price values; the Symbol field can be entered with any case letters. If a row matching the specified Symbol is found in the Query Table, its Price field is updated with the specified Price. If no row with that Symbol is found, no rows are updated or inserted.
DeleteSymbolQuery Accepts a stream with a single Symbol field. Symbols entered must exactly match an entry in the table in spelling and case. The row in the Query Table with the specified Symbol is deleted from the table. If no such row exists, the Query Table is left unchanged.

This Sample's Files

The Query operator sample shares the same Studio project folder as the samples for other operators. The files for the Query operator sample are Query.sbapp and the NASDAQ100.csv data file in the ResourceFiles subdirectory.

Running Query.sbapp in StreamBase Studio

  1. In the Package Explorer, double-click to open the Query.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. On startup, the application uses a CSV File Reader adapter to automatically load the Query Table from the NASDAQ100.csv file.

  3. In the Application Output view, select All Output Streams in the Stream drop-down control. The view initially shows the results of automatically loading the Query Table.

  4. In the Manual Input view, select SymbolStream in the Stream control.

  5. Enter INTC in the Symbol field.

  6. Click Send Data. In the Application Output view, observe the symbol, name, price, color and description of the Intel stock. This stock has been successfully read from the table.

  7. Try another stock symbol that you know is not in the NASDAQ 100 list. For example, enter GE, and observe the entry added to the Application Output view. The Description field contains a string such as:

    Not a member of the NASDAQ-100.
  8. In the Manual Input view, select the PriceStream input stream.

  9. Enter 15 in the MaxPrice and 10 in the MinPrice field.

  10. Click Send Data. In the Application Output view, observe the symbol, name, price, color, and description of five different stocks ranging in price from $12.71 to $14.98. Notice that the stock entries are output in descending order of the secondary key (Price,Color).

  11. In the Manual Input view, select the PriceColorStream input stream.

  12. Enter 17 in the MaxPrice and blue in the Color field.

  13. Click Send Data. In the Application Output view, observe the symbol, name, price, color, and description of three blue stocks whose price is less than $17.00. Notice that the stock entries are output in ascending order of the secondary key (Price,Color).

  14. In the Manual Input view, re-select the SymbolStream input stream.

  15. Enter ADBE in the Symbol field and click Send Data.

  16. Select the row this adds to the Application Output view. Notice that the current price of ADBE stock is $28.43.

  17. In the Manual Input view, select the UpdatePriceStream input stream.

  18. Enter ADBE in the Symbol field and 32.75 in the Price field.

  19. Select the row this adds to the Application Output view. Notice that the price of ADBE stock in the table is now $32.75.

  20. In the Manual Input view, select the DeleteSymbolStream input stream.

  21. Enter ADBE in the Symbol field. This time, you must use uppercase letters as shown.

  22. Click Send Data. In the Application Output view, observe the Adobe row emitted from the DeleteSymbolOutput stream to confirm the row that was deleted.

  23. Once again select the SymbolStream input stream and enter ADBE. This time, the resulting row shows "Not a member of the NASDAQ-100."

  24. Experiment with further entries on any of the five input streams.

  25. When done, press F9 or click the Stop Running Application button.

Running Query.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, start StreamBase Server running the Query sample application. You must specify the provided server configuration file so that the application can locate the nasdaq100.tab file in the ResourceFiles folder. Enter:

    sbd -f op-samples.sbconf Query.sbapp
  3. In window 2, start the dequeuer. Enter:

    sbc dequeue

    No output is displayed at this point, but the dequeuer is prepared to receive output. This window eventually shows the output of the all the query operations.

  4. In window 3, enqueue data to your application. Enter the following command:

    sbc enqueue SymbolStream
  5. Window 3 now waits for manual input. Type:

    INTC

    In window 2, observe the symbol, name, and description of the Intel stock. This stock has been successfully read from the table.

  6. In window 3, type:

    GE

    Observe a message containing the following string in window 2:

    Not a member of the NASDAQ-100.
  7. In window 3, type:

    Ctrl+C
    sbc enq PriceStream
    15,10

    In window 2, observe the symbol, name, price, color and description of five different stocks whose price range is between $10.00 to $15.00. The stock entries are sent in descending order of the secondary key (Price,Color).

  8. In window 3, type:

    Ctrl+C
    sbc enq PriceColorStream
    17,blue

    In window 2, observe the symbol, name, price, color and description of three stocks whose color field is blue and whose price is less than $17.00. The stock entries are sent in ascending order of the secondary key (Price,Color).

  9. In window 3, type:

    Ctrl+C
    sbc enq SymbolStream
    ADBE

    In window 2, observe that the table's current price for ADBE stock is $28.43.

  10. In window 3, type:

    Ctrl+C
    sbc enq UpdatePriceStream
    ADBE, 32.75

    In window 2, notice that the table's new price for ADBE stock is $32.75.

  11. In window 3, type:

    Ctrl+C
    sbc enq DeleteSymbolStream
    ADBE

    In window 2, observe that the Adobe entry in the table is emitted to confirm the row that was deleted.

  12. In window 3, type:

    Ctrl+C
    sbc enq SymbolStream
    ADBE

    In window 2, notice that this time the emitted entry for ADBE reports "Not a member of the NASDAQ-100."

  13. Experiment with further entries on any of the five input streams.

  14. In window 3, type: Ctrl+C to exit the sbc session.

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

    sbadmin shutdown

Importing This Sample into StreamBase Studio

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

  • From the top menu, select FileLoad StreamBase Sample.

  • In the search field, type operator to narrow the list of samples.

  • Select Operator sample group from the Data Constructs and Operators category.

  • Click OK.

StreamBase Studio creates a single project for the operator sample group.

Sample Location

When you load the sample into StreamBase Studio, Studio copies the sample project's files to your Studio workspace, which is normally part of your home directory, with full access rights.

Important

Load this sample in StreamBase Studio, and thereafter use the Studio workspace copy of the sample to run and test it, even when running from the command prompt.

Using the workspace copy of the sample avoids the permission problems that can occur when trying to work with the initially installed location of the sample. The default workspace location for this sample is:

studio-workspace/sample_operator

See Default Installation Directories for the location of studio-workspace on your system.

In the default TIBCO StreamBase installation, this sample's files are initially installed in:

streambase-install-dir/sample/operator

See Default Installation Directories for the default location of studio-workspace on your system.

Related Topics

For another use of the Query operator, see the Query TopN Sample, which tracks a sorted range of values, such as the highest (or lowest) prices.