通过HSBroker的用户名密码认证实现查询HetuEngine SQL任务
功能简介
通过HSBroker连接方式实现,使用用户名和密码连接到HetuEngine,组装对应的SQL发送到HetuEngine执行。
public class JDBCExampleBroker { private static Properties properties = new Properties(); private static void init() throws ClassNotFoundException { // 认证用的密码写入代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; // 本示例以密码保存在环境变量中来实现身份验证为例,运行本示例前,请先在本地环境中设置环境变量HETUENGINE_PASSWORD properties.setProperty("user", "YourUserName"); String password = System.getenv("HETUENGINE_PASSWORD"); properties.setProperty("password",password); Class.forName("io.XXXsql.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: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(); } } } } }
上述代码中各参数说明如表1所示:
参数名称 |
参数说明 |
---|---|
url |
jdbc:XXX://HSBroker1_IP:HSBroker1_Port,HSBroker2_IP:HSBroker2_Port,HSBroker3_IP:HSBroker3_Port/catalog/schema?serviceDiscoveryMode=hsbroker
说明:
|
user |
访问HetuEngine的用户名,即在集群中创建的“人机”用户的用户名。 |
password |
在集群中创建的“人机”用户的用户密码。 |