This example uses a reference table query within a SPLASH routine that iterates over the rows in a table to obtain data about a specified player's batting history that is then used to compute statistics.
For example, for a table named Batting, with an integer column, teamAtBat, a string column, player, and an integer column, bases, that can be accessed by a service named HANA, create a reference table query named AtBats:
CREATE REFERENCE AtBats SCHEMA (teamAtBat INTEGER, player STRING, bases INTEGER) PRIMARY KEY (teamAtBat) PROPERTIES service='HANA',source='Batting',sourceSchema='BattingSchema';
The teamAtBat is incremented each time a player bats, providing a unique number for each at bat by a member of the team, so that column is the primary key in the source table. It must also be the primary key of the reference table query. Because we have specified a primary key in this reference, we have to include the sourceSchema property, which identifies the schema, BattingSchema, of the database containing the table.
For an event which is the appearance, or mention, of a player's name, create an input stream named StatRequest, consisting of a single string, player.
CREATE INPUT STREAM StatRequest SCHEMA (player STRING);
CREATE FLEX Percentage IN AtBats, StatRequest OUT OUTPUT STREAM PercentageOutput SCHEMA (atBatPercentage float) BEGIN ON StatRequest { integer atBats := 0; integer hits := 0;
} float percentage := hits/atBats; output [atBatPercentage=percentage]; }; END;
for ( record in AtBats_stream ) { if ( record.player = StatRequest.player ) { if ( record.bases > 0 ) { hits := hits + 1; } atBats := atBats + 1; } }
This method has the advantage of being easy to read and maintain.
for ( record in AtBats_stream where player = StatRequest.player ) { if ( record.bases > 0 ) { hits:= hits + 1; } atBats := atBats + 1; }
This method has the advantage of reducing processing time, because the records of other players are never pulled from the reference table.
AtBats_iterator := getIterator( AtBats_stream ); resetIterator( AtBats_iterator); setRange( AtBats_iterator, player, StatRequest.player ); typeof(AtBats) result := getNext( AtBats_iterator ); while (not(isnull(result))) { if ( result.bases > 0 ) { hits := hits + 1; } atBats := atBats + 1; result := getNext( AtBats_iterator ); }
This method has the advantage of giving you the most explicit control over the processing.