JDBC External Adapter

This topic describes the StreamBase external adapter for JDBC, which allows StreamBase to communicate with remote SQL databases.

StreamBase also includes an alternate way to connect to JDBC databases, using the JDBC data construct and a Query operator in StreamBase Studio. See the JDBC Data Source Overview.

Deprecation Notice

As of StreamBase release 7.2.0, the JDBC External adapter is deprecated and will be removed in a future release. New applications should migrate to the embedded support for JDBC connections described above.

Introduction

This topic describes the StreamBase External Adapter for JDBC, which allows StreamBase to communicate with any remote database that provides a JDBC-compliant driver for JDBC 2.0 or later. This adapter allows StreamBase to read from, output to, and modify the remote database using SQL statements that can be bound to StreamBase tuples.

A sample application is included with the kit. The sample appears in StreamBase Studio's Load StreamBase Sample dialog only after you install the JDBC External Adapter kit.

The sample contains an example EventFlow application file, highs_and_lows.sbapp, and a configuration file, highs_and_lows.sbconf, that can help you get started using your JDBC adapter. For details, refer to the sample's README file.

Configuration File

This is an XML configuration file that is the main configuration file for the JDBC adapter, and the only (required) argument to the JDBC adapter. The configuration file defines connections to StreamBase Servers and databases, and also defines all SQL statements/queries the adapter will run. These can be one-time queries or long-running queries that act as a bridge between StreamBase Servers and SQL database systems. The StreamBase Adapter for JDBC uses threads to handle any number of queries at any time, although you may run more than one StreamBase Adapter for JDBC across multiple machines if necessary.

For an example of a JDBC configuration file, see highs_and_lows.sbconf in the sample directory.

Below is the description of the four types of configuration blocks that make up a JDBC Adapter configuration file.

Sample Application

A sample application is included with the kit. It is installed in the sample directory, as jdbc. The sample contains an example .sbapp file, highs_and_lows.sbapp and a configuration file, highs_and_lows.sbconf, can help you get started using your JDBC adapter. For details, please refer to the sample's README file.

StreamBase Endpoint Block

Zero or more StreamBase endpoint blocks, defining named connections to StreamBase Servers that can be used by any number of queries.

<sb-endpoint name="[name]" uri="[uri]"/>

Attributes

name

Any user defined name (must be unique across all sb-endpoint definitions). Other configuration blocks refer to a StreamBase connection using this value in a sb-endpoint attribute.

uri

StreamBase URI as defined in the StreamBase documentation.

Example

Define "sb1" to be a StreamBase connection to a StreamBase Server running on localhost at port 10000:

<sb-endpoint name="sb1" uri="sb://localhost:1000/"/>

JDBC Connection Block

One or more JDBC Connection blocks, which define named connections to remote databases using JDBC, that can then be used by any number of queries.

<jdbc-connection name="[name]" driver="[driver]"
     connection-url="[curl]" username="[user]" password="[password]"/>

Attributes

name

Any user defined name (must be unique across all jdbc-connection definitions). Other configuration blocks refer to a database Connection using this value in a db-connection attribute.

driver

Java Class for the JDBC driver to load. When launching the adapter, ensure that your CLASSPATH has been set properly for the adapter to successfully load your database driver.

curl

JDBC URL compatible with the driver. Do not put username/password information here even if the URL allows it.

user

Username to allow access to the remote database

password

Password to allow access to the remote database

Example

Define "c1" to be a JDBC Connection using an Oracle JDBC driver, connecting to the "mydb" database server, "db" instance over port 1521, logging in as username "scott" and password "tiger".

<jdbc-connection name="c1" driver="oracle.jdbc.driver.OracleDriver"
     connection-url="jdbc:oracle:thin:@mydb:1521:db"
     username="scott" password="tiger"/>

Startup Query Configuration Block

Zero or more Startup query configuration blocks. Startup queries are executed when the adapter starts, and run only once. They are all executed in the order in which they appear in the configuration file. Startup queries execute any query on a database, requiring a db-connection attribute, and may optionally retrieve data from the database and enqueue the results back onto a StreamBase Input Stream, requiring then both a sb-connection and dest-stream attributes.

The adapter by default will execute each Startup Query, one at a time, blocking until each query has finished. Optionally, if you have queries that may execute at the same time, you can use a background option that allows a query to execute in the background, allowing the adapter to continue on to the next startup query. Regardless of background settings, the adapter will always wait for all startup queries to finish before running any other non-startup queries.

