0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

redisプロトコルをbashで出力する話

Last updated at Posted at 2017-03-13

大量のデータをredisに流し込む際に、コマンドを送るよりバルクでプロトコル流すと速いので、
http://redis-documentasion-japanese.readthedocs.io/ja/latest/topics/mass-insert.html

pythonで書いてみたけど

redis_bulk.py
def cmd2bulk(*cmd):
    z = lambda x: (len(bytes(str(x), "utf-8")), x)
    return "*%d\r\n%s" % (len(cmd), "".join(["$%d\r\n%s\r\n" % z(x) for x in cmd]))

if __name__ == '__main__':
    import sys
    import unittest

    class TestRedisBulk(unittest.TestCase):

        def test_redis_proto1(self):
            expect = "*3\r\n$3\r\nSET\r\n$6\r\nKEY985\r\n$8\r\nVALUE985\r\n"
            result = cmd2bulk("SET", "KEY985", "VALUE985")
            self.assertEqual(expect, result)

        def test_redis_proto2(self):
            expect = "*5\r\n$4\r\nSADD\r\n$4\r\nhoge\r\n$9\r\nあああ\r\n$6\r\nいい\r\n$3\r\n\r\n"
            result = cmd2bulk("SADD", "hoge", "あああ", "いい", "")
            self.assertEqual(expect, result)

    unittest.main()

bashでもいけるんじゃね

redis_bulk_test.sh
function redis_bulk() {
    printf "*%d\r\n" "${#@}"
    for x in "${@}"; do
        printf "$%d\r\n%s\r\n" $(echo -ne "$x" | wc -c) "$x"
    done
}

redis_bulk HMSET "ほげ" foo 123 bar "ばー" | redis-cli --pipe

っていう話。

注意

bashの方は実運用してないから未知数。

pythonの方は100万件くらいの塊を連続で一気に流し込んでも問題なく動いてます。
コマンドを都度送るより断然速いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?