文档首页/
MapReduce服务 MRS/
开发指南(LTS版)/
HetuEngine开发指南(安全模式)/
开发HetuEngine应用/
HetuEngine样例程序(Java)/
通过HSFabric的用户名密码认证实现查询HetuEngine SQL任务
更新时间:2024-08-03 GMT+08:00
通过HSFabric的用户名密码认证实现查询HetuEngine SQL任务
功能简介
通过HSFabric连接方式实现,使用用户名和密码连接到HetuEngine,组装对应的SQL发送到HetuEngine执行。
public class JDBCExampleFabric { 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.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(); } } } } }
上述代码中各参数说明如表1所示:
父主题: HetuEngine样例程序(Java)