Support Questions
Find answers, ask questions, and share your expertise

java jdbc connect to phoenix from cdh7.1.1 cluster with Kerberos enviroment failed

New Contributor

I use java JDBC to connect phoenix from cdh7.1.1 cluster with Kerberos environment, there are two ways:

 this one is a long time no response

Connection connection = null;
Statement statement = null;
ResultSet rs = null;
PreparedStatement ps = null;
org.apache.hadoop.conf.Configuration conf = null;

try {

System.setProperty("java.security.krb5.conf", "C:\\krb5.conf");

Properties pps = System.getProperties();

pps.setProperty("hbase.zookeeper.quorum", "cdp2.hadoop.com");

pps.setProperty("hbase.master.kerberos.principal", "hbase/cdp2.hadoop.com@HADOOP.COM");

pps.setProperty("hbase.regionserver.kerberos.principal", "hbase/cdp2.hadoop.com@HADOOP.COM");

pps.setProperty("phoenix.queryserver.kerberos.principal", "HTTP/cdp2.hadoop.com@HADOOP.COM");

pps.setProperty("hbase.security.authentication", "kerberos");

pps.setProperty("hadoop.security.authentication", "kerberos");

pps.setProperty("zookeeper.znode.parent", "/hbase");

Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
// Connect to the database
connection = DriverManager.getConnection("jdbc:phoenix:cdp2.hadoop.com:2181/hbase:hbase@HADOOP.COM:C:\\hbase.keytab");
System.out.println("Connection established....");
// Create a JDBC statement
statement = connection.createStatement();

// Execute our statements
statement.executeUpdate(
"create table user (id INTEGER NOT NULL PRIMARY KEY, d.first_name VARCHAR,d.last_name VARCHAR)");
statement.executeUpdate("upsert into user values (1,'John','Mayer')");
statement.executeUpdate("upsert into user values (2,'Eva','Peters')");
connection.commit();

// Query for selecting records from table
ps = connection.prepareStatement("select * from user");
rs = ps.executeQuery();
System.out.println("Table Values");
while (rs.next()) {
Integer id = rs.getInt(1);
String name = rs.getString(2);
System.out.println("\tRow: " + id + " = " + name);
}
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (Exception e) {
}
}
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
}
}
if (statement != null) {
try {
statement.close();
} catch (Exception e) {
}
}
if (connection != null) {
try {
connection.close();
} catch (Exception e) {
}
}
}

another one is:

Connection connection = null;
Statement statement = null;
ResultSet rs = null;
PreparedStatement ps = null;
org.apache.hadoop.conf.Configuration conf = null;

try {

System.setProperty("java.security.krb5.conf", "C:\\krb5.conf");

conf = HBaseConfiguration.create();

conf.set("hadoop.security.authentication" , "Kerberos" );
conf.set("kerberos.principal" , "hbase@HADOOP.COM" );
UserGroupInformation.setConfiguration(conf);
Connection conn = null;
UserGroupInformation.loginUserFromKeytab("hbase@HADOOP.COM", "C:\\hbase.keytab");

Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");

connection = DriverManager.getConnection("jdbc:phoenix:cdp2.hadoop.com:2181/hbase");
System.out.println("Connection established....");
// Create a JDBC statement
statement = connection.createStatement();

// Execute our statements
statement.executeUpdate(
"create table user (id INTEGER NOT NULL PRIMARY KEY, d.first_name VARCHAR,d.last_name VARCHAR)");
statement.executeUpdate("upsert into user values (1,'John','Mayer')");
statement.executeUpdate("upsert into user values (2,'Eva','Peters')");
connection.commit();

// Query for selecting records from table
ps = connection.prepareStatement("select * from user");
rs = ps.executeQuery();
System.out.println("Table Values");
while (rs.next()) {
Integer id = rs.getInt(1);
String name = rs.getString(2);
System.out.println("\tRow: " + id + " = " + name);
}
} catch (SQLException | ClassNotFoundException | IOException e) {
e.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (Exception e) {
}
}
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
}
}
if (statement != null) {
try {
statement.close();
} catch (Exception e) {
}
}
if (connection != null) {
try {
connection.close();
} catch (Exception e) {
}
}
}

this one is a long time no response than the error below:

Tue Jul 21 20:41:29 CST 2020, RpcRetryingCaller{globalStartTime=1595335177837, pause=100, maxAttempts=16}, java.io.IOException: Call to cdp3.hadoop.com/192.168.0.236:16020 failed on local exception: java.io.IOException: Connection closed
Tue Jul 21 20:41:49 CST 2020, RpcRetryingCaller{globalStartTime=1595335177837, pause=100, maxAttempts=16}, java.io.IOException: Call to cdp3.hadoop.com/192.168.0.236:16020 failed on local exception: java.io.IOException: Connection closed

at org.apache.phoenix.util.ServerUtil.parseServerException(ServerUtil.java:138)
at org.apache.phoenix.query.ConnectionQueryServicesImpl.ensureTableCreated(ConnectionQueryServicesImpl.java:1204)
at org.apache.phoenix.query.ConnectionQueryServicesImpl.createTable(ConnectionQueryServicesImpl.java:1501)
at org.apache.phoenix.schema.MetaDataClient.createTableInternal(MetaDataClient.java:2721)
at org.apache.phoenix.schema.MetaDataClient.createTable(MetaDataClient.java:1114)
at org.apache.phoenix.compile.CreateTableCompiler$1.execute(CreateTableCompiler.java:192)
at org.apache.phoenix.jdbc.PhoenixStatement$2.call(PhoenixStatement.java:408)
at org.apache.phoenix.jdbc.PhoenixStatement$2.call(PhoenixStatement.java:391)
at org.apache.phoenix.call.CallRunner.run(CallRunner.java:53)
at org.apache.phoenix.jdbc.PhoenixStatement.executeMutation(PhoenixStatement.java:390)
at org.apache.phoenix.jdbc.PhoenixStatement.executeMutation(PhoenixStatement.java:378)
at org.apache.phoenix.jdbc.PhoenixStatement.executeUpdate(PhoenixStatement.java:1806)
at org.apache.phoenix.query.ConnectionQueryServicesImpl$12.call(ConnectionQueryServicesImpl.java:2569)
at org.apache.phoenix.query.ConnectionQueryServicesImpl$12.call(ConnectionQueryServicesImpl.java:2532)
at org.apache.phoenix.util.PhoenixContextExecutor.call(PhoenixContextExecutor.java:76)
at org.apache.phoenix.query.ConnectionQueryServicesImpl.init(ConnectionQueryServicesImpl.java:2532)
at org.apache.phoenix.jdbc.PhoenixDriver.getConnectionQueryServices(PhoenixDriver.java:255)
at org.apache.phoenix.jdbc.PhoenixEmbeddedDriver.createConnection(PhoenixEmbeddedDriver.java:150)
at org.apache.phoenix.jdbc.PhoenixDriver.connect(PhoenixDriver.java:221)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at com.apress.phoenix.chapter3.PhoenixDemo.main(PhoenixDemo.java:62)
Caused by: java.net.SocketTimeoutException: callTimeout=1200000, callDuration=1220553: Failed after attempts=16, exceptions:
Tue Jul 21 20:39:38 CST 2020, RpcRetryingCaller{globalStartTime=1595335177837, pause=100, maxAttempts=16}, java.io.IOException: Call to cdp3.hadoop.com/192.168.0.236:16020 failed on local exception: java.io.IOException: Connection closed

 

I am confused, how to connect Phoenix from the cdh7.1.1 cluster with Keberos environment?