<startup db-connection="[db]" [name="name"] [sb-connection="[sb]" dest-stream="[stream]"]>
        <param name="sql" value="[sql]"/>
        [<param name="run-in-background" value="[background]"/>]
        [<param name="fetch-size" value="[f]"/>]
        [<param name="streambase-buffering-size" value="[b]"/>]
        [<param name="enqueue-on-query-completion" value="[tuple]"/>]
</startup>

Attributes

db

The name of a previously defined JDBC Connection block

Optional
name

An optional user-defined name, used during notice, warnings and debug messages printed out by the adapter. If none is provided, a name is generated.

db

The name of a previously defined StreamBase endpoint block. Used when the query is expected to produce results, and you wish to enqueue these into your StreamBase application

stream

The name of an Input Stream on the StreamBase Server on which to enqueue the results produced by the query, required if specifying a sb-connection

Parameters

sql

SQL statement or query to execute on the database

Optional
background

If set to 'true', the Startup Query will begin executing and run in a background thread, allowing the adapter to continue executing other Startup queries

f

JDBC driver hint to set the number of rows fetched per retrieval (see the JDBC API for more details). If missing, the default fetch size used by your driver is left unchanged.

b

Sets the buffer size (in number of tuples) to set for the StreamBase enqueue operations when retrieving the results. A timeout of 250ms will be used. If missing, the StreamBase API will not buffer enqueues. See the StreamBase Java Client librar documentation for more details

tuple

If provided, a tuple will be enqueued onto dest-stream using the values provided. This tuple helps recognize the end of the result set, and is enqueued even if the result set returned no rows. The tuple values must be specified one per field, separating each with a comma (','), in order. You can specify a partial tuple, in which case missing values will be set to zero (currently, 0 for integers, 0.0 for doubles, the empty string ("") for strings, 0ms past 1970-01-01 00:00 GMT for timestamps, false for booleans).

Example

Define a query called start1 that executes against the JDBC Connection named c1, that deletes everything from a table called DATA.

<startup db-connection="c1" name="start1">
   <param name="sql" value="DELETE FROM DATA"/>
 </startup>

Example

Define a query called start2 that executes against the JDBC Connection named c2, issuing a SELECT statement against a table called PRELOAD and enqueuing its results onto the Preload Input Stream on the StreamBase Server endpoint named sb1. A tuple (-1,0) will be enqueued after all results (if any) are enqueued.

<startup db-connection="c1" sb-connection="sb1" dest-stream="Preload"
          name="start2">
   <param name="sql" value="SELECT ID, VALUE FROM PRELOAD"/>
   <param name="enqueue-on-query-completion" value="-1/">
</startup>

Data Manipulation Language (DML) Statement

Zero or more DML statements: queries that modify table contents such as INSERT or UPDATE queries) configuration blocks. DML blocks define passive statements that are executed when certain conditions are met, without enqueueing any results back onto the StreamBase Server. Currently, the only supported condition is the appearance of a tuple on a specified Output Stream from a StreamBase Server. The statement can pull values from the tuple that caused it to be executed.

<dml db-connection="[db]" [name="name"] sb-connection="[sb]" trigger-stream="[stream]">
    <param name="sql" value="[sql]"/>
    [<param name="use-prepared-statement" value="[prepare]"/>]
    [<param name="batching-type" value="[batchtype]"/>]
    [<bindings>
         (<parameter pos="[n]" field="[f]"/>)+
     </bindings>]
</dml>

Attributes

db

The name of a previously defined JDBC Connection block

sb

The name of a previously defined StreamBase endpoint block. This is the StreamBase Server whose trigger-stream will trigger this statement

stream

The name of an Output Stream on the StreamBase Server from which to dequeue tuples that trigger the execution of the SQL statement. For every tuple dequeued from this stream, the statement is executed once

Optional
name

An optional user-defined name, used during notice, warnings and debug messages printed out by the adapter. If none is provided, a name is generated.

Parameters

sql

SQL statement executed on the database. If the statement contains question mark characters, a bindings block is expected

Optional
use-prepared-statement

If set to true, a JDBC PreparedStatement is created once, and reused for every execution. Otherwise, a Statement is used instead. Although PreparedStatements are usually faster, certain JDBC drivers are faster when using Statements, as the query string is created by simple concatenation, and not through JDBC's binding API. The default is for the query to use PreparedStatements

