14

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)
vartec
  • 131,205
  • 36
  • 218
  • 244

2 Answers2

33

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.

Community
  • 1
  • 1
deltheil
  • 15,496
  • 2
  • 44
  • 64
  • It's not really from within interactive shell, but I guess that's the only way possible with `redis-cli` – vartec Nov 07 '13 at 10:30
  • 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. – deltheil Nov 08 '13 at 11:00
  • Thanks. For those who want to read a file as a non-last argument, `-X ` is helpful. I'm new to Redis and don't know when `-X ` 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`. – ElpieKay Nov 25 '22 at 03:47
  • @deltheil I have redis version `3.0.504` which doesn't have the `-x` tag. – WISHY Nov 25 '22 at 11:15
0

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

Aryan Ahmed Anik
  • 466
  • 1
  • 9
  • 23