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

Accessing Hive Data Sources Using HSFabric

Description

This section describes how to use HSFabric to connect to HetuServer, assemble SQL statements, and send the SQL statements to HetuServer for execution to add, delete, modify, and query Hive data sources.
public class JDBCExampleFabric {
    private static Properties properties = new Properties();
    private static void init() throws ClassNotFoundException {
        properties.setProperty("user", "YourUserName");
        properties.setProperty("SSL", "false");
        Class.forName("io.XXX.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:29903,192.168.1.131:29903/hive/default?serviceDiscoveryMode=hsfabric";
        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: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.81.37:29903,192.168.195.232:29903,192.168.169.84:29903.

    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 HetuServer, that is, the username of the machine-machine user created in the cluster.

SSL

Indicates whether to use the HTTPS connection. The default value is false.