需求:之前写的脚本获取redis 最大的top-n的bigkeys,没有区分数据类型,如果要针对每个数据类型的前top-n的bigkeys获取呢?

db_ip=5.5.5.101
db_port=
password=abc123
cursor=
cnt=
new_cursor= function get_key()
{
redis-cli -h $db_ip -p $db_port -a $password scan $ count $cnt > scan_tmp_result
new_cursor=`sed -n '1p' scan_tmp_result`
sed -n '2,$p' scan_tmp_result > scan_result
} function get_keyinfo()
{
cat $ |while read line
do
key_size=`redis-cli -h $db_ip -p $db_port -a $password memory usage $line`
key_type=`redis-cli -h $db_ip -p $db_port -a $password type $line`
echo $line $key_type $key_size >> "$key_type.txt"
done
} get_key $cursor
get_keyinfo scan_result while [ $cursor -ne $new_cursor ]
do
get_key $new_cursor
get_keyinfo scan_result
done all_types="string list set hash zset"
for type in $all_types
do
echo "-----------top $1 $type data type-----------"
if [[ -f "$type.txt" ]];then
cat "$type.txt" | sort -nrk3 | sed -n "1,$1p"
else
echo "The instance does not have $type data type"
fi
done rm -rf scan_tmp_result
rm -rf scan_result
rm -rf string.txt
rm -rf set.txt

测试结果:

[redis@lxd-vm1 ~]$ sh get_type_top.sh
-----------top string data type-----------
test2 string
test string
c_9 string
c_99 string
c_999 string
-----------top list data type-----------
c list
b list
-----------top set data type-----------
hello set
world set
-----------top hash data type-----------
The instance does not have hash data type
-----------top zset data type-----------
The instance does not have zset data type
05-27 22:51