Search in sources :

Example 1 with HiveJdbcBrowserException

use of org.apache.hive.jdbc.saml.IJdbcBrowserClient.HiveJdbcBrowserException in project hive by apache.

the class HiveConnection method openSession.

private void openSession() throws SQLException {
    LOG.debug("Opening Hive connection session");
    TOpenSessionReq openReq = new TOpenSessionReq();
    Map<String, String> openConf = new HashMap<>();
    // 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
    LOG.debug("Default database: {}", connParams.getDbName());
    openConf.put("use:database", connParams.getDbName());
    if (wmPool != null) {
        openConf.put("set:hivevar:wmpool", wmPool);
    }
    if (wmApp != null) {
        openConf.put("set:hivevar:wmapp", wmApp);
    }
    // set the session configuration
    if (sessConfMap.containsKey(HiveAuthConstants.HS2_PROXY_USER)) {
        openConf.put(HiveAuthConstants.HS2_PROXY_USER, sessConfMap.get(HiveAuthConstants.HS2_PROXY_USER));
    }
    // set create external purge table by default
    if (sessConfMap.containsKey(JdbcConnectionParams.CREATE_TABLE_AS_EXTERNAL)) {
        openConf.put("set:hiveconf:hive.create.as.external.legacy", sessConfMap.get(JdbcConnectionParams.CREATE_TABLE_AS_EXTERNAL).toLowerCase());
    }
    if (isHplSqlMode()) {
        openConf.put("set:hivevar:mode", HPLSQL);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Dumping initial configuration...");
        for (Map.Entry<String, String> entry : openConf.entrySet()) {
            LOG.debug("{}={}", entry.getKey(), entry.getValue());
        }
    }
    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));
    }
    // explicitly do a HTTP post request and get the response.
    try {
        int numRetry = 1;
        if (isBrowserAuthMode()) {
            numRetry = 2;
            browserClient.startListening();
        }
        for (int i = 0; i < numRetry; i++) {
            try {
                openSession(openReq);
            } catch (TException e) {
                if (isSamlRedirect(e)) {
                    boolean success = doBrowserSSO();
                    if (!success) {
                        String msg = browserClient.getServerResponse() == null || browserClient.getServerResponse().getMsg() == null ? "" : browserClient.getServerResponse().getMsg();
                        throw new SQLException("Could not establish connection to " + jdbcUriString + ": " + msg, " 08S01", e);
                    }
                } else {
                    throw new SQLException("Could not establish connection to " + jdbcUriString + ": " + e.getMessage(), " 08S01", e);
                }
            }
        }
    } catch (HiveJdbcBrowserException e) {
        throw new SQLException("Could not establish connection to " + jdbcUriString + ": " + e.getMessage(), " 08S01", e);
    } finally {
        if (browserClient != null) {
            try {
                browserClient.close();
            } catch (IOException e) {
                LOG.error("Unable to close the browser SSO client : " + e.getMessage(), e);
            }
        }
    }
    isClosed = false;
}
Also used : TException(org.apache.thrift.TException) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) HiveJdbcBrowserException(org.apache.hive.jdbc.saml.IJdbcBrowserClient.HiveJdbcBrowserException) TOpenSessionReq(org.apache.hive.service.rpc.thrift.TOpenSessionReq) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap) Savepoint(java.sql.Savepoint)

Aggregations

IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 SQLException (java.sql.SQLException)1 Savepoint (java.sql.Savepoint)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 HiveJdbcBrowserException (org.apache.hive.jdbc.saml.IJdbcBrowserClient.HiveJdbcBrowserException)1 TOpenSessionReq (org.apache.hive.service.rpc.thrift.TOpenSessionReq)1 TException (org.apache.thrift.TException)1