hive集成kerberos问题2
比如Connection con = DriverManager.getConnection("jdbc:hive2://host:10000/cdnlog;principal=hdfs/host@KERBEROS_HADOOP", "user1", "");
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import
java.sql.SQLException;
import
java.sql.Connection;
import
java.sql.ResultSet;
import
java.sql.Statement;
import
java.sql.DriverManager;
public
class
HiveJdbcClient {
private
static
String driverName =
"org.apache.hive.jdbc.HiveDriver"
;
public
static
void
main(String[] args)
throws
SQLException {
try
{
Class.forName(driverName);
}
catch
(ClassNotFoundException e) {
e.printStackTrace();
System.exit(
1
);
}
Connection con = DriverManager.getConnection(
"jdbc:hive2://host:10000/cdnlog;principal=hdfs/host@KERBEROS_HADOOP"
,
"ericni"
,
"1234"
);
Statement stmt = con.createStatement();
String tableName =
"dd_log"
;
stmt.execute(
"drop table if exists "
+ tableName);
String createSql =
"create table "
+ tableName +
" (tt string) LOCATION 'hdfs://bipcluster/tmp/dd_log' "
;
stmt.execute(createSql);
System.out.println(
"create table sql is"
+ createSql);
}
}
|
在建立连接时,调用了DriverManager类的getConnection的方法。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public
static
Connection getConnection(String url,
String user, String password)
throws
SQLException {
java.util.Properties info =
new
java.util.Properties();
// Gets the classloader of the code that called this method, may
// be null.
ClassLoader callerCL = DriverManager.getCallerClassLoader();
if
(user !=
null
) {
info.put(
"user"
, user);
//调用java.util.Properties类的put方法,生成user的value
}
if
(password !=
null
) {
info.put(
"password"
, password);
//生成password的value
}
return
(getConnection(url, info, callerCL));
}
|
然后调用org.apache.hive.jdbc.HiveConnection类
并调用openTransport方法,传入参数为(uri, connParams.getHost(), connParams.getPort(), connParams.getSessionVars());
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
if
(!sessConf.containsKey(HIVE_AUTH_TYPE)
|| !sessConf.get(HIVE_AUTH_TYPE).equals(HIVE_AUTH_SIMPLE)){
try
{
if
(sessConf.containsKey(HIVE_AUTH_PRINCIPAL)) {
transport = KerberosSaslHelper.getKerberosTransport(
sessConf.get(HIVE_AUTH_PRINCIPAL), host, transport);
}
else
{
String userName = sessConf.get(HIVE_AUTH_USER);
if
((userName ==
null
) || userName.isEmpty()) {
userName = HIVE_ANONYMOUS_USER;
}
String passwd = sessConf.get(HIVE_AUTH_PASSWD);
if
((passwd ==
null
) || passwd.isEmpty()) {
passwd = HIVE_ANONYMOUS_PASSWD;
}
transport = PlainSaslHelper.getPlainTransport(userName, passwd, transport);
}
}
catch
(SaslException e) {
throw
new
SQLException(
"Could not establish secure connection to "
+ uri +
": "
+ e.getMessage(),
" 08S01"
);
}
}
|