如何使用JAVA在Redis中进行批量插入?

mar*_*308 5 java redis jedis

您好,我需要多次插入表单

SADD键值

我有键值对,需要知道如何使用 JAVA 执行批量插入。我在Redis协议中写了一个文件。如何进一步进行

Jun*_*Lim 8

如果您将输入写入 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)