Class CollectionEnDec

java.lang.Object
io.aiven.kafka.connect.amqp.common.data.EncoderDecoder
io.aiven.kafka.connect.amqp.common.data.CollectionEnDec

public final class CollectionEnDec extends EncoderDecoder
Converts collections and maps with arbitrary values into Kafka Struct types.

This Encodeer/Decoder should come after KafkaEnDec if it is used in order to allow the standard Kafka conversion to occur before the conversions here.

This Encoder/Decorder handles the following types and in the following order:

  • Arbitrary java.util.Collection implementation - converts to a Struct with fields names by the numeric index of the natural order of the collection. Values of the fields are encoded/decoded using the current EncoderDecoder instance. The struct itself is named as the class name of the collection implementation.
  • Arbitrary java.util.Map implementation - converts to a struct where each field is named by the toString representation of the key and the value is encoded/decoded using the current EncoderDecoder instance. The struct itself is named as the class name of the Map implementation.
  • Array of arbitrary objects - converts to a Struct with fields names by the numeric index of the array. Values of the array are encoded/decoded using the current EncoderDecoder instance. The struct itself is named as the class name of the array implementation.
  • Constructor Details

    • CollectionEnDec

      public CollectionEnDec()
      Constructor.
  • Method Details

    • encodeCollection

      public Optional<org.apache.kafka.connect.data.SchemaAndValue> encodeCollection(org.apache.kafka.connect.data.SchemaBuilder schemaBuilder, Collection<?> collection)
      Create a struct that contains the items from the collection.
      Parameters:
      schemaBuilder - the schema builder to add the schema to.
      collection - the collection to process.
      Returns:
      the completed schema.
    • encode

      public Optional<org.apache.kafka.connect.data.SchemaAndValue> encode(Object value)
      Description copied from class: EncoderDecoder
      Encodes the value as a SchemaAndValue. If this converter can not perform the encoding it returns an empty Optional.
      Specified by:
      encode in class EncoderDecoder
      Parameters:
      value - the value to convert.
      Returns:
      the Optional SchemaAndValue or an empty Optional.
    • decode

      public Optional<Object> decode(org.apache.kafka.connect.data.SchemaAndValue schemaAndValue)
      Description copied from class: EncoderDecoder
      Decodes a SchemaAndValue into an object. If this converter can not perform the decoding it returns an empty Optional.
      Specified by:
      decode in class EncoderDecoder
      Parameters:
      schemaAndValue - the schema and value to decode.
      Returns:
      the Optional Object or an empty Optional if not decoded.