2016-09-22 67 views
0

这里是我的代码使用Kerberos连接到HBase的:连接从Windows Kerberos的HBase的:无法指定服务器的Kerberos主体名称

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上始终失败。

+0

我发现它可以修复,当我把core-site.xml和hbase-site.xml放入资源目录。但我不想将这两个文件添加到我的项目中。所以,我想我需要在Windows上的HBaseConfiguration中设置更多参数。 –

+0

在Windows上很奇怪。尽管我使用Java API和core-site.xml和hbase-site.xml中的内容设置了相同的参数,但它仍然失败。所以我必须将这两个xml文件放在我的Windows上。 –

回答

1

只是

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

为了简单更换SecurityUtil.login(conf, KEYTAB_KEY, USER_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("[email protected]", "D:\\farooque.keytab"); 
// [email protected] 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!"); 

如果上面的代码运行成功,你可以通过使用变量代替固定值。