Add Spring Batch Redis dependency to your POM file:
<dependency>
<groupId>com.redis</groupId>
<artifactId>spring-batch-redis-core</artifactId>
<version>4.3.2</version>
</dependency>
RedisItemReader<K,V,T>
and RedisItemWriter<K,V,T>
support a single data type called KeyValue
that has the following fields:
* key
of type K
representing the Redis key.
* type
of type String
representing the type of that Redis key (string
, hash
, list
, …).
* ttl
of type long
representing the key’s absolute expiration epoch time in milliseconds.
* value
of type:
byte[]
for dump
reader.
Object
for struct
reader. The type of the object depends on the type of the Redis key (V
for string
, Map<K,V>
for hash
, List<V>
for list
, …). See Data Structures below for more details.
* mem
of type long
representing the memory usage of that key in Redis. This is only populated when memUsageLimit
on MemKeyValueRead
operation is strictly greater than 0.
Values for struct
readers RedisItemReader<K,V,T>
are Java object representations of the underlying Redis data-structures:
-
hash
:java.util.Map<K,V>
-
list
:java.util.List<V>
-
set
:java.util.Set<V>
-
stream
:java.util.List<io.lettuce.core.StreamMessage<K,V>>
-
string
:V
-
zset
:java.util.Set<io.lettuce.core.ScoredValue<V>>
-
json
:V
-
timeseries
:java.util.List<com.redis.lettucemod.timeseries.Sample>>
These generic classes <K,V>
depend on the RedisCodec
used to initialize the reader:
* StringCodec
: java.lang.String
* ByteArrayCodec
: byte[]
RedisItemReader
exposes 2 operation modes:
-
Snapshot: relies on SCAN command to iterate over the keys whose values will be read to produce key/values.
-
Live (AKA continuous): listens to changes in the keyspace via notifications and produces corresponding key/values.
RedisItemWriter
can perform both inserts or deletes depending on the value and TTL in the incoming object.
If value is null or TTL is -2 then the DEL
command is called, otherwise a write is performed.
Item writers support two different payload (value) types:
The writer accepts key dumps (KeyValue
with a byte[]
value) and calls the RESTORE command with the byte array and TTL if any.
Refer to unit tests for usage examples.