java 连接带 kerberos 验证的 phoenix

唉,网上的资料比较少,找了好久,压根不知道如入告诉 phoenix 客户端来使用 kerberos 啊。。

然后就想到了,这东西开源的应该有相关的单元测试吧。。啊哈哈哈哈哈哈,果然

https://github.com/apache/phoenix/blob/master/phoenix-core/src/test/java/org/apache/phoenix/jdbc/SecureUserConnectionsTest.java

private String joinUserAuthentication(String origUrl, String principal, File keytab) {
        StringBuilder sb = new StringBuilder(64);
        // Knock off the trailing terminator if one exists
        if (origUrl.charAt(origUrl.length() - 1) == PhoenixRuntime.JDBC_PROTOCOL_TERMINATOR) {
            sb.append(origUrl, 0, origUrl.length() - 1);
        } else {
            sb.append(origUrl);
        }

        sb.append(PhoenixRuntime.JDBC_PROTOCOL_SEPARATOR).append(principal);
        sb.append(PhoenixRuntime.JDBC_PROTOCOL_SEPARATOR).append(keytab.getPath());
        return sb.append(PhoenixRuntime.JDBC_PROTOCOL_TERMINATOR).toString();
    }

通过 url 拼接就行了!! 测试代码:

public class Main {
    public static void main(String[] args) throws ClassNotFoundException, LoginException {
        System.setProperty("java.security.krb5.conf", "/app/conf/krb5.conf");

        Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
        try (Connection connection = DriverManager.getConnection("jdbc:phoenix:node1,node2,node3:storm-miras:/app/conf/storm.headless.keytab");
             PreparedStatement preparedStatement = connection.prepareStatement("select * from phoenix_krb")) {
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                while (resultSet.next()) {
                    System.out.println(resultSet.getString("content"));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

  

1243 [main] WARN  o.a.h.u.Shell - Did not find winutils.exe: java.io.FileNotFoundException: java.io.FileNotFoundException: HADOOP_HOME and hadoop.home.dir are unset. -see https://wiki.apache.org/hadoop/WindowsProblems
1408 [main] INFO  o.a.p.q.ConnectionQueryServicesImpl - Trying to connect to a secure cluster with keytab:/app/conf/storm.headless.keytab
1485 [main] WARN  o.a.h.u.NativeCodeLoader - Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
1793 [main] INFO  o.a.h.s.UserGroupInformation - Login successful for user storm-miras using keytab file /app/conf/storm.headless.keytab
1793 [main] INFO  o.a.p.q.ConnectionQueryServicesImpl - Successfull login to secure cluster!!
1971 [main] INFO  o.a.h.h.z.RecoverableZooKeeper - Process identifier=hconnection-0x1e4d3ce5 connecting to ZooKeeper ensemble=node2:2181,node3:2181
11612 [main] WARN  o.a.h.h.s.DomainSocketFactory - The short-circuit local reads feature cannot be used because UNIX Domain sockets are not available on Windows.
11664 [main] INFO  o.a.p.m.Metrics - Initializing metrics system: phoenix
11728 [main] WARN  o.a.h.m.i.MetricsConfig - Cannot locate configuration: tried hadoop-metrics2-phoenix.properties,hadoop-metrics2.properties
11745 [main] INFO  o.a.h.m.i.MetricsSystemImpl - Scheduled snapshot period at 10 second(s).
11745 [main] INFO  o.a.h.m.i.MetricsSystemImpl - phoenix metrics system started
hello
world
2017-08-30 17:03:28,854 FATAL Unable to register shutdown hook because JVM is shutting down.
0: jdbc:phoenix:> select * from phoenix_krb;
+-----+----------+
| ID  | CONTENT  |
+-----+----------+
| 1   | hello    |
| 2   | world    |
+-----+----------+
2 rows selected (0.03 seconds)
0: jdbc:phoenix:> 
原文地址:https://www.cnblogs.com/kischn/p/7454049.html