Query Operator Sample

About This Sample

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.

Importing This Sample into StreamBase Studio

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

  • From the top-level menu, click File>Import Samples and Community Content.

  • Enter sample group to narrow the list of options.

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

  • Click Import Now.

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

Running This Sample in StreamBase Studio

  1. In the Project Explorer view, open the sample you just loaded.

    If you see red marks on a project folder, wait a moment for the project to load its features.

    If the red marks do not resolve themselves after a minute, select the project, right-click, and select Maven>Update Project from the context menu.

  2. Open the src/main/eventflow/com.tibco.sb.sample.operator folder.

  3. Open the Query.sbapp file and click the Run button. This opens the SB Test/Debug perspective and starts the module. On startup, the application uses a CSV File Reader adapter to automatically load the Query Table from the NASDAQ100.csv file.

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

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

  6. Enter INTC in the Symbol field.

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

  8. 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 Output Streams view. The Description field contains a string such as:

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

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

  11. Click Send Data. In the Output Streams 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).

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

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

  14. Click Send Data. In the Output Streams 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).

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

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

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

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

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

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

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

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

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

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

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

  26. When done, press F9 or click the Terminate EventFlow Fragment button.

This Sample's Files

The Query operator sample includes the following files:

Query.sbapp

Sample application file.

NASDAQ100.csv

Data simulation file.

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 permission problems. The default workspace location for this sample is:

studio-workspace/sample_operator

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