public abstract class ForeachWriter<T>
extends Object
implements scala.Serializable
StreamingQuery
. Typically this is used to send the
generated data to external systems. Each partition will use a new deserialized instance, so you
usually should do all the initialization (e.g. opening a connection or initiating a transaction)
in the open
method.
Scala example:
datasetOfString.writeStream.foreach(new ForeachWriter[String] {
def open(partitionId: Long, version: Long): Boolean = {
// open connection
}
def process(record: String) = {
// write string to connection
}
def close(errorOrNull: Throwable): Unit = {
// close the connection
}
})
Java example:
datasetOfString.writeStream().foreach(new ForeachWriter<String>() {
@Override
public boolean open(long partitionId, long version) {
// open connection
}
@Override
public void process(String value) {
// write string to connection
}
@Override
public void close(Throwable errorOrNull) {
// close the connection
}
});
Constructor and Description |
---|
ForeachWriter() |
Modifier and Type | Method and Description |
---|---|
abstract void |
close(Throwable errorOrNull)
Called when stopping to process one partition of new data in the executor side.
|
abstract boolean |
open(long partitionId,
long version)
Called when starting to process one partition of new data in the executor.
|
abstract void |
process(T value)
Called to process the data in the executor side.
|
public abstract boolean open(long partitionId, long version)
version
is
for data deduplication when there are failures. When recovering from a failure, some data may
be generated multiple times but they will always have the same version.
If this method finds using the partitionId
and version
that this partition has already been
processed, it can return false
to skip the further data processing. However, close
still
will be called for cleaning up resources.
partitionId
- the partition id.version
- a unique id for data deduplication.true
if the corresponding partition and version id should be processed. false
indicates the partition should be skipped.public abstract void process(T value)
open
returns true
.value
- (undocumented)public abstract void close(Throwable errorOrNull)
open
returns true
or false
. However,
close
won't be called in the following cases:
- JVM crashes without throwing a Throwable
- open
throws a Throwable
.
errorOrNull
- the error thrown during processing data or null if there was no error.