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

Username and Password Authentication Using HSFabric

Description

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

public class JDBCExampleFabric {    
    private static Properties properties = new Properties();
    private static void init() throws ClassNotFoundException {
        // Hard-coded password or plaintext password in code poses significant security risks. Encrypt and store them in configuration files or environment variables and decrypt them when needed.
        // The password is stored in environment variables for identity authentication. Before running this example, set the environment variable HETUENGINE_PASSWORD.
        properties.setProperty("user", "YourUserName");
        String password = System.getenv("HETUENGINE_PASSWORD");
        properties.setProperty("password", password);
        Class.forName("io.xxx.jdbc.xxxDriver");
    }
    public static void main(String[] args){
        Connection connection = null;
        ResultSet resultSet = null;
        PreparedStatement statement = null;
        String url = "jdbc:xxx://192.168.1.130:29902,192.168.1.131:29902/hive/default?serviceDiscoveryMode=hsfabric";
        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:xxx://HSFabric1_IP:HSFabric1_Port,HSFabric2_IP:HSFabric2_Port,HSFabric3_IP:HSFabric3_Port/catalog/schema?serviceDiscoveryMode=hsfabric

NOTE:
  • xxx: driver name, which is subjective to the real-world code you use.
  • catalog and schema indicate the names of the catalog and schema to be connected to the JDBC client, respectively.
  • HSFabric_IP:HSFabric_Port indicates the HSFabric URL. Use commas (,) to separate multiple URLs, for example, 192.168.1.130:29902,192.168.1.131:29902,192.168.1.132:29902.

    On FusionInsight Manager, choose Cluster > Services > HetuEngine and click Instance to obtain the service IP addresses of all HSFabric instances. On the Configurations page, search for gateway.port to obtain the port number of HSFabric.

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.