Contents
This sample contains several Query operators and an associated Query Table data construct that stores stock index data, and enables you to look up a symbol's name and stock description.
In StreamBase Studio, import this sample with the following steps:
-
From the top menu, click → .
-
Select operator from the Applications list.
-
Click OK.
StreamBase Studio creates a single project for the operator samples.
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 Studion.mWorkspace\ sample_operator Windows Vista: C:\Users\username\Documents\StreamBase Studion.mWorkspace\ sample_operator
The Query operator sample shares the same Studio project folder as the samples for other operators.
The sample_operator project directory includes a
nasdaq100.tab data file that contains stock names,
symbols, and descriptions.
-
In the Package Explorer, double-click to open the
Query.sbappapplication. Make sure the application is the currently active tab in the EventFlow Editor. -
Click the
Run button. This opens the SB
Test/Debug perspective and starts the application.
-
In the Application Output view, select
Allin theOutput streamcontrol. No output is displayed at this point, but the dequeuer is prepared to receive output. This view eventually shows the output of the application. -
Open a StreamBase Command Prompt (on Windows) or terminal window on UNIX. In the window:
-
Navigate to the directory where the sample is installed, or to your workspace copy of the sample, as described above.
-
Load the stock entries in the
nasdaq100.tabdata file into the sample application's query table:sbc enqueue -dtab Nasdaq100In < nasdaq100.tab
Each stock entry has the stock symbol, company name, price, a color (arbitrarily assigned red, blue, or yellow), and company description. The color entry is an example of a custom characterization of the stock, and could be any custom field.
-
-
In the Manual Input view, select
SymbolStreamin theInput streamcontrol. -
Enter
INTCin the Symbol field. -
Click . 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.
-
Try another stock symbol that you know is not in the NASDAQ 100 list. For example, enter
GE, and observe the message in the dequeue window:Symbol=GE, Name="UNKNOWN", Price=null, Color=null, Description="Not a member of the Nasdaq100"
-
In the Manual Input view, select the
PriceStreaminput stream. -
Enter
10in theMaxPriceand5in theMinPricefield. -
Click Send Data. In the Application Output view, observe the symbol, name, price, color, and description of five different stocks ranging in price from $8.94. to $5.92. Note that the stock entries are sent in descending order of the secondary key (Price,Color).
-
In the Manual Input view, select the
PriceColorStreaminput stream. -
Enter
in the10MaxPriceandbluein theColorfield. -
Click Send Data. In the Application Output view, observe the symbol, name, price, color, and description of four
bluestocks ranging in price from $2.13. to $8.94. Note that the stock entries are sent in ascending order of the secondary key (Price,Color). -
When done, press F9 or click the
Stop Running Application button.
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.
-
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.
-
In window 1, start StreamBase Server running the Query sample application. Enter:
sbd Query.sbapp
Look for output similar to the following:
notice[StreamBaseServer] listening on port 10000
-
In window 2, load the stocks into the table by entering:
sbc enqueue -dtab Nasdaq100In < nasdaq100.tab
-
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.
-
In window 3, enqueue data to your application. Enter the following command:
sbc enqueue SymbolStream
-
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.
-
In window 3, type:
GE
Observe a message like the following in window 2:
"Unknown", "Not a member of the Nasdaq100"
-
In window 3, type:
Ctrl-C sbc eng PriceStream 10,5
Observe the symbol, name, price, color and description of five different stocks in window 2, ranging in price from $8.94 to $5.92. The stock entries are sent in descending order of the secondary key (Price,Color).
-
In window 3, type:
Ctrl-C sbc eng PriceColorStream 10,5
Observe the symbol, name, price, color and description of 4 different
bluestocks in window 2, ranging in price from $2.13 to $8.94. The stock entries are sent in ascending order of the secondary key (Price,Color). -
In window 3, type: Ctrl-C to exit the sbc session.
-
In window 3, type the following command to terminate the server and dequeuer:
sbadmin shutdown
-
Launched StreamBase Studio
-
Created the
sample_operatorproject. -
From the top menu, in the SB Authoring perspective, selected File > New > EventFlow Application. Selected the
sample_operatorproject and enteredQueryfor the diagram name. -
Created four input streams. For each one:
-
Dragged an input stream from the palette to the canvas.
-
Clicked the input stream icon on the canvas to its Properties view.
-
On the General tab, entered the stream name in the Name: field.
-
On the Edit Schema tab, entered the schema's field names, types, and sizes, as in the following example:
Schema data properties used for Query sample application PriceColorStream input stream.
The following table shows the stream names and schema values used for all four input streams:
Input Stream Field Name Type Size PriceColorStream MaxPrice double 8 Color string 8 PriceStream MaxPrice double 8 MinPrice double 8 Nasdaq100In Name string 30 Symbol string 5 Price double 8 Color string 8 Description string 1024 SymbolStream Symbol string 5
-
-
Created the Query Table data construct:
-
Dragged a Query Table data construct icon from the palette to the canvas.
-
Clicked the Query Table icon on the canvas to open its Properties view.
-
On the General tab, Name:
Nasdaq100Table -
On the Table Settings tab, selected Type:
In memory.Note: When you are defining a Query Table data construct, your choice of "In memory" or "On disk" options should be based on the persistence and performance requirements of your application. The memory-resident Query Table will always be faster, but its size is restricted by address space limitations. Use the disk-based Query Table (available only in StreamBase Enterprise Edition) when address space is an issue, or you need your data to persist when the server is shut down and restarted.
-
On the Edit Schema tab, clicked the Copy Schema button in the upper right corner. On the Copy Schema from Existing Component dialog, we selected:
Project: sample_operator
Application: Query
Copy schema from: Nasdaq100In Input Stream
For example:
-
Clicked OK.
-
On the Primary Index tab, selected
Symbolas the primary key. For example:
-
On the Secondary Indices tab, added a secondary key:
-
Clicked Add....
-
In the Edit Secondary Index dialog, selected Price and Color in Available Fields. Then clicked the top right-arrow button to load Price and Color in Selected Fields.
-
Selected the option
Ordered, with ranges (btree). -
Clicked OK.
-
-
-
Set up the
LoadNasdaq100TableQuery operator to write input tuples to (load)Nasdaq100TableQuery Table:-
Dragged a Query operator icon from the palette to the canvas.
-
Connected the new Query operator to the
Nasdaq100TableQuery Table data construct. -
Connected the
Nasdaq100Ininput stream to the new Query operator -
Clicked the Query operator on the canvas to open its Properties view.
-
In the General tab Name field, entered ReadNasdaq100Table.
-
In the Query Settings tab:
-
Operation:
Write -
Where:
Primary Index -
Matches:
Symbol:
Symbol
-
-
In the Operation Settings tab:
-
Type of write:
Insert -
Values to insert
For each
Field Namethe followingExpression:-
Name:
Name -
Price:
Price -
Color:
Color -
Description:
Description
-
-
If insert fails because an existing row was found:Ignore.Here we elected to do nothing to the existing record because we chose to not overwrite existing stock records that reside in the Query Table.
At this point, the Operations Settings tab looks like this:
-
-
On the Output Values tab, we want to emit only the Name field for the stock as received from the
Nasdaq100Ininput stream. We chose theexplicitly specified fieldsoption, then selected only the input.Name field.
Note: Only fields that are selected (checked) are output; unselected (unchecked) fields not shown are omitted.
-
-
Set up the
QueryPriceColoroperator to read data from the Query Table:-
Dragged a Query operator icon from the palette to the canvas.
-
Connected the new Query operator to the
Nasdaq100TableQuery Table data construct. -
Connected the
PriceColorStreaminput stream to the new Query operator -
Clicked the Query operator on the canvas to open its Properties view.
-
In the General tab Name field, entered QueryPriceColor.
-
In the Query Settings tab:
Operation:
ReadWhere:
Secondary Index (Price,Color)Because the secondary index in the Query Table is a btree index and this is a read operation, this tab enabled us to specify a query range for each field in the index.
-
In the Operation Settings tab:
The tab loads non-key fields, based on the key we selected in the Query Settings tab. For each of the non-key fields, we entered values to by used if the field's data record is not found in the Query Table. If a record is found, we will emit its matched value from the Query Table.
For each Field name, we defined the output
Expressionas follows:-
Symbol:
string(null) -
Name:
string(null) -
Price:
string(null) -
Color:
string(null) -
Description: "No stock matched the color and price requirements"
-
-
In the Output Settings tab:
Order:
AscendingNote: The sort order option was available because we are doing a read operation on a btree index.
Next, we selected the fields to be output from this operator's input stream and query table, and changed the output field names to make them more readable. (Note: the output field names will be referenced later by the output stream connected to this operator.) The following table shows only the fields that were selected:
Input stream fields chosen for this operator to output
Field Output Field Name input.MaxPrice MaxPrice input.Color Color
Input stream fields chosen for this operator to output
Field Output Field Name current.Symbol Symbol current.Name Name current.Price Price current.Description Description
-
-
Set up the
QueryPriceoperator to read data from the Query Table:-
Dragged a Query operator icon from the palette to the canvas.
-
Connected the new Query operator to the
Nasdaq100TableQuery Table data construct. -
Connected the
PriceStreaminput stream to the new Query operator -
Clicked the Query operator on the canvas to open its Properties view.
-
In the General tab Name field, entered QueryPrice.
-
In the Query Settings tab:
Operation:
ReadWhere:
Secondary Index (Price,Color)Again, we specified a query range for each field in the btree index:
Query Settings for the QueryPriceColor operator
Field Name Type Size Start Operation Start Expression End Operation End Expression Price double 8 > MinPrice ≤ MaxPrice Color string 8 begin end
-
In the Operation Settings tab:
Entered the following alternate values for records not found. Note: We completed this tab just as in the
QueryPriceColoroperator; only the Description is different:-
Symbol:
string(null) -
Name:
string(null) -
Price:
string(null) -
Color:
string(null) -
Description:
"No stocks from within this price range"
-
-
In the Output Settings tab:
For this operator we chose the opposite btree sort order from the previous operator, and we chose to output all of the available fields.
Order:
DescendingInput stream fields chosen for this operator to output
Field Output Field Name input.MaxPrice MaxPrice input.MinPrice MinPrice
Input stream fields chosen for this operator to output
Field Output Field Name current.Symbol Symbol current.Name Name current.Price Price current.Color Color current.Description Description
-
-
Set up the
ReadNasdaq100Tableoperator to read data from the Query Table:-
Dragged a Query operator icon from the palette to the canvas.
-
Connected the new Query operator to the
Nasdaq100TableQuery Table data construct. -
Connected the
SymbolStreaminput stream to the new Query operator -
Clicked the Query operator on the canvas to open its Properties view.
-
In the General tab Name field, entered ReadNasdaq100Table.
-
In the Query Settings tab:
-
Operation:
Write -
Where:
Primary Index -
Matches:
Symbol:
Symbol
-
-
In the Operation Settings tab:
Type of write:
InsertEntered the following alternate values for records not found:
-
Symbol:
input.Symbol -
Name:
'"UNKNOWN"' -
Price:
double(null) -
Color:
string(null) -
Description:
'"Not a member of the Nasdaq100"'
-
-
In the Output Settings tab:
Since the Primary Index key used by this operator is not a btree index, we cannot sort on it (the
Orderis not applicable). We choose to output all the Query Table fields, and none of the input stream fields:Input stream fields chosen for this operator to output
Field Output Field Name current.Symbol Symbol current.Name Name current.Price Price current.Color Color current.Description Description
-
-
Created three output streams. For each one:
-
Dragged an output stream from the palette to the canvas.
-
Clicked the output stream icon on the canvas to open its Properties view.
-
On the General tab, entered its name in the Name field. Used the following names for the three streams:
-
PriceColorQueryOutput -
PriceQueryOutput -
SymbolOutputStream
-
-
-
Connected the three output streams to their corresponding Query operators, as follows:
Output stream connections to Query operators
Input Stream Query Operator connected PriceColorStream QueryPriceColor PriceStream QueryPrice Nasdaq100In Nasdaq100Table SymbolStream ReadNasdaq100Table
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.
