K
- key typeV
- value typeR
- KeyValue
return type (both key and value type can be set
arbitrarily)public interface Transformer<K,V,R>
Transformer
interface is for stateful mapping of an input record to zero, one, or multiple new output
records (both key and value type can be altered arbitrarily).
This is a stateful record-by-record operation, i.e, transform(Object, Object)
is invoked individually for
each record of a stream and can access and modify a state that is available beyond a single call of
transform(Object, Object)
(cf. KeyValueMapper
for stateless record transformation).
Additionally, this Transformer
can schedule
a method to be called periodically
with the provided context.
Use TransformerSupplier
to provide new instances of Transformer
to Kafka Stream's runtime.
If only a record's value should be modified ValueTransformer
can be used.
Modifier and Type | Method and Description |
---|---|
void |
close()
Close this transformer and clean up any resources.
|
void |
init(ProcessorContext context)
Initialize this transformer.
|
R |
transform(K key,
V value)
Transform the record with the given key and value.
|
void init(ProcessorContext context)
close()
will be called on it; the
framework may later re-use the transformer by calling init(ProcessorContext)
again.
The provided context
can be used to access topology and record meta data, to
schedule
a method to be
called periodically
and to access attached StateStore
s.
Note, that ProcessorContext
is updated in the background with the current record's meta data.
Thus, it only contains valid record meta data when accessed within transform(Object, Object)
.
context
- the contextR transform(K key, V value)
state
that is attached
to this operator can be accessed and modified
arbitrarily (cf. ProcessorContext.getStateStore(String)
).
If only one record should be forward downstream, transform
can return a new KeyValue
. If
more than one output record should be forwarded downstream, ProcessorContext.forward(Object, Object)
and ProcessorContext.forward(Object, Object, To)
can be used.
If no record should be forwarded downstream, transform
can return null
.
Note that returning a new KeyValue
is merely for convenience. The same can be achieved by using
ProcessorContext.forward(Object, Object)
and returning null
.
key
- the key for the recordvalue
- the value for the recordKeyValue
pair—if null
no key-value pair will
be forwarded to down streamvoid close()
init(ProcessorContext)
on it again.
To generate new KeyValue
pairs ProcessorContext.forward(Object, Object)
and
ProcessorContext.forward(Object, Object, To)
can be used.