更新时间:2024-08-03 GMT+08:00

HBase访问多个ZooKeeper样例程序

功能简介

在同一个客户端进程内同时访问FusionInsight ZooKeeper和第三方的ZooKeeper,其中HBase客户端访问FusionInsight ZooKeeper,客户应用访问第三方ZooKeeper。

代码样例

以下代码片段在“hbase-zk-example\src\main\java\com\huawei\hadoop\hbase\example”包的“TestZKSample”类中,用户主要需要关注“login”“connectApacheZK”这两个方法。

    private static void login(String keytabFile, String principal) throws IOException {
        conf = HBaseConfiguration.create();
        //In Windows environment
        String confDirPath = TestZKSample.class.getClassLoader().getResource("").getPath() + File.separator;[1]
        //In Linux environment
        //String confDirPath = System.getProperty("user.dir") + File.separator + "conf" + File.separator;

        // Set zoo.cfg for hbase to connect to fi zookeeper.
        conf.set("hbase.client.zookeeper.config.path", confDirPath + "zoo.cfg");
        if (User.isHBaseSecurityEnabled(conf)) {
            // jaas.conf file, it is included in the client pakcage file
            System.setProperty("java.security.auth.login.config", confDirPath + "jaas.conf");
            // set the kerberos server info,point to the kerberosclient
            System.setProperty("java.security.krb5.conf", confDirPath + "krb5.conf");
            // set the keytab file name
            conf.set("username.client.keytab.file", confDirPath + keytabFile);
            // set the user's principal
            try {
                conf.set("username.client.kerberos.principal", principal);
                User.login(conf, "username.client.keytab.file", "username.client.kerberos.principal",
                    InetAddress.getLocalHost().getCanonicalHostName());
            } catch (IOException e) {
                throw new IOException("Login failed.", e);
            }
        }
    }
    private void connectApacheZK() throws IOException, org.apache.zookeeper.KeeperException {
        try {
            // Create apache zookeeper connection.
            ZooKeeper digestZk = new ZooKeeper("127.0.0.1:2181", 60000, null);
            LOG.info("digest directory:{}", digestZk.getChildren("/", null));
            LOG.info("Successfully connect to apache zookeeper.");
        } catch (InterruptedException e) {
            LOG.error("Found error when connect apache zookeeper ", e);
        }
    }
  • [1]userdir获取的是编译后资源目录的路径。将初始化需要的配置文件“core-site.xml”、“hdfs-site.xml”、“hbase-site.xml”放置到"src/main/resources"的目录下。
  • “login”方法中的参数“java.security.auth.login.config”设置的jaas.conf文件用来设置访问ZooKeeper相关认证信息,样例代码中包含Client_new和Client两部分,Client_new的配置用来访问FusionInsight ZooKeeper,Client用来访问Apache ZooKeeper。
  • “login”方法中的参数“hbase.client.zookeeper.config.path”用来设置访问FusionInsight ZooKeeper客户端的配置,主要涉及如下三个参数:
    • zookeeper.sasl.clientconfig:指定使用jaas.conf文件中的对应配置访问FusionInsight ZooKeeper;
    • zookeeper.server.principal:指定ZooKeeper服务端使用principal;
    • zookeeper.sasl.client:如果集群是安全模式,该值设置为“true”,否则设置为“false”,设置为“false”的情况下,“zookeeper.sasl.clientconfig”“zookeeper.server.principal”参数不生效。