twb
twb

Reputation: 1368

how to use jedis for mass insertion of commands

If I have create a file containing the following commands in the Redis protocol format:

SET Key0 Value0
SET Key1 Value1
...
SET KeyN ValueN

how can I use Jedis in my application to feed it to Redis?

Upvotes: 3

Views: 5409

Answers (2)

Tw Bert
Tw Bert

Reputation: 3809

Edit

The OP is looking for a solution to be run from Tomcat, I found out later. Using the raw Redis Protocol is not recommended here. Use Pascal's answer.

/Edit

I agree with Pascal that pipelining is a good way for almost-fastest mass insertion. However, the OP is already talking about the Redis Protocol, which is even faster than pipelining.

It is however, not meant to be used from a Redis Client (like Jedis), but meant to be used directly on the server, in conjunction with redis-cli.

See here for more information.

Hope this helps, TW

Upvotes: 0

Pascal Le Merrer
Pascal Le Merrer

Reputation: 5981

You can use pipelining. It allows to send multiple commands without waiting for individual responses, and get a unique response then. You will achieve better performance with this feature (you can expect x5 compared to a basic implementation).

Your implementation could look like this:

Pipeline p = jedis.pipelined();
for (int i=0; i < numberOfItems; i++) {
    p.set(key[i], value[i]); 
}   
List<Object> results = p.syncAndReturnAll();

Upvotes: 7

Related Questions