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