Updated on 2022-11-18 GMT+08:00

Accessing Hive Data Sources Using ZooKeeper

Description

This section describes how to use Zookeeper to connect to HetuEngine, assemble SQL statements, and send the SQL statements to HetuEngine for execution to add, delete, modify, and query Hive data sources.

public class JDBCExampleZk {
    private static Properties properties = new Properties();
    private static void init() throws ClassNotFoundException {
        properties.setProperty("user", "YourUserName");
        properties.setProperty("tenant", "default");
        properties.setProperty("deploymentMode", "on_yarn");

        properties.setProperty("ZooKeeperAuthType", "simple");
        Class.forName("io.prestosql.jdbc.PrestoDriver");
    }

    /**
     * Program entry
     *
     * @param args no need program parameter
     */
    public static void main(String[] args) {
        Connection connection = null;
        ResultSet result = null;
        PreparedStatement statement = null;
        String url = "jdbc:presto://192.168.136.130:2181,192.168.136.131:2181,192.168.136.132:2181/hive/default?"
            + "serviceDiscoveryMode=zooKeeper&zooKeeperNamespace=hsbroker";

        try {
            init();

            String sql = "show tables";
            connection = DriverManager.getConnection(url, properties);
            statement = connection.prepareStatement(sql.trim());
            result = statement.executeQuery();
            ResultSetMetaData resultMetaData = result.getMetaData();
            Integer colNum = resultMetaData.getColumnCount();
            for (int j = 1; j <= colNum; j++) {
                System.out.print(resultMetaData.getColumnLabel(j) + "\t");
            }
            System.out.println();
            while (result.next()) {
                for (int j = 1; j <= colNum; j++){
                    System.out.print(result.getString(j) + "\t");
                }
                System.out.println();
            }
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (result != null) {
                try {
                    result.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Table 1 describes the parameters in the preceding code.

Table 1 Parameter description

Parameter

Description

url

jdbc:presto://zkNode1_IP:zkNode1_Port,zkNode2_IP:zkNode2_Port,zkNode3_IP:zkNode3_Port/catalog/schema?serviceDiscoveryMode=zooKeeper&zooKeeperNamespace=hsbroker

NOTE:
  • catalog and schema indicate the names of the catalog and schema to be connected to the JDBC client, respectively.
  • zkNode_IP:zkNode_Port indicates the ZooKeeper URL. Use commas (,) to separate multiple URLs, for example, 192.168.81.37:2181,192.168.195.232:2181,192.168.169.84:2181.

user

Username for accessing HetuEngine, that is, the username of the machine-machine user created in the cluster.

zookeeper.auth.type

Indicates the ZooKeeper authentication mode. The value is simple in normal mode.

tenant

Indicates the tenant to which a user belongs.

deploymentMode

Only on_yarn is supported.