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

Username and Password Authentication Using HSBroker

Description

This section describes how to use HSBroker to connect to HetuEngine with the username and password, and assemble and send the SQL statements to HetuEngine for execution.

public class JDBCExampleBroker {    
    private static Properties properties = new Properties();
    private static void init() throws ClassNotFoundException {
        properties.setProperty("user", "YourUserName");
        properties.setProperty("password", "YourPassword");
        Class.forName("io.prestosql.jdbc.PrestoDriver");
    }
    public static void main(String[] args){
        Connection connection = null;
        ResultSet resultSet = null;
        PreparedStatement statement = null;
        String url = "jdbc:presto://192.168.1.130:29860,192.168.1.131:29860/hive/default?serviceDiscoveryMode=hsbroker";
        try {
            init();
            String sql = "show tables";
            connection = DriverManager.getConnection(url, properties);
            statement = connection.prepareStatement(sql.trim());
            resultSet = statement.executeQuery();
            Integer colNum = resultSet.getMetaData().getColumnCount();
            while (resultSet.next()) {
                for (int i = 1; i <= colNum; i++) {
                    System.out.println(resultSet.getString(i) + "\t");
                }
            }
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.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://HSBroker1_IP:HSBroker1_Port,HSBroker2_IP:HSBroker2_Port,HSBroker3_IP:HSBroker3_Port/catalog/schema?serviceDiscoveryMode=hsbroker

NOTE:
  • catalog and schema indicate the names of the catalog and schema to be connected to the JDBC client, respectively.
  • HSBroker_IP:HSBroker_Port indicates the HSBroker 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 human-machine user created in the cluster.

password

Password of the human-machine user created in the cluster.