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 menu, click FileLoad StreamBase Sample.

  • Type operator to narrow the list of options.

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

  • Click OK.

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

Running Query.sbapp in StreamBase Studio

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

  2. Open the src/main/eventflow folder.

  3. Open the package folder (most samples contain a single package folder. Open the top-level package folder if your sample contains more than one folder).

  4. Open the Query.sbapp application and 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.

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

    If red marks do not resolve themselves in a moment, select the project and right-click MavenUpdate Project from the context menu.

  5. 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.

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

  7. Enter INTC in the Symbol field.

  8. 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.

  9. 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.
  10. In the Manual Input view, select the PriceStream input stream.

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

  12. 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).

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

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

  15. 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).

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

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

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

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

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

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

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

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

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

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

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

  27. When done, press F9 or click the Stop Running Application 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.