【redis工具】redis-rdb-tools,redis分析key大小

【官网】

下载、安装官网参考:https://github.com/sripathikrishnan/redis-rdb-tools

【rdbtools作用】

Rdbtools是Redis的dump.rdb文件的解析器。解析器生成类似于xml sax解析器的事件,并且在内存方面非常有效。

另外,rdbtools提供了实用程序以:

  1. 生成所有数据库和键中数据的内存报告
  2. 将转储文件转换为JSON
  3. 使用标准差异工具比较两个转储文件

尽管有其他语言的类似项目,但Rdbtools是用Python编写的。有关更多信息,请参见常见问题解答

请参阅https://rdbtools.com以获取用于管理Redis,商业支持和其他企业功能的gui。

【安装 redis-rdb-tools】

先决条件:

  (1)python-lzf是可选的,但强烈建议您加快解析速度。

  (2)redis-py是可选的,仅在运行测试用例时需要。

要从PyPI安装(推荐):

pip install rdbtools python-lzf

源码安装:

git clone https://github.com/sripathikrishnan/redis-rdb-tools
cd redis-rdb-tools
sudo python setup.py install

【相关操作】直接看官网

下载、安装官网参考:https://github.com/sripathikrishnan/redis-rdb-tools

写的非常详细的文章:https://www.cnblogs.com/zhoujinyi/p/13276697.html

usage: rdb [options] /path/to/dump.rdb

Example : rdb --command json -k "user.*" /var/redis/6379/dump.rdb

positional arguments:
-- 要处理的dump文件
  dump_file             RDB Dump file to process       

optional arguments:
-- 帮助
  -h, --help            show this help message and exit  
-- 要处理的命令,-c后的有效参数为:json, diff,justkeys, justkeyvals, memory,protocol 
  -c CMD, --command CMD
                        Command to execute. Valid commands are json, diff,
                        justkeys, justkeyvals, memory and protocol  
-- 输出文件       
  -f FILE, --file FILE  Output file
-- 数据库号,可以提供多个数据库。如果未指定,则包括所有数据库。
  -n DBS, --db DBS      Database Number. Multiple databases can be provided.
                        If not specified, all databases will be included.
-- 要导出的key。这可以是一个正则表达式
  -k KEYS, --key KEYS   Keys to export. This can be a regular expression
-- key不导出。这可以是一个正则表达式
  -o NOT_KEYS, --not-key NOT_KEYS
                        Keys Not to export. This can be a regular expression
-- 解析的数据类型。可能的值为string,hash,set,sortedset,list。可以输入多种类型提供。如果未指定,则为所有数据类型
  -t TYPES, --type TYPES
                        Data types to include. Possible values are string,
                        hash, set, sortedset, list. Multiple typees can be
                        provided. If not specified, all data types will be
                        returned
--  将key的内存输出限制为大于或等此值(以字节为单位)
  -b BYTES, --bytes BYTES
                        Limit memory output to keys greater to or equal to
                        this value (in bytes)
--  将内存按大小输出前N个key                       
  -l LARGEST, --largest LARGEST
                        Limit memory output to only the top N keys (by size)
-- 将字符串转义为编码:raw(默认),print,utf8或base64。
  -e {raw,print,utf8,base64}, --escape {raw,print,utf8,base64}
                        Escape strings to encoding: raw (default), print,
                        utf8, or base64.
-- 使用command protocol参数,从所有键中删除到期的key                       
  -x, --no-expire       With protocol command, remove expiry from all keys
-- 使用command protocol参数,将N秒添加到key的到期时间
  -a N, --amend-expire N
                        With protocol command, add N seconds to key expiry
                        time

【我的基本实践】内存报告

nohup rdb -c memory /data/redis_6380/dump.rdb --bytes 128 -f memory6380_1.csv &

针对rdb文件,获取key大于 128B 的 key 信息;

最终效果:

  

 结果:21G的 dump.rdb 文件,花了4个小时,生成了185MB的 内存分析报告

【错误】

(1)没有装Python

参考:https://www.cnblogs.com/gered/p/13246889.html#_label6

(2)error: Python.h: No such file or directory

  

For apt ( Ubuntu, Debian... ): 对于apt ( Ubuntu,Debian ... ):

  1. sudo apt-get install python-dev # for python2.x installs
  2.  sudo apt-get install python3-dev # for python3.x installs

For yum ( CentOS, RHEL... ): 对于yum ( CentOS,RHEL ... ):

  1.  sudo yum install python-devel  # for python2.x installs
  2.  sudo yum install python3-devel # for python3.x installs

For dnf ( Fedora... ): 对于dnf ( Fedora ... ):

  1.  sudo dnf install python2-devel # for python2.x installs
  2.  sudo dnf install python3-devel # for python3.x installs

For zypper ( openSUSE... ): 对于zypper ( openSUSE ... ):

  1.  sudo zypper in python-devel # for python2.x installs
  2.  sudo zypper in python3-devel # for python3.x installs

For apk ( Alpine... ): 对于apk ( Alpine ... ):

  1.  # This is a departure from the normal Alpine naming
  2.  # scheme, which uses py2- and py3- prefixes
  3.  sudo apk add python2-dev # for python2.x installs
  4.  sudo apk add python3-dev # for python3.x installs

For apt-cyg ( Cygwin... ): 对于apt-cyg ( Cygwin ... ):

  1.  apt-cyg install python-devel # for python2.x installs
  2.  apt-cyg install python3-devel # for python3.x installs
原文地址:https://www.cnblogs.com/gered/p/14602512.html