2016-09-22 47 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!"); 

如果上面的代碼運行成功,你可以通過使用變量代替固定值。