redis-cli and value from a file
Asked Answered
A

2

15

Is it possible to easily set specific value from a file using interactive redis-cli?

I'd like to achieve same result as with following Python snippet:

with open("some.jpg") as f:
    image_binary = f.read()

rd.hset("some_key", "image_binary", image_binary)
Ashwell answered 6/11, 2013 at 16:20 Comment(1)
You can write this script in LUA, and call it via redis-cli.Telegraphese
T
34

Is it possible to easily set specific value from a file using interactive redis-cli?

Since -x reads the last argument from STDIN, what about:

redis-cli -x HSET some_key image_binary <some.jpg

You can then easily retrieve the file as follow:

redis-cli --raw HGET some_key image_binary > img.jpg

Note that there is an extra \n character at the end.

Tertiary answered 6/11, 2013 at 22:10 Comment(4)
It's not really from within interactive shell, but I guess that's the only way possible with redis-cliAshwell
Right: there is no direct way in pure shell mode (as far as I know). As suggested above I assume Lua scripting may help if you really need to do that.Tertiary
Thanks. For those who want to read a file as a non-last argument, -X <tag> is helpful. I'm new to Redis and don't know when -X <tag> was introduced. Now it's Redis 7.0. For example, redis-cli -X foo SET some_key foo EX 100 < some_file.txt. It puts the content of some_file.txt to some_key.Faro
@Tertiary I have redis version 3.0.504 which doesn't have the -x tag.Bedard
D
2

A different approach is to feed redis-cli a sequence of commands written in a text file:

$ cat /tmp/commands.txt
SET item:3374 100
INCR item:3374
APPEND item:3374 xxx
GET item:3374
$ cat /tmp/commands.txt | redis-cli
OK
(integer) 101
(integer) 6
"101xxx"

Taken from : Redis Cli Manual

Dismuke answered 8/11, 2022 at 20:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.