public static void sync(String sourceUri, String targetUri) throws IOException, URISyntaxException {
        RedisURI suri = new RedisURI(sourceUri);
        RedisURI turi = new RedisURI(targetUri);
        final ExampleClient target = new ExampleClient(turi.getHost(), turi.getPort());
        Configuration tconfig = Configuration.valueOf(turi);
        if (tconfig.getAuthPassword() != null) {
            Object auth = target.send(AUTH, tconfig.getAuthPassword().getBytes());
            System.out.println("AUTH:" + auth);
        }
        final AtomicInteger dbnum = new AtomicInteger(-1);
        Replicator r = dress(new RedisReplicator(suri));

        r.addEventListener(new EventListener() {
            @Override
            public void onEvent(Replicator replicator, Event event) {
                if (event instanceof DumpKeyValuePair) {
                    DumpKeyValuePair dkv = (DumpKeyValuePair) event;
                    // Step1: select db
                    DB db = dkv.getDb();
                    int index;
                    if (db != null && (index = (int) db.getDbNumber()) != dbnum.get()) {
                        target.send(SELECT, toByteArray(index));
                        dbnum.set(index);
                        System.out.println("SELECT:" + index);
                    }

                    // Step2: restore dump data
                    if (dkv.getExpiredMs() == null) {
                        Object r = target.restore(dkv.getKey(), 0L, dkv.getValue(), true);
                        System.out.println(r);
                    } else {
                        long ms = dkv.getExpiredMs() - System.currentTimeMillis();
                        if (ms <= 0) return;
                        Object r = target.restore(dkv.getKey(), ms, dkv.getValue(), true);
                        System.out.println(r);
                    }
                }

                if (event instanceof DefaultCommand) {
                    // Step3: sync aof command
                    DefaultCommand dc = (DefaultCommand) event;
                    Object r = target.send(dc.getCommand(), dc.getArgs());
                    System.out.println(r);
                }
            }
        });

        r.addCloseListener(new CloseListener() {
            @Override
            public void handle(Replicator replicator) {
                target.close();
            }
        });
        r.open();
    }
public static void sync ( String sourceUri , String targetUri ) throws IOException , URISyntaxException { RedisURI suri = new RedisURI ( sourceUri ) ; RedisURI turi = new RedisURI ( targetUri ) ; final ExampleClient target = new ExampleClient ( turi . getHost ( ) , turi . getPort ( ) ) ; Configuration tconfig = Configuration . valueOf ( turi ) ; if ( tconfig . getAuthPassword ( ) != null ) { Object auth = target . send ( AUTH , tconfig . getAuthPassword ( ) . getBytes ( ) ) ; System . out . println ( "AUTH:" + auth ) ; } final AtomicInteger dbnum = new AtomicInteger ( - 1 ) ; Replicator r = dress ( new RedisReplicator ( suri ) ) ; r . addEventListener ( new EventListener ( ) { @ Override public void onEvent ( Replicator replicator , Event event ) { if ( event instanceof DumpKeyValuePair ) { DumpKeyValuePair dkv = ( DumpKeyValuePair ) event ; DB db = dkv . getDb ( ) ; int index ; if ( db != null && ( index = ( int ) db . getDbNumber ( ) ) != dbnum . get ( ) ) { target . send ( SELECT , toByteArray ( index ) ) ; dbnum . set ( index ) ; System . out . println ( "SELECT:" + index ) ; } if ( dkv . getExpiredMs ( ) == null ) { Object r = target . restore ( dkv . getKey ( ) , 0L , dkv . getValue ( ) , true ) ; System . out . println ( r ) ; } else { long ms = dkv . getExpiredMs ( ) - System . currentTimeMillis ( ) ; if ( ms <= 0 ) return ; Object r = target . restore ( dkv . getKey ( ) , ms , dkv . getValue ( ) , true ) ; System . out . println ( r ) ; } } if ( event instanceof DefaultCommand ) { DefaultCommand dc = ( DefaultCommand ) event ; Object r = target . send ( dc . getCommand ( ) , dc . getArgs ( ) ) ; System . out . println ( r ) ; } } } ) ; r . addCloseListener ( new CloseListener ( ) { @ Override public void handle ( Replicator replicator ) { target . close ( ) ; } } ) ; r . open ( ) ; }
/*
Precondition:
1. Make sure the two redis version is same.
2. Make sure the single key-value is not very big.(highly recommend less then 1 MB)

We running following steps to sync two redis.
1. Get rdb stream from source redis.
2. Convert source rdb stream to redis dump format.
3. Use Jedis RESTORE command to restore that dump format to target redis.
4. Get aof stream from source redis and sync to target redis.