LTME
LTME

Reputation: 1126

Executing batches of commands using redis cli

I have a long text file of redis commands that I need to execute using the redis command line interface:

e.g.

DEL 9012012
DEL 1212
DEL 12214314

etc.

I can't seem to figure out a way to enter the commands faster than one at a time. There are several hundred thousands lines, so I don't want to just pile them all into one DEL command, they also don't need to all run at once.

Upvotes: 85

Views: 66401

Answers (5)

rkosolapov
rkosolapov

Reputation: 21

I want to place it here since I searched for my case and found this question along the way. I needed to execute redis multi-line command via npm scripts with variables to be parsed in redis commands (redis implemented as a Docker container). To do so, I created txt named redis-delete-key.txt file with commands:

SELECT 1
DEL ${KEY}

where I choose desired redis db and remove the key.

In package.json I added the following command:

"scripts": {
  ...
  "delete-key-from-redis": "envsubst <redis-delete-key.txt | docker exec -i redis redis-cli"
}

where envsubst is linux command to replace variables in file.

To pass the variables I execute the npm command like this:

KEY="anykey" npm run delete-key-from-redis

Works for me, hope will be helpful for someone.

Upvotes: 1

Kamehameha
Kamehameha

Reputation: 5473

I know this is an old old thread, but adding this since it seems missed out among other answers, and one that works well for me.

Using heredoc works well here, if you don't want to use echo or explicitly add \n or create a new file -

redis-cli <<EOF
select 15
get a
EOF

Upvotes: 13

Sanghyun Lee
Sanghyun Lee

Reputation: 23002

If you don't want to make a file, use echo and \n

echo "DEL 9012012\nDEL 1212" | redis-cli

Upvotes: 72

mrnovalles
mrnovalles

Reputation: 353

The redis-cli --pipe can be used for mass-insertion. It is available since 2.6-RC4 and in Redis 2.4.14. For example:

cat data.txt | redis-cli --pipe

More info in: http://redis.io/topics/mass-insert

Upvotes: 21

ControlAltDel
ControlAltDel

Reputation: 35011

the following code works for me with redis 2.4.7 on mac

./redis-cli < temp.redisCmds

Does that satisfy your requirements? Or are you looking to see if there's a way to programmatically do it faster?

Upvotes: 105

Related Questions