任意指定一個key獲取該key所處在哪個node節點
阿新 • • 發佈:2018-12-29
需求:任意指定一個key獲取該key所處在哪個node節點上。
說明:目前來看用處不是太大。
#獲取指定的key在哪個slot上,該key可以是存在的或者不存在的均可。 key_slot=`redis-cli -h 5.5.5.101 -p 29001 -a abc123 -c cluster keyslot $1` #獲取node和slot的分佈,輸出格式如下,開始的#是註釋,並非有效資料 #5.5.5.101|0-5461 #5.5.5.102|5462-10922 #5.5.5.103|10923-16383 node_slot=`redis-cli -h 5.5.5.101 -p 29001 -a abc123 -c cluster nodes | grep master | awk -F' |:' '{print $2"|"$NF}'` #判斷是否找到對應的slot find_tag=0 for i in $node_slot do node_ip=`echo $i | awk -F '[|]' '{print $1}'` start_slot=`echo $i | awk -F '[|]' '{print $2}' | awk -F '[-]' '{print $1}'` end_slot=`echo $i | awk -F '[|]' '{print $2}' | awk -F '[-]' '{print $2}'` for((j=$start_slot;j<=$end_slot;j++)) do if [[ $j == $key_slot ]];then echo $node_ip find_tag=1 fi done if [[ $find_tag == 1 ]];then #找到後,退出迴圈,因為slot不會有重複的 break fi done
測試
[[email protected] ~]$ sh get_node.sh a 5.5.5.103 [[email protected]-vm1 ~]$ sh get_node.sh b 5.5.5.101 [[email protected]-vm1 ~]$ sh get_node.sh c 5.5.5.102 [[email protected]-vm1 ~]$ sh get_node.sh 1{abc}1 5.5.5.102 [[email protected]-vm1 ~]$ sh get_node.sh 2{abc}2 5.5.5.102 [[email protected]-vm1 ~]$ sh get_node.sh hello{lxd}world 5.5.5.101 [[email protected]-vm1 ~]$ sh get_node.sh new{lxd}year 5.5.5.101 [[email protected]-vm1 ~]$