Skip to content

Instantly share code, notes, and snippets.

@abtrout
Last active August 11, 2023 03:29
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save abtrout/432ce44fa77a9620c739 to your computer and use it in GitHub Desktop.
Save abtrout/432ce44fa77a9620c739 to your computer and use it in GitHub Desktop.
Bash script to prepare Redis commands for mass insertion via `redis-cli --pipe`

Redis supports mass insert via redis-cli --pipe, but commands need to be written in redis protocol. For more details, check out the Redis mass insertion page. This script takes care of converting ordinary redis commands to redis protocol.

#!/usr/bin/env bash

while read CMD; do
  # each command begins with *{number arguments in command}\r\n
  XS=($CMD); printf "*${#XS[@]}\r\n"
  # for each argument, we append ${length}\r\n{argument}\r\n
  for X in $CMD; do printf "\$${#X}\r\n$X\r\n"; done
done
Example:
$ for N in $(seq 1 1000); do echo "SADD test $N"; done > data.txt
$ cat data.txt | sh redis-pipe.sh | redis-cli --pipe
All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 1000
$ echo "SCARD test" | redis-cli
(integer) 1000
@luyishisi
Copy link

这个脚本在部分电脑,比如我自己的会报错
Syntax error: "(" unexpected (expecting "done")
解决方式是
cat data.txt | bash crate.sh | redis-cli --pipe
将sh 改成bash 。。

@MatthewLQM
Copy link

hi @abtrout, I have a problem with your code. when my value is json data, this code may not work well.
(By the way, I'm sorry for my bad English.)

@Stadicus
Copy link

I encountered the following two issue running GNU bash, version 4.4.20 on Ubuntu 18.04

  • XS=($CMD) not recognized, substitute with XS="${CMD}"
  • printf "*${#XS[@]}\r\n gives character count instead of word count, substitute with 'set -- $XSand use variable$#`

Full adapted script:

#!/usr/bin/env bash

while read CMD; do
  # each command begins with *{number arguments in command}\r\n
  XS="${CMD}"
  set -- $XS
  printf "*${#}\r\n"
  # for each argument, we append ${length}\r\n{argument}\r\n
  for X in $CMD; do
    printf "\$${#X}\r\n$X\r\n"
  done
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment