Updated on 2024-04-02 GMT+08:00

Accessing Multiple ZooKeepers

Function

This function allows simultaneous access to FusionInsight ZooKeeper from the HBase client and third-party ZooKeeper from the customer application in the same client process.

Example code

The following code snippet is in the TestZKSample class of the hbase-zk-example\src\main\java\com\huawei\hadoop\hbase\example. You need to pay attention to the login and connectApacheZK methods.

    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 obtains the conf directory in the resource path after compilation. Save the core-site.xml, hdfs-site.xml, and hbase-site.xml configuration files required for initialization and the user credential file used for security authentication to the src/main/resources directory.
  • The jaas.conf file specified by the java.security.auth.login.config parameter in the login method is used to set the authentication information for accessing ZooKeeper. The example code contains the Client_new and Client configurations. The Client_new configuration is used to access FusionInsight ZooKeeper and the Client configuration is used to access Apache ZooKeeper.
  • The hbase.client.zookeeper.config.path parameter in the login method controls the access to the FusionInsight ZooKeeper client. The following parameters are involved:
    • zookeeper.sasl.clientconfig: Specifies the configuration in the jaas.conf file used for accessing FusionInsight ZooKeeper.
    • zookeeper.server.principal: Specifies the principle of the ZooKeeper server.
    • zookeeper.sasl.client: If the MRS cluster works in the security mode, set this parameter to true. Otherwise, set this parameter to false. If this parameter is set to false, the zookeeper.sasl.clientconfig and zookeeper.server.principal parameters do not take effect.