最近想在本地搭建线上的项目环境,项目中用到了redis,就需要把redis的数据拷贝到本地,拷贝的方式这里说两种
1.把线上redis目录下的dump.rdb放到本地redis目录下,因为没有线上服务器的密码,所以久没能用这种简单的方式
2.还有网上一大摞的方法,没成功
3.既然脑瓜不好使,那就写java代码把!
RedisUtil 类,提供redis线程池
package mlstudy.redis;
import java.util.HashMap;
import java.util.Map;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class RedisUtil {
//将JedisPool放入map缓存起来
private static Map<String, JedisPool> jedisPoolMap = new HashMap<String, JedisPool>();
/**
* 创建连接池
* @return
*/
private static void createJedisPool(String url,int port){
JedisPoolConfig config = new JedisPoolConfig();
//设置连接参数
config.setMaxIdle(10);
// 最大连接数, 默认8个
config.setMaxTotal(200);
// 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
config.setMaxWaitMillis(1000);
//将连接池放入map缓存
jedisPoolMap.put(url+port, new JedisPool(config, url, port));
}
/**
* 多线程初始化redis线程池
*/
private static synchronized void initPool(String url,int port){
if (!jedisPoolMap.containsKey(url+port)){
createJedisPool(url,port);
}
}
/**
* 获取jedis实例
*/
public static Jedis getJedis(String url, int port, String auth){
initPool(url, port);
try {
Jedis jedis = jedisPoolMap.get(url + port).getResource();
if (auth != null){
jedis.auth(auth);
}
return jedis;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 释放jedis资源
*
* @param jedis
*/
public static void close(Jedis jedis) {
if (null != jedis) {
jedis.close();
}
}
/**
*
* @param args String
* @throws Exception Exception
*/
public static void main(String[] args) throws Exception {
Jedis jedis = RedisUtil.getJedis("10.10.50.50", 6379, "peiWWU3y!Zc#giKp");
//jedis.hgetAll("kpclientcfg");
System.out.println(jedis.keys("*"));
}
}
package mlstudy.redis;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;
public class RedisDataTransfer {
/**
* @param srcDbIndex 源数据库索引
* @param targetDbIndex 目标数据库索引
*/
public static void copyRedisData(int srcDbIndex,int targetDbIndex){
Jedis jedisSrc = RedisUtil.getJedis("10.10.10.50", 6379, "1234556");
Jedis jedisTarget = RedisUtil.getJedis("localhost", 6379, null);
//这里选择数据库db0
jedisSrc.select(srcDbIndex);
jedisTarget.select(targetDbIndex);
//获取源数据库所有“表”
Set<String> allKeys = jedisSrc.keys("*");
//System.out.println(allKeys);
Iterator<String> it = allKeys.iterator();
int count = 0;
int successCount=0;
while(it.hasNext()){
count ++;
String key = it.next();
//获取key(“表对应的类型”)我们知道,redis数据有五种类型,分别是string,hash,list,set,zset
String keyType = jedisSrc.type(key);
if (keyType.equals("string")){
String data = jedisSrc.get(key);
jedisTarget.set(key, data);
successCount++;
}
else if (keyType.equals("hash")){
Map<String, String> data = jedisSrc.hgetAll(key);
for (Map.Entry<String, String> entry: data.entrySet()){
jedisTarget.hset(key, entry.getKey(),entry.getValue());
}
successCount ++;
}
else if (keyType.equals("zset")){
Set<Tuple> value = jedisSrc.zrangeWithScores(key, 0, -1);
for (Tuple entry : value) {
jedisTarget.zadd(key, entry.getScore(), entry.getElement());
}
successCount ++;
}
else if (keyType.equals("set")){
Set<String> smembers = jedisSrc.smembers(key);
for (String member : smembers){
jedisTarget.sadd(key, member);
}
successCount ++;
}
else if (keyType.equals("list")){
List<String> list = jedisSrc.lrange(key, 0 ,-1);
for (String value : list){
jedisTarget.lpush(key, value);
}
successCount ++;
}
System.out.println("总复制数:"+count+","+"复制成功数:"+successCount);
}
System.out.println(count+","+successCount);
}
public static void main(String[] args) {
copyRedisData(0,0);
}
}