一个导出redis有序集合sorted-sets的shell脚本
- 通过keys匹配需要导出的有序集合名称,这些集合命名格式为:*_010_09/Dec/2015
- 依次通过zscan导出有序集合中的数据,并分别保存
#/bin/sh
zset_pattern=”*_010_09/Dec/2015″
keys=`redis-cli keys $zset_pattern`
for k in $keys;do_date=`echo “$k”|awk -F”_” ‘{print $3}’|awk -F”/” ‘{print $3″_”$2″_”$1}’`
out=`echo “$k”|awk -F”_” ‘{print $1″_”$2}’`”_$_date.txt”
cursor=0batches=0
while [ 1==1 ];doreply=`redis-cli zscan $k $cursor`
cursor=`echo “$reply” | head -n 1`pairs=`echo “$reply”|tail -n +2`
[ $cursor -eq 0 ] && [ $batches -gt 0 ] && break
batches=`expr $batches + 1`
skip_key=0
echo “$pairs”|while read msdo
#skip empty members
[ “$ms” == “” ] && skip_key=1 && continue
[ $skip_key -eq 1 ] && skip_key=0 && continue
if [ “$m” == “” ];thenm=$ms && continue
fi
if [ “$s” == “” ];thens=$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