gpt4 book ai didi

python - Redis 批量插入不起作用

转载 作者:IT王子 更新时间:2023-10-29 06:15:41 25 4
gpt4 key购买 nike

问题很简单,就像我想在管道模式下使用文件和 redis-cli 在 redis 中进行大量插入一样。 Redis 文档在此处对此进行了解释:http://redis.io/topics/mass-insert

我的文件只包含这个命令:

HMSET client:1 name "Michael"

当我在我的 shell (Ubuntu Linux) 中尝试时:

cat data.txt | redis-cli --pipe

我收到这个错误:

ERR unknown command '$4'
ERR wrong number of arguments for 'echo' command
ERR unknown command '$20'

我看到其他用户也有同样的问题 ( How to use Redis mass insertion? ) 但我还没有找到好的解决方案。

如有任何帮助,我们将不胜感激。

更新:解决方案

最后,对我来说最好的选择是使用用 Python 开发的 redis 客户端。这是一个非常直接的解决方案,重点是我们不需要直接使用/编写 redis 协议(protocol)。

我选择 redis-py 客户端 ( https://github.com/andymccurdy/redis-py )。安装后,您可以在 python 脚本中导入它的功能。这是我的脚本如何完成工作的示例:

import redis

# Connection to 0 database (default in redis)
r = redis.Redis(host="localhost",db=0)

# inserting client hashmaps
r.hmset('client:1', {'name':'John', 'company':'Microsoft'})
r.hmset('client:2', {'name':'James', 'company':'Apple'})

# inserting a list of domains for client 1
r.rpush('client:1:domains','www.microsoft.com','www.msn.com')

#to print values in stdout
print(r.hgetall('client:1'))

最佳答案

您的输入文件应该包含 Redis 协议(protocol),而不是文本命令。

请在此处查看示例:Redis Mass Insertion

这里描述了 Redis 协议(protocol): http://redis.io/topics/protocol

关于python - Redis 批量插入不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18872380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com