当前位置:操作系统 > Unix/Linux >>

redis数据批量导入导出

redis数据批量导入导出

针对工作中可能用到 将某台服务器中的redis数据 导出然后导入到新的服务器中,一种方法是redis-dump工具,但是 他需要安装ruby环境,安装环境的过程中还可能出现意想不到的错误。所以不得不选用其他方法了。一下 是几点思路 供参考。

1、数据导出,不用自己写,也不用第三方脚本,
echo "HGETALL xxx" | redis-cli -h localhost -p 6379
echo "HGETALL xxx" | redis-cli -h localhost -p 6379 >> wordlist.raw
2.这样得到的结果,你可以上到服务器上 查看 wordlist.raw文件
3.整理成输入需要的文件格式 
$f = 'xx.oo';
$lines = 0;
$inkey = 0;
$rawfile = 'xx.oo';
$hashkey = 'xx';
$buff = '';
$fp = fopen($rawfile,'w');
$fps = fopen($f,'r');
while($line= fgets($fps)){
 $inkey = !$inkey;
 if ($inkey){
$f = 'bayes_wordlist.raw';
$lines = 0;
$inkey = 0;
$rawfile = 'bayes_wordlist.3.raw';
$hashkey = 'bayes_wordlist';
$buff = '';
$fp = fopen($rawfile,'w');
$fps = fopen($f,'r');
while($line= fgets($fps)){
 $inkey = !$inkey;
 if ($inkey){
  $line = sprintf('"%s"',trim($line));
  $buff = "HSET $hashkey ".trim($line);
  }
 else
 {
  $buff .= ' "'.trim($line).'"';
  fwrite($fp,$buff."\r\n");
 }
}
  $buff = "HSET $hashkey ".trim($line);
 }
 else
 {
  $buff .= ' "'.trim($line).'"';
  fwrite($fp,$buff."\r\n");
 }
}
如果选择哪个库 要在首行写入 select x
4.利用redis-cli进行导入
echo `date` > pipe.log && cat xx.oo | redis-cli >> pipe.log && echo `date` >> pipe.log
5.加上了时间记录,和对导入结果进行纪录,真正执行导入的语句是
cat wordlist.raw | redis-cli,当然严谨些的话redis-cli后面还要加-h localhost -p 6379等参数。
 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,