Exemplo n.º 1
0
 private void openTransport() throws SQLException {
   while (true) {
     try {
       assumeSubject =
           JdbcConnectionParams.AUTH_KERBEROS_AUTH_TYPE_FROM_SUBJECT.equals(
               sessConfMap.get(JdbcConnectionParams.AUTH_KERBEROS_AUTH_TYPE));
       transport = isHttpTransportMode() ? createHttpTransport() : createBinaryTransport();
       if (!transport.isOpen()) {
         LOG.info("Will try to open client transport with JDBC Uri: " + jdbcUriString);
         transport.open();
       }
       break;
     } catch (TTransportException e) {
       LOG.info("Could not open client transport with JDBC Uri: " + jdbcUriString);
       // We'll retry till we exhaust all HiveServer2 uris from ZooKeeper
       if ((sessConfMap.get(JdbcConnectionParams.SERVICE_DISCOVERY_MODE) != null)
           && (JdbcConnectionParams.SERVICE_DISCOVERY_MODE_ZOOKEEPER.equalsIgnoreCase(
               sessConfMap.get(JdbcConnectionParams.SERVICE_DISCOVERY_MODE)))) {
         try {
           // Update jdbcUriString, host & port variables in connParams
           // Throw an exception if all HiveServer2 uris have been exhausted,
           // or if we're unable to connect to ZooKeeper.
           Utils.updateConnParamsFromZooKeeper(connParams);
         } catch (ZooKeeperHiveClientException ze) {
           throw new SQLException(
               "Could not open client transport for any of the Server URI's in ZooKeeper: "
                   + ze.getMessage(),
               " 08S01",
               ze);
         }
         // Update with new values
         jdbcUriString = connParams.getJdbcUriString();
         host = connParams.getHost();
         port = connParams.getPort();
         LOG.info("Will retry opening client transport");
       } else {
         LOG.info(
             "Transport Used for JDBC connection: "
                 + sessConfMap.get(JdbcConnectionParams.TRANSPORT_MODE));
         throw new SQLException(
             "Could not open client transport with JDBC Uri: "
                 + jdbcUriString
                 + ": "
                 + e.getMessage(),
             " 08S01",
             e);
       }
     }
   }
 }
Exemplo n.º 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;
  }
Exemplo n.º 3
0
  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);
  }