Class KeyValueItemWriter<K,V>

java.lang.Object
org.springframework.batch.infrastructure.item.KeyValueItemWriter<K,V>
All Implemented Interfaces:
ItemWriter<V>, org.springframework.beans.factory.InitializingBean
Direct Known Subclasses:
KafkaItemWriter, RedisItemWriter

public abstract class KeyValueItemWriter<K,V> extends Object implements ItemWriter<V>, org.springframework.beans.factory.InitializingBean
A base class to implement any ItemWriter that writes to a key value store using a Converter to derive a key from an item. If a derived key is null, the item will be skipped and a warning logged.
Since:
2.2
Author:
David Turanski, Mahmoud Ben Hassine, Stefano Cordio
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected boolean
     
    protected org.springframework.core.convert.converter.Converter<V,K>
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    KeyValueItemWriter(org.springframework.core.convert.converter.Converter<V,K> itemKeyMapper)
    Create a new KeyValueItemWriter instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    protected void
    Flush items to the key/value store.
    protected abstract void
    afterPropertiesSet() hook
    void
    setDelete(boolean delete)
    Sets the delete flag to have the item writer perform deletes
    void
    setItemKeyMapper(org.springframework.core.convert.converter.Converter<V,K> itemKeyMapper)
    Set the Converter to use to derive the key from the item
    void
    write(Chunk<? extends V> chunk)
    Process the supplied data element.
    protected abstract void
    writeKeyValue(K key, V value)
    Subclasses implement this method to write each item to key value store

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • itemKeyMapper

      protected org.springframework.core.convert.converter.Converter<V,K> itemKeyMapper
    • delete

      protected boolean delete
  • Constructor Details

    • KeyValueItemWriter

      public KeyValueItemWriter(org.springframework.core.convert.converter.Converter<V,K> itemKeyMapper)
      Create a new KeyValueItemWriter instance.
      Parameters:
      itemKeyMapper - the Converter used to derive a key from an item.
      Since:
      6.0
  • Method Details

    • write

      public void write(Chunk<? extends V> chunk) throws Exception
      Description copied from interface: ItemWriter
      Process the supplied data element. Will not be called with any null items in normal operation but might be called with an empty chunk, for example when all items have been filtered by an ItemProcessor or skipped by the fault-tolerant step processing. Implementations are expected to handle empty chunks gracefully.
      Specified by:
      write in interface ItemWriter<K>
      Parameters:
      chunk - of items to be written. Never null but may be empty.
      Throws:
      Exception - if there are errors. The framework will catch the exception and convert or rethrow it as appropriate.
    • flush

      protected void flush() throws Exception
      Flush items to the key/value store.
      Throws:
      Exception - if unable to flush items
    • writeKeyValue

      protected abstract void writeKeyValue(K key, V value)
      Subclasses implement this method to write each item to key value store
      Parameters:
      key - the key
      value - the item
    • init

      protected abstract void init()
      afterPropertiesSet() hook
    • setItemKeyMapper

      public void setItemKeyMapper(org.springframework.core.convert.converter.Converter<V,K> itemKeyMapper)
      Set the Converter to use to derive the key from the item
      Parameters:
      itemKeyMapper - the Converter used to derive a key from an item.
    • setDelete

      public void setDelete(boolean delete)
      Sets the delete flag to have the item writer perform deletes
      Parameters:
      delete - if true ItemWriter will perform deletes, if false not to perform deletes.
    • afterPropertiesSet

      public void afterPropertiesSet() throws Exception
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
      Throws:
      Exception