如果您将输入写入 Redis 协议格式,那么为什么不只使用 redis-cli 或 nc 的管道模式呢?这是从http://redis.io/topics/mass-insert解释的。
如果您有大量(键、值)输入,那么您可以使用 Jedis 通过管道执行 Sadd 以获得更高的性能。
下面的例子假设iter(迭代器)有元素,每一项都是key"\t"value形式。
try (Jedis jedis = new Jedis(host, port)) {
Pipeline pipeline = jedis.pipelined();
while (iter.hasNext()) {
String[] keyValue = iter.next().split("\t");
pipeline.sadd(keyValue[0], keyValue[1]);
// you can call pipeline.sync() and start new pipeline here if you think there're so much operations in one pipeline
}
pipeline.sync();
}
Run Code Online (Sandbox Code Playgroud)