kerberos下JAVA代码操作hbase的方式(客户端方式,应用程序方式)
2018-03-07 09:07 staryea-bigdata 阅读(3018) 评论(0) 编辑 收藏 举报(一)如果部署JAVA 代码的主机用户获取了kerberos权限情况下
假设主机名是:client
su - client 登录主机后
kinit -kt /keytab的路径/client.keytab client@STARYEA.COM
编写的java代码入下:
public static void main(String[] args) throws IOException, InterruptedException {
conf = HBaseConfiguration.create();
conf.addResource("hbase-site.xml");
conf.addResource("core-site.xml");
conf.addResource("hdfs-site.xml");
--代码正常写不需要添加任何东西
conn = ConnectionFactory.createConnection(conf);
createTable("client:test_table_0204");
insertData("clienttest_table_0204");
QueryAll("client:test_table_0204");
conn.close();
}
(二)以应用程序的方式获取kerberos认证的情况下
假设主机名是:client
su - client 登录主机后
kdestory --删除主机的凭证,在这种情况下 JAVA代码如下
public static void main(String[] args) throws IOException,
InterruptedException {
conf = HBaseConfiguration.create();
conf.addResource("hbase-site.xml");
conf.addResource("core-site.xml");
conf.addResource("hdfs-site.xml");
--需要添加kerberos认证
String USER = "client@STARYEA.COM";
String KEYTAB = "/etc/security/keytabs/client.keytab";
--APP需要kerberos认证
UserGroupInformation ugi = UserGroupInformation.loginUserFromKeytabAndReturnUGI(USER, KEYTAB);
ugi.doAs(new PrivilegedExceptionAction<Void>() {
public Void run() {
try {
conn = ConnectionFactory.createConnection(conf);
createTable("client:test_table_0204");
insertData("client:test_table_0204");
QueryAll("client:test_table_0204");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
});
conn.close();
}