Java using JDBC connection Hive

  Recently, worked on a problem that hive jdbc connection problems, in fact, not a big problem, is to write the wrong url, preventing the connection. The problem is that HiveServer2 increase the level of security validation, resulting in normal circumstances, the parameters passed can not be used, hive connection returns an empty user error, the specific error is no longer go into details, today to talk about the jdbc url writing problems.

  Under normal circumstances url:

jdbc:hive2://node1:10000/default

  In this case, all the default, no access restrictions. Only seen from this example, the basic format of the URL is:

jdbc:hive2://Host:Port[/Schema];Property1=Value;Property2=Value;…

  For example, using a user name and password to connect to the URL:

jdbc:hive2://localhost:10000;AuthMech=3;UID=UserName;PWD=Password

  Which, UID defaults to the hive, AuthMech default is 2.

  hive jdbc Connection security mechanisms can be divided into centralized situation, were without authority, Kerberos, user names and user names and passwords, security attributes AuthMech, such as:

jdbc:hive2://localhost:10000;AuthMech=0
jdbc:hive2://localhost:10000;AuthMech=1;KrbRealm=EXAMPLE.COM;KrbHostFQDN=hs2.example.com;KrbServiceName=hive
jdbc:hive2://localhost:10000;AuthMech=2;UID=hs2
jdbc:hive2://localhost:10000;AuthMech=3;UID=hs2;PWD=*****
jdbc:hive2://localhost:10000;AuthMech=3;SSL=1;SSLKeyStore=C:\\Users\\bsmith\\Desktop\\keystore.jks;SSLKeyStorePwd=*****;UID=hs2;PWD=*****

 

  The above are hive jdbc url general pattern, however, if you need to modify some of the hive environment variables when executing sql, how to change it? Take a look:

 

jdbc:hive2://<host>:<port>/dbName;sess_var_list?hive_conf_list#hive_var_list

 

This is the full jdbc url format, in which:

 

  • sess_var_list parameter list: session parameters, such as the principal, serviceDiscoveryMode like;
  • hive_conf_list parameter list: hive configuration parameters, hive-site.xml configuration item;
  • hive_var_list parameter list: hive of variable parameters;

If you want to specify the hive implementation of the relevant parameters sql time, that some of the variables in the hive-site.xml, you need to add? Later, key = value form a plurality of separated by semicolons. E.g:

jdbc:hive2://ubuntu:11000/db2?hive.cli.conf.printheader=true;hive.exec.mode.local.auto.inputbytes.max=9999#stab=salesTable;icol=customerID

jdbc:hive2://?hive.cli.conf.printheader=true;hive.exec.mode.local.auto.inputbytes.max=9999#stab=salesTable;icol=customerID

jdbc:hive2://ubuntu:11000/db2;user=foo;password=bar

jdbc:hive2://server:10001/db;user=foo;password=bar?hive.server2.transport.mode=http;hive.server2.thrift.http.path=hs2

jdbc:hive2://zk01:2181,zk02:2181,zk03:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2

Specific parsing code in hive-jdbc, the class file HiveConnection.java. Resolve general process is:

// Now the parse URI with dummy Authority The Connection 
the URI jdbcURI = URI.create (uri.substring (URI_JDBC_PREFIX.length ()));
 // dbname Settings and the session (parameter acquiring a first portion) 
String sessVars = jdbcURI.getPath ();
 // the parse Hive the conf Settings (parameter acquiring the second portion) 
String confstr = jdbcURI.getQuery ();
 // the parse Hive var Settings (part acquiring a third parameter) 
String varStr jdbcURI.getFragment = ();

These are the various parts of the Hive Jdbc URL will be described.

 

HveServer2 official documentation:

https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients#HiveServer2Clients-JDBC

 

Guess you like

Origin www.cnblogs.com/flowerbirds/p/11256432.html