Updated on 2024-08-12 GMT+08:00

Accessing Multiple ZooKeepers

Description

This section describes how to access both FusionInsight ZooKeeper and the third-party ZooKeeper in the same client process using the testConnectHive and testConnectApacheZK methods respectively.

In the JDBCExample class of the hive-jdbc-example-multizk package, the code structure of the main method is as follows:
public static void main(String[] args) throws InstantiationException,IllegalAccessException, ClassNotFoundException, SQLException, IOException{
 testConnectHive();// Method of accessing FusionInsight ZooKeeper
 testConnectApacheZk();// Method of accessing the open-source ZooKeeper
 }

Accessing FusionInsight ZooKeeper

If only the method of accessing FusionInsight ZooKeeper needs to be executed, comment out the testConnectApacheZk method in the main function.

Before using the testConnectHive method to access FusionInsight ZooKeeper, perform the following operations:

  1. Change the value of USER_NAME in the init method in JDBCExample. USER_NAME indicates the user that is used to access FusionInsight ZooKeeper and has permissions of the FusionInsight Hive and Hadoop common user groups.
  2. Go to the client decompression path FusionInsight_Cluster_1_Services_ClientConfig_ConfigFiles\Hive\config and manually import the core-site.xml and hiveclient.properties files to the hive-jdbc-example-multizk\src\main\resources directory of the sample project.
  3. Download the krb5.conf and user.keytab files of the user to the resources directory in the hive-jdbc-example-multizk package.
  4. Check and change the values of zk.port and zk.quorum in the hiveclient.properties file in the resources directory.

    • zk.port: indicates the port for accessing FusionInsight ZooKeeper. Generally, the default value is used. Change the value as required.
    • zk.quorum: indicates the IP address for accessing ZooKeeper quorumpeer. Set it to the IP address of the cluster deployed with the FusionInsight ZooKeeper service.

Accessing Open-Source ZooKeeper

To use testConnectApacheZk to connect to the open-source ZooKeeper code, change xxx.xxx.xxx.xxx in the following code to the IP address of the open-source ZooKeeper to be connected. Change the port number as required. If only the sample for accessing the third-party ZooKeeper needs to be executed, comment out the testConnectHive method in the main function.

digestZK = new org.apache.zookeeper.ZooKeeper("xxx.xxx.xxx.xxx:port", 60000, null);

After the ZooKeeper connection is used, the connection needs to be closed. Otherwise, connection leakage may occur. You can perform the processing according to the actual situation. The code is as follows:

//Use the try-with-resources mode. After the try statement is executed, the ZooKeeper connection is automatically closed.
try (org.apache.zookeeper.ZooKeeper digestZk =
    new org.apache.zookeeper.ZooKeeper("xxx.xxx.xxx.xxx:port", 600000, null)) {
    ...
}