batching-type

Set to none or sb-match (default 'none'). When set to none, each dequeued tuple will execute the statement immediately. If set to sb-match, statement executions will be batched up to the same number of dequeued tuples that the StreamBase API received at once. (That is, if the dequeue received a batch of five tuples, the adapter will batch up the five executions and execute all give statements in a single operation.)

bindings

A bindings block defines tuple-to-SQL bindings when question mark characters are present in the sql parameter. A bindings block consists of one or more parameter tags with the following attributes:

  • pos: The index of the question mark character being bound, starting at 1.

  • f: A field name (from the trigger-stream Output Stream whose value will replace the question mark character being bound.

Example

Define a DML block named clear that executes against the JDBC Connection named c1, deleting everything from a table called DATA every time a tuple is dequeued from the DeleteAll Output Stream on the StreamBase Server endpoint named sb1.

<dml db-connection="c1" sb-connection="sb1" name="clear" trigger-stream="DeleteAll">
   <param name="sql" value="DELETE FROM DATA"/>
 </dml>

Example

Define a DML block called clear2 that executes against the JDBC Connection named c1, deleting certain rows from a table called DATA every time a tuple is dequeued from the DeleteID Output Stream on the StreamBase Server endpoint named sb1. The rows to delete are those whose ID has the same value as the id field from the tuple just dequeued from DeleteID. The statement will be executed in batches matching the number of tuples that were dequeued as a batch.

<dml db-connection="c1" sb-connection="sb1" name="clear2" trigger-stream="DeleteID">
   <param name="sql" value="DELETE FROM DATA WHERE ID = ?"/>
   <param name="batching-type" value="sb-match"/>
   <bindings>
      <parameter pos="1" field="id"/>
   </bindings>
 </dml>

Query Configuration Block

Zero or more Query configuration blocks. Query blocks define passive statements that are executed when certain conditions are met, and unlike DML blocks, expect a result set that will be enqueued back onto the StreamBase Server. The two supported conditions are the appearance of a tuple on a specified Output Stream from a StreamBase Server, and a timer that periodically executes the query. The query can use bindings to values from the tuple that caused it to be executed in the former case.

<query db-connection="[db]" [name="name"] sb-connection="[sb]" type="[type]"
   dest-stream="[dstream]" [trigger-stream="[tstream]" | interval="[interval]"]>
    <param name="sql" value="[sql]"/>
    [<param name="fetch-size" value="[f]"/>]
    [<param name="streambase-buffering-size" value="[b]"/>]
    [<param name="enqueue-on-query-completion" value="[tuple]"/>]
    [<bindings>
         (<parameter pos="[n]" field="[f]"/>)+
     </bindings>]
</query>

Attributes

db

the name of a previously defined JDBC Connection block

sb

the name of a previously defined StreamBase endpoint block. This is the StreamBase Server whose trigger-stream will trigger this statement

type

one of triggered or poll

dstream

the name of an Input Stream on the StreamBase Server on which to enqueue the results produced by the query

tstream

for triggered queries only; the name of an Output Stream on the StreamBase Server from which to dequeue tuples that trigger the execution of the SQL statement. For every tuple dequeued from this stream, the query is executed once

interval

for poll queries only; the time in ms to sleep between executions of the query

Optional
name

an optional user-defined name, used during notice, warnings and debug messages printed out by the adapter. If none is provided, a name is generated.

Parameters

sql

SQL query executed on the database. If the query contains question mark characters, a bindings block is expected

Optional
f

JDBC driver hint to set the number of rows fetched per retrieval (see the JDBC API for more details). If missing, the default fetch size used by your driver is left unchanged.

b

sets the buffer size (in number of tuples) to set for the StreamBase enqueue operations when retrieving the results. A timeout of 250ms will be used. If missing, the StreamBase API will not buffer enqueues. See the StreamBase Java Client library documentation for more details.

tuple

if provided, a tuple will be enqueued onto dest-stream using the values provided. This tuple helps recognize the end of the result set, and is enqueued even if the result set returned no rows. The tuple values must be specified one per field, separating each with a comma (','), in order. You can specify a partial tuple, in which case missing values will be set to "zero" (currently, 0 for integers, 0.0 for doubles, the empty string ("") for strings, 0ms past 1970-01-01 00:00 GMT for timestamps, false for booleans). You can also enter the name of a field from the triggering stream (for triggered queries), whose entry will be replaced with its value.

bindings

(applies only to triggered queries) a bindings block defines tuple-to-sql bindings when question mark characters are present in the sql parameter. a bindings block consists of one or more parameter tags with the following attributes:

  • pos The index of the question mark character being bound, starting at 1.

  • field A field name (from the trigger-stream Output Stream whose value will replace the '?' being bound.

Example

Define a Query block called poll that executes against the JDBC Connection named c1, reading everything from a table called LAST_UPDATE every 5 seconds. The results are sent to the LastUpdates stream.

<query db-connection="c1" sb-connection="sb1" name="poll" type="poll" 
 interval="5000" dest-stream="LastUpdates">
 <param name="sql" value="SELECT * FROM Last_Update"/>
</query>

Example

Define a Query block called feed1 that executes against the JDBC Connection named c1, reading everything from a table called DATA every time a tuple is dequeued from the ReadAll Output Stream on the StreamBase Server endpoint named sb1. The results are enqueued onto the AllData Input Stream.

<query db-connection="c1" sb-connection="sb1" name="feed1" type="triggered"
    trigger-stream="ReadAll" dest-stream="AllData">
    <param name="sql" value="SELECT * FROM DATA"/>
</query>

Example

Define a Query block called feed2 that executes against the JDBC Connection named c1, reading selectively from a table called DATA every time a tuple is dequeued from the ReadID Output Stream on the StreamBase Server endpoint named sb1. The results are enqueued onto the AllData Input Stream. The rows retrieved are those whose ID value matches the id field value from trigger tuple.

<query db-connection="c1" sb-connection="sb1" name="feed2" type="triggered"
    trigger-stream="ReadAll" dest-stream="AllData">
    <param name="sql" value="SELECT * FROM DATA WHERE ID = ?"/>
    <bindings>
      <parameter pos="1" field="id"/>
    </bindings>
</query>

Running the Adapter

Once you have the adapter configured, you are ready to run the adapter.

StreamBase Servers and JDBC databases should already be started before the Adapter is run.

The StreamBase Adapter for JDBC is contained in the sb-jdbc.jar file that resides under the lib directory where StreamBase was installed, and has the same Java version requirements as the rest of the product. (However, while the full JDK is required to run StreamBase applications, you can use either the JRE or JDK for the StreamBase Adapter for JDBC.)

The installation has also placed an executable called sb-jdbc that should be used to launch the adapter. To run the adapter then, simply execute sb-jdbc followed by a single argument: the name of the configuration file.

Extending the JDBC Adapter Classpath

The StreamBase Adapter for JDBC does not ship with any JDBC database drivers. The relational database vendor will provide JDBC drivers in a JAR file. The JDBC adapter will need to execute with this JAR file in its classpath.

On Linux, the classpath is extended by editing the file streambase-directory/bin/sb-jdbc. The classpath for the JDBC adapter is defined in this file. Include the classpath for a JDBC driver by editing the line that executes the adapter; this line looks something like exec $java -cp "$sb_all:$ad_jar" $adapter_class $@.

Similarly, the classpath used to execute the JDBC driver needs to be extended on Windows. This is accomplished by editing the file streambase-install-dir\bin\sb-jdbc.ini. The line that defines the classpath, which looks something like Class Path=streambase-install-dir\lib\sb-jdbc.jar;streambase-install-dir\lib\sbclient.jar, needs to be extended to include the vendor's JDBC JAR file.

Arguments

The adapter expects one argument: the file name of its configuration file. It also accepts:

  • -h or --help to display brief help,

  • -v or --version, to display version information, and

  • -s to display a skeleton configuration file.

  • -J to specify additional arguments to the JVM that runs sb-jdbc. See sbd in the StreamBase Command Reference for more on the -J option.

The adapter will read the configuration file, and if no errors are found, will begin executing every startup query first. Once all these have completed, the adapter will load and begin processing DML and query blocks.

Error handling

If an error occurs during the processing of any query, that query will report the error and shutdown, but all other queries will continue unaffected. If no queries remain running, the adapter shuts down.

Memory requirements

If large fetching or buffering parameters are set for configured queries, it is recommended to increase the default heap size Java allocates to the adapter. For this, add the -Xmx and -Xms JVM options to the command line. See the Java documentation for more details on these options.

Back to Top ^