static JdbcConnectionParams parseURL(String uri)throws JdbcUriParseException, SQLException, ZooKeeperHiveClientException { JdbcConnectionParamsconnParams=newJdbcConnectionParams(); // ... // Extract host, port if (connParams.isEmbeddedMode()) { // In case of embedded mode we were supplied with an empty authority. // So we never substituted the authority with a dummy one. connParams.setHost(jdbcURI.getHost()); connParams.setPort(jdbcURI.getPort()); } else { // Configure host, port and params from ZooKeeper if used, // and substitute the dummy authority with a resolved one configureConnParams(connParams); //就是这里 // We check for invalid host, port while configuring connParams with configureConnParams() StringauthorityStr= connParams.getHost() + ":" + connParams.getPort(); LOG.info("Resolved authority: " + authorityStr); uri = uri.replace(dummyAuthorityString, authorityStr); connParams.setJdbcUriString(uri); } return connParams; }
staticvoidconfigureConnParams(JdbcConnectionParams connParams) throws ZooKeeperHiveClientException { //省略... serverHosts = zooKeeperClient.getChildren().forPath("/" + zooKeeperNamespace); // Remove the znodes we've already tried from this list serverHosts.removeAll(connParams.getRejectedHostZnodePaths()); if (serverHosts.isEmpty()) { thrownewZooKeeperHiveClientException( "Tried all existing HiveServer2 uris from ZooKeeper."); } // Now pick a server node randomly serverNode = serverHosts.get(randomizer.nextInt(serverHosts.size())); connParams.setCurrentHostZnodePath(serverNode); // Read config string from the znode for this server node StringserverConfStr= newString( zooKeeperClient.getData().forPath("/" + zooKeeperNamespace + "/" + serverNode), Charset.forName("UTF-8")); applyConfs(serverConfStr, connParams); //省略 ... }
支持连接参数 connParams 初始化完成,接下来调用方法 org.apache.hive.jdbc.HiveConnection#openTransport 打开与 ThriftServer 的连接,在一个 while true 的循环中尝试连接,处理异常。看下其中处理打开异常的部分: