2 Using the StreamOperation Class
A streams application works by implementing Subscribers. Subscribers receive a stream of events that consist of write operations to a table of interest.
For more information, see Implementing Subscribers.
Every event your application receives in the subscription stream is represented as an oracle.kv.pubsub.StreamOperation
. Each of these events represents either a put
or delete
operation on the table that your application subscribes to.
For more information, see oracle.kv.pubsub.StreamOperation class summary in the — Java Direct Driver API Reference
The StreamOperation
interface provides the following methods:
-
StreamOperation.getType()
Returns a
StreamOperation.Type
object. This is an enum constant that is eitherdelete
orput
. For example:// so is a StreamOperation object. It is obtained using // NoSQLSubscriber.onNext(). switch (so.getType()) { case PUT: { // Process the put operation here. } break; case DELETE: { // Process the delete operation here. } break; default: // Received an unknown and therefore illegal operation type. throw new IllegalStateException("... exception message ..."); }
-
StreamOperation.asDelete()
Returns the operation as a
StreamOperation.DeleteEvent
object. The object contains only the Primary Key associated with thedelete
operation:// so is a StreamOperation object. It is obtained using // NoSQLSubscriber.onNext(). StreamOperation.DeleteEvent de = so.asDelete(); PrimaryKey pk = de.getPrimaryKey();
-
StreamOperation.asPut()
Returns the operation as a
StreamOperation.PutEvent
object. This object allows you to obtain the row that was changed by theput
operation. Be aware that the row returned here represents the state of the row after theput
operation has been performed:// so is a StreamOperation object. It is obtained using // NoSQLSubscriber.onNext(). StreamOperation.PutEvent pe = so.asPut(); Row row = pe.getRow();
-
StreamOperation.getRepGroupId()
Returns the Shard ID (as an
int
) where thiswrite
operation was performed. -
StreamOperation.getSequenceId()
Returns the unique sequence ID associated with this operation. This ID uniquely identifies a stream operation associated with a given Publisher.
These IDs can be used to sequence operations seen for a given key. The Subscription API guarantees that the order of events for a particular key is the same as the order in which these operations were applied in Oracle NoSQL Database. The subscription API provides no guarantees about the order of operations beyond the single key.