예제 #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);
       }
     }
   }
 }
예제 #2
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);
  }