- Index

< Previous |  | Next >

# TableOperator Java Client API Sample

## Introduction

This sample illustrates the use of the TableAccessor class in the Java Client API. This class lets Java code (such as custom Java operators) interact with data sources in various ways. In particular, this class lets you insert rows, replace rows, read a single row based on the primary index, perform more complex multi-row queries, delete rows, get table size, and truncate entire tables.

This sample includes seven custom Java operators, each of which illustrates a single aspect of the TableAccessor class:

- InsertJavaOperator

- ReplaceJavaOperator

- ReadPrimaryJavaOperator

- QueryJavaOperator

- DeleteJavaOperator

- TruncateJavaOperator

- SizeJavaOperator

Code shared by these operators exists in a common abstract superclass, AbstractTableOperator. So, for example, the InsertJavaOperator.processTuple method illustrates the use of the corresponding TableAccessor.insert method.

The StreamBase application `TableOperator.sbapp` includes an instance of each of those seven Java operators, along with related input, output, and error streams, as appropriate.

To demonstrate Java operator input and output, this sample also includes a StreamBase unit test suite, `UnitTest.java`. Thus, instead of running the `TableOperator.sbapp` EventFlow application and enqueuing tuples, you just run the unit test. This starts the `TableOperator.sbapp` application, populates its Query Table, then runs each of the application's seven Java operators, and reports the results. A solid green bar in the JUnit view indicates a successful run.

## Importing This Sample into StreamBase Studio

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

- From the top-level menu, click File → Load StreamBase Sample.

- Select this application from the Applications list.

- Click OK.

StreamBase Studio creates a project for each sample.

## Running This Sample in StreamBase Studio

Follow these steps:

1. In the Project Explorer view, open the `java-src` folder tree to expose the `UnitTest.java` file.

2. Select `UnitTest.java`, right-click, and from its context menu, select Run As → StreamBase Unit Test.

   The JUnit view appears and displays the successful execution of the six tests specified in the `UnitTest.java` file.

You can then explore the sample in other ways. For example:

- Read the source of `UnitTest.java` to see samples of Java operator input and expected output.

- In the `java-src` tree, read the sources of the Java operators: `com.streambase.sample.*JavaOperator`

- Run `TableOperator.sbapp` and interact manually with the operators. Follow these steps:

  1. In the Project Explorer view, double-click to open the `TableOperator.sbapp` application. Make sure the application is the currently active tab in the EventFlow Editor.

  2. Click the Run button. This opens the SB Test/Debug perspective and starts the module.

  3. In the Manual Input view, select the `Insert` stream, and send tuples that populate the application's Query Table with values.

  4. Once you have a small set of data in the Query Table, select the other input streams one at a time to manage and manipulate the data in the Query Table.

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

## 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_TableOperator

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