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等參數。