Example #1
0
 private TTransport createHttpTransport() throws SQLException, TTransportException {
   CloseableHttpClient httpClient;
   boolean useSsl = isSslConnection();
   // Create an http client from the configs
   httpClient = getHttpClient(useSsl);
   try {
     transport = new THttpClient(getServerHttpUrl(useSsl), httpClient);
     // We'll call an open/close here to send a test HTTP message to the server. Any
     // TTransportException caused by trying to connect to a non-available peer are thrown here.
     // Bubbling them up the call hierarchy so that a retry can happen in openTransport,
     // if dynamic service discovery is configured.
     TCLIService.Iface client = new TCLIService.Client(new TBinaryProtocol(transport));
     TOpenSessionResp openResp = client.OpenSession(new TOpenSessionReq());
     if (openResp != null) {
       client.CloseSession(new TCloseSessionReq(openResp.getSessionHandle()));
     }
   } catch (TException e) {
     LOG.info(
         "JDBC Connection Parameters used : useSSL = "
             + useSsl
             + " , httpPath  = "
             + sessConfMap.get(JdbcConnectionParams.HTTP_PATH)
             + " Authentication type = "
             + sessConfMap.get(JdbcConnectionParams.AUTH_TYPE));
     String msg = "Could not create http connection to " + jdbcUriString + ". " + e.getMessage();
     throw new TTransportException(msg, e);
   }
   return transport;
 }
Example #2
0
  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;
  }