private TTransport open(Configuration conf, @NotNull URI uri) throws
      TException, IOException, LoginException {
    boolean useSSL = MetastoreConf.getBoolVar(conf, MetastoreConf.ConfVars.USE_SSL);
    boolean useSasl = MetastoreConf.getBoolVar(conf, MetastoreConf.ConfVars.USE_THRIFT_SASL);
    boolean useFramedTransport = MetastoreConf.getBoolVar(conf, MetastoreConf.ConfVars.USE_THRIFT_FRAMED_TRANSPORT);
    boolean useCompactProtocol = MetastoreConf.getBoolVar(conf, MetastoreConf.ConfVars.USE_THRIFT_COMPACT_PROTOCOL);
    int clientSocketTimeout = (int) MetastoreConf.getTimeVar(conf,
        MetastoreConf.ConfVars.CLIENT_SOCKET_TIMEOUT, TimeUnit.MILLISECONDS);
    int connectionTimeout = (int) MetastoreConf.getTimeVar(conf,
        MetastoreConf.ConfVars.CLIENT_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);

    LOG.debug("Connecting to {}, framedTransport = {}", uri, useFramedTransport);

    String host = uri.getHost();
    int port = uri.getPort();

    // Sasl/SSL code is copied from HiveMetastoreCLient
    if (!useSSL) {
      transport = new TSocket(new TConfiguration(),host, port, clientSocketTimeout, connectionTimeout);
    } else {
      String trustStorePath = MetastoreConf.getVar(conf, MetastoreConf.ConfVars.SSL_TRUSTSTORE_PATH).trim();
      if (trustStorePath.isEmpty()) {
        throw new IllegalArgumentException(MetastoreConf.ConfVars.SSL_TRUSTSTORE_PATH.toString()
            + " Not configured for SSL connection");
      }
      String trustStorePassword =
          MetastoreConf.getPassword(conf, MetastoreConf.ConfVars.SSL_TRUSTSTORE_PASSWORD);
      String trustStoreType =
              MetastoreConf.getVar(conf, MetastoreConf.ConfVars.SSL_TRUSTSTORE_TYPE).trim();
      String trustStoreAlgorithm =
              MetastoreConf.getVar(conf, MetastoreConf.ConfVars.SSL_TRUSTMANAGERFACTORY_ALGORITHM).trim();

      // Create an SSL socket and connect
      transport = SecurityUtils.getSSLSocket(host, port, clientSocketTimeout, connectionTimeout,
          trustStorePath, trustStorePassword, trustStoreType, trustStoreAlgorithm);
      LOG.info("Opened an SSL connection to metastore, current connections");
    }

    if (useSasl) {
      // Wrap thrift connection with SASL for secure connection.
      HadoopThriftAuthBridge.Client authBridge =
          HadoopThriftAuthBridge.getBridge().createClient();

      // check if we should use delegation tokens to authenticate
      // the call below gets hold of the tokens if they are set up by hadoop
      // this should happen on the map/reduce tasks if the client added the
      // tokens into hadoop's credential store in the front end during job
      // submission.
      String tokenSig = MetastoreConf.getVar(conf, MetastoreConf.ConfVars.TOKEN_SIGNATURE);
      // tokenSig could be null
      String tokenStrForm = SecurityUtils.getTokenStrForm(tokenSig);

      if (tokenStrForm != null) {
        LOG.info("HMSC::open(): Found delegation token. Creating DIGEST-based thrift connection.");
        // authenticate using delegation tokens via the "DIGEST" mechanism
        transport = authBridge.createClientTransport(null, host,
            "DIGEST", tokenStrForm, transport,
            MetaStoreUtils.getMetaStoreSaslProperties(conf, useSSL));
      } else {
        LOG.info("HMSC::open(): Could not find delegation token. Creating KERBEROS-based thrift connection.");
        String principalConfig =
            MetastoreConf.getVar(conf, MetastoreConf.ConfVars.KERBEROS_PRINCIPAL);
        transport = authBridge.createClientTransport(
            principalConfig, host, "KERBEROS", null,
            transport, MetaStoreUtils.getMetaStoreSaslProperties(conf, useSSL));
      }
    } else {
      if (useFramedTransport) {
        transport = new TFramedTransport(transport);
      }
    }

    final TProtocol protocol;
    if (useCompactProtocol) {
      protocol = new TCompactProtocol(transport);
    } else {
      protocol = new TBinaryProtocol(transport);
    }
    client = new ThriftHiveMetastore.Client(protocol);
    if (!transport.isOpen()) {
      transport.open();
      LOG.info("Opened a connection to metastore, current connections");

      if (!useSasl && MetastoreConf.getBoolVar(conf, MetastoreConf.ConfVars.EXECUTE_SET_UGI)) {
        // Call set_ugi, only in unsecure mode.
        try {
          UserGroupInformation ugi = SecurityUtils.getUGI();
          client.set_ugi(ugi.getUserName(), Arrays.asList(ugi.getGroupNames()));
        } catch (LoginException e) {
          LOG.warn("Failed to do login. set_ugi() is not successful, " +
              "Continuing without it.", e);
        } catch (IOException e) {
          LOG.warn("Failed to find ugi of client set_ugi() is not successful, " +
              "Continuing without it.", e);
        } catch (TException e) {
          LOG.warn("set_ugi() not successful, Likely cause: new client talking to old server. "
              + "Continuing without it.", e);
        }
      }
    }

    LOG.debug("Connected to metastore, using compact protocol = {}", useCompactProtocol);
    return transport;
  }
Create database if it doesn't exist

@param name        Database name
@param description Database description
@param location    Database location
@param params      Database params
@throws TException if database exists
standalone-metastore/metastore-tools/tools-common/src/main/java/org/apache/hadoop/hive/metastore/tools/HMSClient.java