KNet for OPC (KNetOPC): Introduction to predicates and transformations
Overview
Predicates are conditions evaluated on each OPC-UA change notification record by the Kafka Connect framework. A predicate does not modify or discard records by itself: it is attached to a transformation, which is applied to a record only when the predicate evaluates to true for that record (or to false, when the transformation is configured with negate=true). Combined with the standard Kafka Connect Filter transformation, predicates discard records before they are written to an Apache Kafka™ topic; combined with other transformations, they apply them to a subset of the records only, for example to route them to a different topic or to change their encoding type.
KNetOPC predicates extend the Apache Kafka™ Connect predicate mechanism, which is a standard feature of the Kafka Connect framework. They integrate natively with KNet Connect SDK and follow the same configuration and activation model used by any Kafka Connect predicate.
The same assembly also hosts general-purpose transformations: processing steps applied to the records, optionally gated by a predicate, not tied to KEFCore (for the KEFCore transformations see Introduction to KEFCore). The currently available one is KNetOPCTransformationEncodingTypeChange, which converts the records to a different OPC-UA encoding type; see Transformations.
Architecture
KNetOPC predicates have a two-layer architecture:
- Java layer: activates the predicate within the Apache Kafka™ Connect runtime and exposes the configuration to the connector properties file. Each predicate is identified by its fully-qualified Java class name (e.g.
org.mases.knetopc.transformations.predicates.KNetOPCPredicateGood). - .NET layer: contains the actual evaluation logic. The Java layer delegates execution to the corresponding .NET class via KNet Connect SDK. The .NET assembly involved is
MASES.KNet.OPC.TransformationsPredicates.
This design keeps the OPC-UA–specific logic in .NET while staying fully compatible with the standard Kafka Connect predicate infrastructure.
The transformations hosted in the same assembly follow the same architecture: a Java class (e.g. org.mases.knetopc.transformations.KNetOPCTransformationEncodingTypeChange) activates the transformation within the Apache Kafka™ Connect runtime and delegates the record conversion to the corresponding .NET class.
Performance and runtime requirements
KNetOPC is built on JCOBridge HPA (High Performance Application). Predicates use its advanced features, in particular JCOBridgeStream, to work on the JVM memory directly instead of copying it into the CLR. The result depends on the encoding type configured in the KNetOPC Source connector (EncodingType) and, for some predicates, on the .NET runtime.
KNetOPCPredicateGood, KNetOPCPredicateBad, KNetOPCPredicateNotOldThan
| Encoding type | .NET 8 and .NET 10 |
|---|---|
Binary |
Full HPA: the record is evaluated through views directly over the JVM memory, without copying it. |
Xml, Json |
The JVM memory is copied and the record is fully decoded. |
KNetOPCPredicateValueCompare, KNetOPCPredicateBuiltIn, KNetOPCPredicateBoolean
These predicates evaluate the node value and the runtime makes the difference:
| Encoding type | .NET 10 | Other supported runtimes (e.g. .NET 8) |
|---|---|---|
Binary |
Full HPA: the record is evaluated through views directly over the JVM memory, without copying it. | The JVM memory is read as a stream, without copying it, but the type is materialized during the decoding. |
Xml, Json |
The JVM memory is copied and the record is fully decoded. | The JVM memory is copied and the record is fully decoded. |
To take full advantage of HPA set EncodingType to 0 (Binary) in the KafkaConnectParameters of the Source connector configuration, since the default is 2 (Json), and use the Binary variant of the connector properties file (ByteArrayConverter); for the second group of predicates use also .NET 10, which is the only runtime providing what is needed for the full optimization.
Fallback to the full decoder. Setting always.use.full.decoder=true on a predicate or on a transformation disables the use of the JVM memory (the views of the predicates and the stream-based decoding of the transformations): the record is always decoded with the official full decoder, whatever the encoding type and the .NET runtime. This is also what happens on .NET Framework, where the full decoder is always used. It is a safety net: if an issue is suspected in the handling of the memory views, the official decoder can always be used, at the cost of the optimizations described above. For transformations (as for the Sink connector and the clients) the optimization is not a view in the strict sense: the JVM memory is exposed as a stream consumed by the binary decoder, without moving it to the CLR, while the resulting object is always fully created on the CLR side.
predicates.IsGood.always.use.full.decoder=true
The optimizations above apply to predicates. KNetOPCTransformationEncodingTypeChange works on the decoded record and produces a new value encoded with the configured encoding type. A predicate is evaluated on the record as it is at the point of the transforms chain where the transformation it is attached to runs, so the typical usage is to keep the Binary encoding type in the Source connector, so that predicates take advantage of HPA, apply the standard Kafka Connect transformations that use them (for example to split the records into two topics) and place this transformation after them, to write the records of a topic in Xml or Json so that they can be read easily.
How predicates are applied
KNetOPC predicates follow the standard Kafka Connect model: a predicate is declared with the predicates key and attached to a transformation with transforms.<alias>.predicate. For each record the transformations are evaluated in the order in which they are listed in transforms:
OPC-UA Server
└─► KNet Connect SDK receives change notification
└─► Transformation 1 (applied only if its predicate, when set, evaluates to true)
└─► Transformation 2 (applied only if its predicate, when set, evaluates to true)
└─► Record published to Apache Kafka™ topic
- A transformation without a predicate is applied to all the records.
- A transformation with a predicate is applied only to the records for which the predicate evaluates to
true. Settransforms.<alias>.negate=trueto invert the result, so that the transformation is applied to the records for which the predicate evaluates tofalse. - The standard
Filtertransformation drops the records it is applied to. To discard records use it with a predicate: withoutnegateit drops the records matching the predicate, withnegate=trueit keeps only the records matching the predicate. - A predicate that is declared but not attached to any transformation has no effect.
In the rest of the documentation a record matches a predicate when the predicate evaluates to true for it.
Configuration
Predicates are configured in the connector properties file using the standard Kafka Connect predicate syntax. The predicates key lists one or more predicate aliases; each alias is then configured with its own predicates.<alias>.* properties and attached to a transformation with transforms.<alias>.predicate=<predicateAlias> and, optionally, transforms.<alias>.negate=true.
Common configuration properties
All KNetOPC predicates and transformations share the following base properties, in addition to any specific ones:
| Property | Type | Required | Default | Description |
|---|---|---|---|---|
application.name |
string | No | "" |
Application name used in logging and telemetry. |
application.version |
string | No | "" |
Application version used in logging and telemetry. |
always.use.full.decoder |
boolean | No | false |
When true, the use of the JVM memory (views or stream) is disabled and the record is always decoded with the full (official) decoder, as it happens on .NET Framework. It is a fallback to use if an issue is suspected in the memory views handling; see Performance and runtime requirements. |
Example: single predicate
The following example configures the connector to publish only records with a Good OPC-UA status code. The Filter transformation drops the records matching its predicate, so negate=true is used to drop the ones that do not match, i.e. the records that are not Good:
predicates=IsGood
predicates.IsGood.type=org.mases.knetopc.transformations.predicates.KNetOPCPredicateGood
predicates.IsGood.application.name=MyApp
predicates.IsGood.application.version=1.0.0
transforms=KeepGood
transforms.KeepGood.type=org.apache.kafka.connect.transforms.Filter
transforms.KeepGood.predicate=IsGood
transforms.KeepGood.negate=true
Example: multiple predicates with negation
Each transformation can be attached to its own predicate, with or without negate. The following example discards records with a Bad status code and records older than 60 seconds:
predicates=IsBad,IsFresh
predicates.IsBad.type=org.mases.knetopc.transformations.predicates.KNetOPCPredicateBad
predicates.IsFresh.type=org.mases.knetopc.transformations.predicates.KNetOPCPredicateNotOldThan
predicates.IsFresh.delta.time.in.seconds=60
transforms=DropBad,DropOld
transforms.DropBad.type=org.apache.kafka.connect.transforms.Filter
transforms.DropBad.predicate=IsBad
transforms.DropOld.type=org.apache.kafka.connect.transforms.Filter
transforms.DropOld.predicate=IsFresh
transforms.DropOld.negate=true
Note
negate is a property of the transformation, not of the predicate: when set to true, the predicate result is inverted. In the example above DropBad drops the records matching IsBad, while DropOld drops the records for which IsFresh is false, i.e. the ones older than 60 seconds.
Available predicates
The following predicates are available out of the box. For the full configuration reference of each predicate see Available predicates.
| Class name | Description |
|---|---|
KNetOPCPredicateGood |
Matches records whose OPC-UA StatusCode is Good. |
KNetOPCPredicateBad |
Matches records whose OPC-UA StatusCode is Bad. |
KNetOPCPredicateBoolean |
Matches records whose node value (Boolean type) is equal to a configured value. |
KNetOPCPredicateBuiltIn |
Matches records whose BuiltInType value is one of the configured values. |
KNetOPCPredicateNotOldThan |
Matches records whose timestamp is not older than a configured number of seconds. |
KNetOPCPredicateValueCompare |
Matches records whose scalar node value satisfies a comparison condition against a configured threshold. |
Transformations
Transformations are configured in the connector properties file using the standard Kafka Connect transformation syntax: the transforms key lists one or more transformation aliases, and each alias is configured with its own transforms.<alias>.* properties. As in any Kafka Connect pipeline, they are applied in the order in which they are listed in transforms, and each one can be attached to a predicate as described in How predicates are applied.
Available transformations
The following transformations are available out of the box. For the full configuration reference see Available predicates and transformations.
| Class name | Description |
|---|---|
KNetOPCTransformationEncodingTypeChange |
Converts the records to a different OPC-UA encoding type (Binary, Xml or Json). |
Example: predicate and transformation
The following example keeps only the records with a Good OPC-UA status code and writes them as JSON. The KeepGood transformation is the Filter of the previous examples; it is followed by the encoding conversion:
predicates=IsGood
predicates.IsGood.type=org.mases.knetopc.transformations.predicates.KNetOPCPredicateGood
transforms=KeepGood,ToJson
transforms.KeepGood.type=org.apache.kafka.connect.transforms.Filter
transforms.KeepGood.predicate=IsGood
transforms.KeepGood.negate=true
transforms.ToJson.type=org.mases.knetopc.transformations.KNetOPCTransformationEncodingTypeChange
transforms.ToJson.encoding.type.value=Json