Redis 哈希存储,使用换行作为键值。

huangapple go评论62阅读模式
英文:

Redis Hashes store with new line key value

问题

I want to store data in Redis Hashes. Data is as below (Key = Value):

30.2.25=REF_IP
30.2.24=MY_HOST_IP
30.2.32=PEER_IP
30.2.32=IM_USER_MY_HOST
30.2.2=23992

Easy way to store this info in redis is below :

hmset info 30.2.25 REF_IP 30.2.24 MY_HOST_IP 30.2.32 PEER_IP 30.2.32 IM_USER_MY_HOST 30.2.2 23992

Considering I have 1000's key value and want to change few (actually so many) values in one go so searching and editing value in above command is too painful.

i want some way to execute command in below manner, that is nice formatted command with new line after every key value :

hmset info
30.2.25 REF_IP
30.2.24 MY_HOST_IP
30.2.32 PEER_IP
30.2.32 IM_USER_MY_HOST
30.2.2 23992

Is it possible to do so ?

Currently when i copy above formatted command and paste, it ignore text after new line and giving below error which is obvious because argument is wrong due to new line.

hmset info
(error) ERR wrong number of arguments for 'hmset' command

Can anyone help please. Thanks.

英文:

I want to store data in Redis Hashes. Data is as below (Key = Value):

30.2.25=REF_IP
30.2.24=MY_HOST_IP
30.2.32=PEER_IP
30.2.32=IM_USER_MY_HOST
30.2.2=23992

Easy way to store this info in redis is below :

hmset info 30.2.25 REF_IP 30.2.24 MY_HOST_IP 30.2.32 PEER_IP 30.2.32 IM_USER_MY_HOST 30.2.2 23992 

Considering I have 1000's key value and want to change few (actually so many) values in one go so searching and editing value in above command is too painful.

i want some way to execute command in below manner, that is nice formatted command with new line after every key value :

hmset info
30.2.25 REF_IP
30.2.24 MY_HOST_IP
30.2.32 PEER_IP
30.2.32 IM_USER_MY_HOST
30.2.2 23992

Is it possible to do so ?

Currently when i copy above formatted command and paste, it ignore test after new line and giving below error which is obvious because argument is wrong due to new line.

hmset info
(error) ERR wrong number of arguments for 'hmset' command

Can anyone help please. Thanks.

答案1

得分: 0

假设你在谈论使用redis-cli,目前还没有支持这个功能的方法。这个问题在GitHub上有一个开放的问题。请查看https://github.com/antirez/redis/issues/3474

根据Redis 4.0.0的版本,HMSET被视为不推荐使用。你应该使用HSET代替。https://redis.io/commands/hset

如果你想要确保所有的HSET操作同时完成,并且仍然一行一行输入它们,你可以使用一个事务。

MULTI
HSET info 30.2.25 REF_IP
HSET info 30.2.24 MY_HOST_IP
...
EXEC

这些命令将逐行发送到服务器,但它们会被排队,只有在EXEC命令时才会执行。

你可以使用另一个客户端,比如Python,然后做一些更复杂的操作,将你的字段值HSET合并为一个命令。

英文:

Assuming you are talking about using redis-cli, there is no way to support this at the moment. There is an open issue for this. See https://github.com/antirez/redis/issues/3474

As per Redis 4.0.0, HMSET is considered deprecated. You should use HSET instead. https://redis.io/commands/hset

You can use a transaction if you want to ensure all HSETs are done at the same time, and still enter them one line at a time.

MULTI
HSET info 30.2.25 REF_IP
HSET info 30.2.24 MY_HOST_IP
...
EXEC

The commands will be sent to the server one line at a time, but they are queued and only executed at the EXEC command.

You may use another client, say in Python, and then do something fancier as well to condense your field-value hsets into one command.

huangapple
  • 本文由 发表于 2020年1月3日 21:47:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定