private void openSession() throws SQLException { TOpenSessionReq openReq = new TOpenSessionReq(); Map<String, String> openConf = new HashMap<String, String>(); // for remote JDBC client, try to set the conf var using 'set foo=bar' for (Entry<String, String> hiveConf : connParams.getHiveConfs().entrySet()) { openConf.put("set:hiveconf:" + hiveConf.getKey(), hiveConf.getValue()); } // For remote JDBC client, try to set the hive var using 'set hivevar:key=value' for (Entry<String, String> hiveVar : connParams.getHiveVars().entrySet()) { openConf.put("set:hivevar:" + hiveVar.getKey(), hiveVar.getValue()); } // switch the database openConf.put("use:database", connParams.getDbName()); // set the session configuration Map<String, String> sessVars = connParams.getSessionVars(); if (sessVars.containsKey(HiveAuthFactory.HS2_PROXY_USER)) { openConf.put(HiveAuthFactory.HS2_PROXY_USER, sessVars.get(HiveAuthFactory.HS2_PROXY_USER)); } openReq.setConfiguration(openConf); // Store the user name in the open request in case no non-sasl authentication if (JdbcConnectionParams.AUTH_SIMPLE.equals(sessConfMap.get(JdbcConnectionParams.AUTH_TYPE))) { openReq.setUsername(sessConfMap.get(JdbcConnectionParams.AUTH_USER)); openReq.setPassword(sessConfMap.get(JdbcConnectionParams.AUTH_PASSWD)); } try { TOpenSessionResp openResp = client.OpenSession(openReq); // validate connection Utils.verifySuccess(openResp.getStatus()); if (!supportedProtocols.contains(openResp.getServerProtocolVersion())) { throw new TException("Unsupported Hive2 protocol"); } protocol = openResp.getServerProtocolVersion(); sessHandle = openResp.getSessionHandle(); } catch (TException e) { LOG.error("Error opening session", e); throw new SQLException( "Could not establish connection to " + jdbcUriString + ": " + e.getMessage(), " 08S01", e); } isClosed = false; }
public HiveConnection(String uri, Properties info) throws SQLException { setupLoginTimeout(); try { connParams = Utils.parseURL(uri); } catch (ZooKeeperHiveClientException e) { throw new SQLException(e); } jdbcUriString = connParams.getJdbcUriString(); // extract parsed connection parameters: // JDBC URL: jdbc:hive2://<host>:<port>/dbName;sess_var_list?hive_conf_list#hive_var_list // each list: <key1>=<val1>;<key2>=<val2> and so on // sess_var_list -> sessConfMap // hive_conf_list -> hiveConfMap // hive_var_list -> hiveVarMap host = connParams.getHost(); port = connParams.getPort(); sessConfMap = connParams.getSessionVars(); hiveConfMap = connParams.getHiveConfs(); hiveVarMap = connParams.getHiveVars(); for (Map.Entry<Object, Object> kv : info.entrySet()) { if ((kv.getKey() instanceof String)) { String key = (String) kv.getKey(); if (key.startsWith(HIVE_VAR_PREFIX)) { hiveVarMap.put(key.substring(HIVE_VAR_PREFIX.length()), info.getProperty(key)); } else if (key.startsWith(HIVE_CONF_PREFIX)) { hiveConfMap.put(key.substring(HIVE_CONF_PREFIX.length()), info.getProperty(key)); } } } isEmbeddedMode = connParams.isEmbeddedMode(); if (isEmbeddedMode) { EmbeddedThriftBinaryCLIService embeddedClient = new EmbeddedThriftBinaryCLIService(); embeddedClient.init(null); client = embeddedClient; } else { // extract user/password from JDBC connection properties if its not supplied in the // connection URL if (info.containsKey(JdbcConnectionParams.AUTH_USER)) { sessConfMap.put( JdbcConnectionParams.AUTH_USER, info.getProperty(JdbcConnectionParams.AUTH_USER)); if (info.containsKey(JdbcConnectionParams.AUTH_PASSWD)) { sessConfMap.put( JdbcConnectionParams.AUTH_PASSWD, info.getProperty(JdbcConnectionParams.AUTH_PASSWD)); } } if (info.containsKey(JdbcConnectionParams.AUTH_TYPE)) { sessConfMap.put( JdbcConnectionParams.AUTH_TYPE, info.getProperty(JdbcConnectionParams.AUTH_TYPE)); } // open the client transport openTransport(); // set up the client client = new TCLIService.Client(new TBinaryProtocol(transport)); } // add supported protocols supportedProtocols.add(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V1); supportedProtocols.add(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V2); supportedProtocols.add(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V3); supportedProtocols.add(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V4); supportedProtocols.add(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V5); supportedProtocols.add(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V6); supportedProtocols.add(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V7); supportedProtocols.add(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V8); // open client session openSession(); // Wrap the client with a thread-safe proxy to serialize the RPC calls client = newSynchronizedClient(client); }