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

通过HSBroker的用户名密码认证实现查询HetuEngine SQL任务

功能简介

通过HSBroker方式连接到HetuEngine,组装对应的SQL发送到HetuEngine执行,完成对Hive数据源的增删改查操作。
public class JDBCExampleBroker {
    private static Properties properties = new Properties();
    private static void init() throws ClassNotFoundException {
        properties.setProperty("user", "YourUserName");
        properties.setProperty("SSL", "false");
        Class.forName("io.xxxsql.jdbc.xxxDriver");
    }
    /**
     * 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:xxx://192.168.1.130:29861,192.168.1.131:29861/hive/default?serviceDiscoveryMode=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();
                }
            }
        }
    }
}

上述代码中各参数说明如表1所示:

表1 参数及参数说明

参数名称

参数说明

url

jdbc:xxx://HSBroker1_IP:HSBroker1_Port,HSBroker2_IP:HSBroker2_Port,HSBroker3_IP:HSBroker3_Port/catalog/schema?serviceDiscoveryMode=hsbroker

说明:
  • XXX:驱动名,请以实际样例代码中的内容为准。
  • catalogschema分别是JDBC客户端要连接的catalog和schema名称。
  • HSBroker_IP:HSBroker_Port,是HSBroker的URL,多个URL以逗号隔开。例如:“192.168.81.37:2181,192.168.195.232:2181,192.168.169.84:2181”

    在Manager页面,选择“集群 > 服务 > HetuEngine > 实例”,获取HSBroker所有实例的业务IP;在“配置”页签,搜索“server.port”,获取HSBroker端口号。

user

访问HetuEngine的用户名,即在集群中创建的“机机”用户的用户名。

SSL

是否使用HTTPS连接,默认为“false”