Class RedisStreamHelper
- All Implemented Interfaces:
RedisHelperInt
This class provides convenient methods for storing and retrieving JSON records in Redis STREAMs. Unlike the native Redis STREAM API, which works with field-value pairs, this helper exposes a JSON-based interface similar to the other Redis helpers in this library.
When adding a record, the provided JSON object is automatically converted into STREAM fields, where each top-level JSON property becomes a field-value pair. When reading records, STREAM fields are reconstructed into JSON strings.
Example:
Input JSON:
{
"id": 1,
"name": "Alex",
"active": true
}
Stored STREAM entry:
1723456789012-0
id -> "1"
name -> "Alex"
active -> "true"
Returned JSON:
{
"id":"1",
"name":"Alex",
"active":"true"
}
Notes:
- Only flat JSON objects are supported.
- Each top-level JSON property becomes a Redis STREAM field.
- All field values are stored as Redis strings. Primitive JSON values such as numbers and booleans are converted to their string representation.
- Nested JSON objects and arrays are not supported.
- Redis STREAM entry IDs are generated automatically.
- TTL is applied to the entire STREAM key, not individual entries.
- This helper uses
JedisPoolinternally to manage Redis connections.
- See Also:
-
JedisJedisPool
-
Field Summary
Fields inherited from class net.bugreaper.modules.redis.helpers.RedisHelper
awaitMs, jedisPool, SCAN_BATCH_SIZE -
Constructor Summary
ConstructorsConstructorDescriptionRedisStreamHelper(String host, int port, String username, String password) This constructor initializes client for interaction with Redis STREAM data types -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a JSON record to the specified Redis STREAM.voidAdds a JSON record to the specified Redis STREAM.longReturns the number of records stored in the specified Redis STREAM.net.bugreaper.core.assertable.AssertableStringListgrabRecordsByKey(String key) Returns all records stored in the specified Redis STREAM.Methods inherited from class net.bugreaper.modules.redis.helpers.RedisHelper
attachAndLogList, checkKeyExists, checkKeyTypeIs, cleanAll, deleteKeysByPattern, execute, executeReturn, getConfigSummary, getKeyExistsStatus, getKeysCountByPattern, getKeyType, getPrefix, getTtlByKey, seeKeyExists, seeKeysCountByPatternIsExactly, seeKeysCountByPatternIsGreaterThan, seeKeysCountByPatternIsLessThan, seeKeyTypeIs, seeRecordsCountByKeyIsExactly, seeTtlIsGreaterThan, seeTtlIsLessThan, setAwaitMsMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface net.bugreaper.modules.redis.interfaces.RedisHelperInt
cleanAll, deleteKeysByPattern, getKeyExistsStatus, getKeysCountByPattern, getKeyType, getTtlByKey, seeKeyExists, seeKeysCountByPatternIsExactly, seeKeysCountByPatternIsGreaterThan, seeKeysCountByPatternIsLessThan, seeKeyTypeIs, seeRecordsCountByKeyIsExactly, seeTtlIsGreaterThan, seeTtlIsLessThan
-
Constructor Details
-
RedisStreamHelper
This constructor initializes client for interaction with Redis STREAM data types- Parameters:
host- host of Redis"localhost"port- port of Redisusername- admin usernamepassword- admin password
-
-
Method Details
-
addRecord
Adds a JSON record to the specified Redis STREAM.The provided JSON object is validated and converted into Redis STREAM fields.
Each top-level JSON property becomes a STREAM field and its value is stored as a Redis string.
Example:
Input JSON: addRecord("users:admins", """ {"id": 1, "name": "Alex", "active": true}"""); Stored STREAM entry: 1723456789012-0 id -> "1" name -> "Alex" active -> "true"- Specified by:
addRecordin interfaceRedisHelperInt- Parameters:
key- Redis STREAM keyjsonValue- JSON record to add- Throws:
net.bugreaper.core.exceptions.JsonMappersException- if the provided JSON is invalid
-
addRecord
@Step("(Redis) Insert record into key <{key}> with ttl: <{ttlSeconds}>") public void addRecord(String key, String jsonValue, long ttlSeconds) Adds a JSON record to the specified Redis STREAM.The provided JSON object is validated and converted into Redis STREAM fields, and sets its time-to-live (TTL)
Each top-level JSON property becomes a STREAM field and its value is stored as a Redis string.
Example:
Input JSON: addRecord("users:admins", """ {"id": 1, "name": "Alex", "active": true}""", 600); Stored STREAM entry: 1723456789012-0 id -> "1" name -> "Alex" active -> "true"- Specified by:
addRecordin interfaceRedisHelperInt- Parameters:
key- Redis STREAM keyjsonValue- JSON record to addttlSeconds- expiration time in seconds- Throws:
net.bugreaper.core.exceptions.JsonMappersException- if the provided JSON is invalid
-
grabRecordsByKey
@Step("(Redis) Grab records from key: {key}") public net.bugreaper.core.assertable.AssertableStringList grabRecordsByKey(String key) Returns all records stored in the specified Redis STREAM.Uses await until the key exists.
Each STREAM entry is reconstructed into a JSON string by converting its field-value pairs back into a JSON object. Records are returned in STREAM insertion order.
Because Redis STREAM fields store string values only, all JSON property values in the returned records are represented as JSON strings, even if the original JSON contained numbers, booleans, or other primitive types.
Examples:
users:admins 1723456789012-0 name -> Alex age -> 30 1723456789050-0 name -> John age -> 25Returned result:
[ {"name":"Alex","age":"30"}, {"name":"John","age":"25"} ]grabRecordsByKey("users:admins") .seeListAnyContainsJson(""" {"name": "Alex"}""");- Specified by:
grabRecordsByKeyin interfaceRedisHelperInt- Parameters:
key- Redis STREAM key- Returns:
AssertableStringListlist of stored JSON records- Throws:
AssertionError- if the key does not exist or not a Redis STREAM
-
getRecordsCountByKey
Returns the number of records stored in the specified Redis STREAM.This method uses Redis
XLENcommand.Example:
users:admins ├── 1723456789012-0 -> {"data": "record1"} ├── 1723456789050-0 -> {"data": "record2"} getRecordsCountByKey("users:admins") returns 2- Specified by:
getRecordsCountByKeyin interfaceRedisHelperInt- Parameters:
key- Redis STREAM key- Returns:
- number of records in the STREAM
-