zoukankan      html  css  js  c++  java
  • redis中获取每个数据类型top-n的bigkeys信息

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

    db_ip=5.5.5.101
    db_port=6379
    password=abc123
    cursor=0
    cnt=100
    new_cursor=0
    
    function get_key()
    {
    redis-cli -h $db_ip -p $db_port -a $password scan $1 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 $1 |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 5
    -----------top 5 string data type-----------
    test2 string 12325
    test string 1589
    c_9 string 49
    c_99 string 49
    c_999 string 49
    -----------top 5 list data type-----------
    c list 885
    b list 553
    -----------top 5 set data type-----------
    hello set 285
    world set 71
    -----------top 5 hash data type-----------
    The instance does not have hash data type
    -----------top 5 zset data type-----------
    The instance does not have zset data type
  • 相关阅读:
    Linux基础3-1 Bash及其特性
    三、手写ORM实现数据库更新
    三、TCP协议
    一、OIS七层模型及数据传输过程
    泛型缓存原理
    树莓派公网服务器实现frp内网穿透
    Dto数据传输对象
    Ubuntu下 Nginx静态代理部署网页常见报错
    JWT权限验证
    解决传入的请求具有过多的参数,该服务器支持最多 2100 个参数
  • 原文地址:https://www.cnblogs.com/imdba/p/10173306.html
Copyright © 2011-2022 走看看