从windows连接到kerberized hbase:未能指定服务器的kerberos主体名称

cwxwcias  于 2021-06-10  发布在  Hbase
关注(0)|答案(1)|浏览(341)

下面是我使用kerberos连接到hbase的代码:

Configuration conf = HBaseConfiguration.create();

conf.set("hbase.zookeeper.quorum", "20.20.100.1");
conf.set("hbase.zookeeper.property.clientPort", "2181");

final String USER_KEY = "appuser.principal";
final String KEYTAB_KEY = "appuser.keytab.filename";
final String Key_user = Config.getProperties().getString("kerberos_keyuser");
final String Key_tab = Config.getProperties().getString("kerberos_keytab");
conf.set("hadoop.security.authentication", "kerberos");
conf.set("hbase.security.authentication", "kerberos");
conf.set(USER_KEY, Key_user);
conf.set(KEYTAB_KEY, Key_tab);
SecurityUtil.login(conf, KEYTAB_KEY, USER_KEY);

Connection connection = ConnectionFactory.createConnection(conf);
try (Table table = connection.getTable(TableName.valueOf("newssentiment:test"))) {
    Scan scan = new Scan();
    ResultScanner scanner = table.getScanner(scan);
    Iterator<Result> iterator = scanner.iterator();
    System.out.println();
    while(iterator.hasNext()) {
        System.out.println(iterator.next());
    }
}
catch (Exception e){
    System.out.print(e.getMessage());
}

HBaseAdmin.checkHBaseAvailable(conf);
connection.close();
System.out.println("HBase is running!");

但我有以下错误:

org.apache.hadoop.security.UserGroupInformation - PriviledgedActionException as:u8000 (auth:SIMPLE) cause:java.io.IOException: Failed to specify server's Kerberos principal name
org.apache.hadoop.hbase.ipc.AbstractRpcClient - Exception encountered while connecting to the server : java.io.IOException: Failed to specify server's Kerberos principal name
org.apache.hadoop.security.UserGroupInformation - PriviledgedActionException as:u8000 (auth:SIMPLE) cause:java.io.IOException: java.io.IOException: Failed to specify server's Kerberos principal name

同样的代码可以在linux上成功运行,但在windows上总是失败。

fcg9iug3

fcg9iug31#

只要替换一下 SecurityUtil.login(conf, KEYTAB_KEY, USER_KEY);

UserGroupInformation.setConfiguration(conf);
UserGroupInformation.loginUserFromKeytab("USER_KEY", KEYTAB_KEY);

为简单起见,您可以按以下方式编写代码:

Configuration conf = HBaseConfiguration.create();

conf.addResource(new Path("D:\\core-site.xml");
conf.addResource(new Path("D:\\hbase-site.xml");

System.setProperty( "java.security.krb5.conf", "D:\\krb5.conf"); 
// give the path of krb5.conf file 

UserGroupInformation.setConfiguration(conf);
UserGroupInformation.loginUserFromKeytab("Dummy@EXAMPLE.COM", "D:\\farooque.keytab"); 
// Dummy@EXAMPLE.COM is the principal name, give the path of keytab file

Connection connection = ConnectionFactory.createConnection(conf);
try (Table table = connection.getTable(TableName.valueOf("newssentiment:test"))) {
    Scan scan = new Scan();
    ResultScanner scanner = table.getScanner(scan);
    Iterator<Result> iterator = scanner.iterator();
    System.out.println();
    while(iterator.hasNext()) {
        System.out.println(iterator.next());
    }
}
catch (Exception e){
    System.out.print(e.getMessage());
}

HBaseAdmin.checkHBaseAvailable(conf);
connection.close();
System.out.println("HBase is running!");

如果以上代码成功运行,您可以用变量替换固定值。

相关问题