Skip to content

Instantly share code, notes, and snippets.

@schabluk
Forked from abtrout/redis-pipe.md
Created July 16, 2019 11:08
Show Gist options
  • Save schabluk/75f56a98194b760e6658dad60e912757 to your computer and use it in GitHub Desktop.
Save schabluk/75f56a98194b760e6658dad60e912757 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment