念念不忘
必有回响

迁移Zookeeper的数据

由于之前的Hbase和Hadoop用的不是同一套zookeeper,现在需要将两个业务合并到同一个zookeeper,那么此时就需要将zookeeper中的数据迁移到新的zookeeper中。具体代码入下:

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import java.io.IOException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: liyang
 * Date: 2017/8/8
 * Time: 下午1:51
 * DESCIPTION:
 */
public class TransferZK {

    private static void transferData(ZooKeeper srcZK, ZooKeeper destZK, List children, String parent)
            throws KeeperException, InterruptedException {
        if (children == null || children.isEmpty()) {
            return;
        } else {
            for (String child : children) {
                String c = parent + "/" + child;
                System.out.println(c);
                byte[] data = srcZK.getData(c, false, null);
                if (destZK.exists(c, false) == null) {
                    destZK.create(c, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                } else {
                    destZK.setData(c, data, -1, null, null);
                }
                transferData(srcZK, destZK, srcZK.getChildren(c, false), c);
            }
        }
    }

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        //源zk
        ZooKeeper srcZK = new ZooKeeper("10.10.202.156:2181", 60000, null);
        //目标zk
        ZooKeeper destZK = new ZooKeeper("10.10.202.157:2181,10.10.202.158:2181,10.10.202.159:2181", 60000, null);
        //需要迁移的节点
        String node = "/hbase";
        List children = srcZK.getChildren(node, false);
        transferData(srcZK, destZK, children, node);
        srcZK.close();
        destZK.close();
    }
}

或者使用开源的工具zkcopy

赞(0) 打赏
未经允许不得转载:码农志 » 迁移Zookeeper的数据
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