title: Hive JDBC connection problems
body: <p>I am trying to connect to Hive2 server via JDBC with kerberos authentication. After numerous attempts to make it work, I can't get it to work with the Cloudera driver. </p>
  
  <p>If someone can help me to solve the problem, I can greatly appreciate it.</p>
  
  <p>I have this method:</p>
  
  <pre><code>    private Connection establishConnection() {
      final String driverPropertyClassName = "driver";
      final String urlProperty = "url";
      Properties hiveProperties = config.getMatchingProperties("hive.jdbc");
      String driverClassName = (String) hiveProperties.remove(driverPropertyClassName);
      String url = (String) hiveProperties.remove(urlProperty);
      Configuration hadoopConfig = new Configuration();
      hadoopConfig.set("hadoop.security.authentication", "Kerberos");
      String p = config.getProperty("hadoop.core.site.path");
      Path path = new Path(p);
      hadoopConfig.addResource(path);
      UserGroupInformation.setConfiguration(hadoopConfig);
  
      Connection conn = null;
      if (driverClassName != null) {
          try {
              UserGroupInformation.loginUserFromKeytab(config.getProperty("login.user"), config.getProperty("keytab.file"));
              Driver driver = (Driver) Class.forName(driverClassName).newInstance();
              DriverManager.registerDriver(driver);
              conn = DriverManager.getConnection(url, hiveProperties);
          } catch (Throwable e) {
              LOG.error("Failed to establish Hive connection", e);
          }
      }
      return conn;
  }
  </code></pre>
  
  <p>URL for the server, that I am getting from the properties in the format described in Cloudera <a href="https://www.cloudera.com/documentation/enterprise/5-8-x/topics/cdh_sg_hiveserver2_security.html#topic_9_1_1__section_upd_qwv_ls" rel="nofollow noreferrer">documentation</a></p>
  
  <p>I am getting an exception:</p>
  
  <pre><code>2018-05-05 18:26:49 ERROR HiveReader:147 - Failed to establish Hive connection
  java.sql.SQLException: [Cloudera][HiveJDBCDriver](500164) Error initialized or created transport for authentication: Peer indicated failure: Unsupported mechanism type PLAIN.
      at com.cloudera.hiveserver2.hivecommon.api.HiveServer2ClientFactory.createTransport(Unknown Source)
      at com.cloudera.hiveserver2.hivecommon.api.ZooKeeperEnabledExtendedHS2Factory.createClient(Unknown Source)
  ...
  </code></pre>
  
  <p>I thought, that it is missing AuthMech attribute and added AuthMech=1 to the URL. Now I am getting:</p>
  
  <pre><code>java.sql.SQLNonTransientConnectionException: [Cloudera][JDBC](10100) Connection Refused: [Cloudera][JDBC](11640) Required Connection Key(s): KrbHostFQDN, KrbServiceName; [Cloudera][JDBC](11480) Optional Connection Key(s): AsyncExecPollInterval, AutomaticColumnRename, CatalogSchemaSwitch, DecimalColumnScale, DefaultStringColumnLength, DelegationToken, DelegationUID, krbAuthType, KrbRealm, PreparedMetaLimitZero, RowsFetchedPerBlock, SocketTimeOut, ssl, StripCatalogName, transportMode, UseCustomTypeCoercionMap, UseNativeQuery, zk
      at com.cloudera.hiveserver2.exceptions.ExceptionConverter.toSQLException(Unknown Source)
      at com.cloudera.hiveserver2.jdbc.common.BaseConnectionFactory.checkResponseMap(Unknown Source)
      ...
  </code></pre>
  
  <p>But KrbHostFQDN is already specified in the principal property as required in the documentation.</p>
  
  <p>Am I missing something or is this documentation wrong?</p>