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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值