Redis 批量插入 [英] Redis Mass Insertion

查看:60
本文介绍了Redis 批量插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑使用 Redis 的协议进行批量插入,如下所述:http://redis.io/topics/mass-insert 在我忙着编写代码来处理这个问题之前,我只想确保我清楚 Redis 需要什么来完成这项工作.

I am considering using Redis' protocol to do mass insertions as described here: http://redis.io/topics/mass-insert Before I get busy write code to handle this I just want to be sure that I am clear on what is required by Redis to make this work.

上面的链接表明,要使用批量插入调用 SET 操作 [SET myKey Value myValue],我需要创建一个命令,该命令可以在文件中的多行或单引号字符串中完成.

The above link suggests that to call the SET operation [SET myKey Value myValue] using mass insertion I need to create a command that can be done in either several lines in a file or a single quoted string.

假设我不想使用 SET 命令,而是想使用 SADD 命令添加到集合中.我这里的内容对带引号的字符串格式有效吗?

Assuming I don't want to use the SET command instead I want to use the SADD command to add to a set. Is what I have here valid for a quoted string format?

"*4\r\n$4\r\nSADD\r\n$2\r\n80\r\n$5\r\n1,2,34\r\n"

"*4\r\n$4\r\nSADD\r\n$2\r\n80\r\n$5\r\n1,2,34\r\n"

本质上我存储的是一个键:80,其值为 1,2,34

Essentially what I am storing is a key: 80 whose value is 1,2,34

最后我想要的功能是这样的:

In the end what I want the capability to do is this:

Key     Value
80      1,2,34
90      4,8,34

获取两个集合的交集(SINTER)和/或并集(SUNION).即 SINTER = 34 或 SUNION = 1,2,4,8,34

Get the intersection (SINTER) and/or union (SUNION) of the two sets. i.e. SINTER = 34 or SUNION = 1,2,4,8,34

感谢您提供的任何有用信息.我只是想确保我走在正确的道路上.

Any helpful information you can provide is appreciated. I just want to make sure I'm on the right path this.

推荐答案

我会说你正走在过早优化的道路上(这通常不是最好的).

I would say you are on the path of premature optimization (which is usually not the best one).

任何带有支持流水线的 Redis 客户端的脚本语言都应该能够将至少 50K 命令/秒推送到 Redis 服务器.代码将很简单,无需手动编码 Redis 协议.诚然,大规模插入技巧更快,但您真的需要它吗?

Any scripting language with a Redis client supporting pipelining should be able to push at least 50K commands/s to the Redis server. The code will be straightforward, with no need to manually encode the Redis protocol. Granted, the massive insert trick is faster, but do you really need it?

现在,如果你仍然想坚持大量插入,你需要编码一个合适的Redis命令.由于以下几个原因,您提供的示例是错误的:

Now, if you still want to stick to massive insert, you need to encode a proper Redis command. The example you provided is wrong for several reasons:

  • 参数个数错误(你的例子应该以*3开头)

  • the number of arguments is wrong (your example should start by *3)

最后一个参数的长度是错误的(1,2,34 长度是 6 个字节而不是 5 个).

the length of the last argument is wrong (1,2,34 length is 6 bytes not 5).

在 SADD 命令中,集合中的每个项目都需要一个参数(即对于 Redis,1、2、34 将是单个项目,而不是三个).

in the SADD command, you need one argument per item of the set (i.e. for Redis 1,2,34 will be a single item, not three).

正确的命令应该是这样的:

The proper command would rather be something like this:

"*5\r\n$4\r\nSADD\r\n$2\r\n80\r\n$1\r\n1\r\n$1\r\n2\r\n$2\r\n34\r\n"

Redis 协议描述如下:http://redis.io/topics/protocol

The Redis protocol is described here: http://redis.io/topics/protocol

这篇关于Redis 批量插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