redis資料大量匯入匯出

來源:互聯網
上載者:User
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等參數。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.