一個導出redis有序集合sorted-sets的shell腳本

  1. 經過keys匹配須要導出的有序集合名稱,這些集合命名格式爲:*_010_09/Dec/2015
  2. 依次經過zscan導出有序集合中的數據,並分別保存

#/bin/sh
zset_pattern=」*_010_09/Dec/2015″
keys=`redis-cli keys $zset_pattern`
for k in $keys;doredis

_date=`echo 「$k」|awk -F」_」 ‘{print $3}’|awk -F」/」 ‘{print $3″_」$2″_」$1}’`
out=`echo 「$k」|awk -F」_」 ‘{print $1″_」$2}’`」_$_date.txt」
cursor=0shell

batches=0
while [ 1==1 ];dospa

reply=`redis-cli zscan $k $cursor`
cursor=`echo 「$reply」 | head -n 1`ip

pairs=`echo 「$reply」|tail -n +2`get

[ $cursor -eq 0 ] && [ $batches -gt 0 ] && break
batches=`expr $batches + 1`it


skip_key=0
echo 「$pairs」|while read msclass

doawk

#skip empty members
[ 「$ms」 == 「」 ] && skip_key=1 && continue
[ $skip_key -eq 1 ] && skip_key=0 && continue
if [ 「$m」 == 「」 ];thencli

m=$ms && continuedate

fi
if [ 「$s」 == 「」 ];then

s=$ms
echo 「$m $s」 >> $out
m=」」 && s=」」

fi

done

done

done

 

零一積流|IT參考 原創文章,轉載請註明出處: http://www.it-refer.com/2015/12/14/a-shell-script-to-export-data-of-sorted-sets-from-redis

相關文章
相關標籤/搜索