更新时间:2022-07-14 GMT+08:00
通过Zookeeper方式访问Hive数据源
功能简介
通过Zookeeper方式连接到HetuEngine,组装对应的SQL发送到HetuEngine执行,完成对Hive数据源的增删改查操作。
public class JDBCExampleZk { private static Properties properties = new Properties(); private static void init() throws ClassNotFoundException { properties.setProperty("user", "YourUserName"); properties.setProperty("tenant", "default"); properties.setProperty("deploymentMode", "on_yarn"); properties.setProperty("ZooKeeperAuthType", "simple"); Class.forName("io.prestosql.jdbc.PrestoDriver"); } /** * 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:presto://192.168.136.130:2181,192.168.136.131:2181,192.168.136.132:2181/hive/default?" + "serviceDiscoveryMode=zooKeeper&zooKeeperNamespace=hsbroker"; 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所示:
参数名称 |
参数说明 |
---|---|
url |
jdbc:presto://zkNode1_IP:zkNode1_Port,zkNode2_IP:zkNode2_Port,zkNode3_IP:zkNode3_Port/catalog/schema?serviceDiscoveryMode=zooKeeper&zooKeeperNamespace=hsbroker
说明:
|
user |
访问HetuEngine的用户名,即在集群中创建的“机机”用户的用户名。 |
zookeeper.auth.type |
ZooKeeper的认证方式,普通模式下取值为“simple”。 |
tenant |
用户所属的租户。 |
deploymentMode |
只支持on_yarn。 |
父主题: Java样例代码