Reputation: 31
I am relatively new to redis . I am designing an rest API, to get the entire data set in the redis db.
I used the below code to get all the keys
public Map getAllRedisData() {
Map<Object, Object> map = new HashMap<>();
Set<byte[]> keys = redisService.getTemplate().getConnectionFactory().getConnection().keys("*".getBytes());
Iterator<byte[]> it = keys.iterator();
while(it.hasNext()){
byte[] data = (byte[])it.next();
// String key = new String(data,0,data.length);
Object key = null;
try {
key = getObjectFromByteArray(data);
System.out.println(key);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return map;
}
private Object getObjectFromByteArray(byte[] bytes) throws IOException, ClassNotFoundException
{
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInput in = new ObjectInputStream(bis)) {
return in.readObject();
}
}
I was able to get all the keys, but wasnt able to retrieve the value for all the keys. Below is the approach "Object value = redisService.getTemplate().opsForValue().get("");"
I guess this is because some keys are of different datatypes.
Let me know if I have to modify/change my approach.
Upvotes: 1
Views: 1961
Reputation: 1454
First keys should not be used in production. Instead you can use scan command.
When you have the key you can call the redis type command, to retrieve the DataType for the object.
DataType -> NONE, STRING, LIST, SET, ZSET, HASH
Then you can call the right function to get the data
On large data set this could take a while. Are you sure that you rest api need to get all data into the redis? You can use a pattern with scan to only retrieve the data you really need.
Upvotes: 1
Reputation: 587
i didn't test it, but try this
try {
Object value = getObjectFromByteArray(redisService.getTemplate().getConnectionFactory().getConnection().get(data));
key = getObjectFromByteArray(data);
System.out.println(key);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Upvotes: 0