java.lang.Object
io.aiven.commons.kafka.connector.source.task.Context

public class Context extends Object
The context for a source record.

The context contains information necessary for processing the record. By default, it contains the topic, partition, offset and native key for the `Native Object` being processed.

The context stores named values. Additional values may be added to the context. value names should be namespaced to avoid collisions. For example the default topic value is named "io.aiven.commons.kafka.connector.source.task.Context#Topic".

Contexts are intended to be immutable and constructed using the Builder pattern. New Context implementations should simply provide easy retrieval of new named values.

New Builder implementations should extend the Context.Builder class and add setters and validators for the new named values.

  • Field Details

    • TOPIC_KEY

      public static final String TOPIC_KEY
      The key for the topic value.
    • PARTITION_KEY

      public static final String PARTITION_KEY
      the key for the partition value.
    • OFFSET_KEY

      public static final String OFFSET_KEY
      the key for the offset value.
    • NATIVE_KEY

      public static final String NATIVE_KEY
      The key for the native key value.
  • Constructor Details

    • Context

      public Context(Context.Builder<?> builder)
      Create a context from the properties.
      Parameters:
      builder - The builder to provide the properties.
  • Method Details

    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • builder

      public static Context.Builder<?> builder(Comparable<?> primaryKey)
      Creates a builder for a default Context.
      Parameters:
      primaryKey - the primary key.
      Returns:
      a builder
    • builder

      public Context.Builder<?> builder()
      Creates a builder from this context. Note: Implementations that extend the base context should override this method to return a builder for the specific context implementation.

      The returned context builder should, by default and without modification, create a new context that has the same properties as this context.

      Returns:
      a builder for this Context.
    • getObject

      public <T> Optional<T> getObject(String key, Function<Object,T> fn)
      Gets an arbitrary object from the context.
      Type Parameters:
      T - the object type to return.
      Parameters:
      key - the key for the object.
      fn - a function to convert the Object into a the desired return type.
      Returns:
      an Optional containing a T type or an empty optional.
    • getObject

      public Optional<Object> getObject(String key)
      Gets an optional object from the context.
      Parameters:
      key - the key to get.
      Returns:
      the optional object or an empty optionl if it was not present.
    • getString

      public Optional<String> getString(String key)
      Gets an optional string from the context.
      Parameters:
      key - the key to get.
      Returns:
      the optional string or an empty optional if it was not present.
    • getNumber

      public Optional<Number> getNumber(String key)
      Gets an optional Number from the context.
      Parameters:
      key - the key to get.
      Returns:
      the optional Number or an empty optional if it was not present.
    • getInteger

      public Optional<Integer> getInteger(String key)
      Gets an optional Integer from the context.
      Parameters:
      key - the key to get.
      Returns:
      the optional Integer or an empty optional if it was not present.
    • getLong

      public Optional<Long> getLong(String key)
      Gets an optional Long from the context.
      Parameters:
      key - the key to get.
      Returns:
      the optional Long or an empty optional if it was not present.
    • getShort

      public Optional<Short> getShort(String key)
      Gets an optional Short from the context.
      Parameters:
      key - the key to get.
      Returns:
      the optional Short or an empty optional if it was not present.
    • getTopic

      public Optional<String> getTopic()
      Gets the Kafka topic as specified by the context.
      Returns:
      an Optional kafka topic.
    • getPartition

      public Optional<Integer> getPartition()
      Gets the Kafka partition as specified by the context.
      Returns:
      an Optional kafka partition.
    • getNativeKey

      public <T extends Comparable<T>> T getNativeKey()
      Get the native key as specified by this context.
      Type Parameters:
      T - the returned native key type.
      Returns:
      the Optional storage key for the native object this context is associated with.
    • getOffset

      public Optional<Long> getOffset()
      Gets the native offset for the associated data. This may be the number of bytes into the native stream that the associated data starts at, or it may be the number of lines into a text file. The offset definition is dependant upon the native data structure.
      Returns:
      an optional native offset for this context.