private static Configuration getHdfsConf() {
String userKeytabPath = PropertiesUtil.getProperty("hive.user.keytab.path");
String userPrincipal = PropertiesUtil.getProperty("hive.user.principal");
Configuration conf = new Configuration();
conf.set("hadoop.security.authentication", "kerberos");
conf.set("hdfs.keytab.file", userKeytabPath);
conf.set("fs.defaultFS", "hdfs://hdfscluster");
conf.set("dfs.nameservices", "hdfscluster");
conf.set("dfs.ha.namenodes.hdfscluster", "nn1,nn2");
conf.set("dfs.namenode.rpc-address.hdfscluster.nn1", "nn-01.hadoop.citic:8020");
conf.set("dfs.namenode.rpc-address.hdfscluster.nn2", "nn-02.hadoop.citic:8020");
conf.set("dfs.client.failover.proxy.provider.hdfscluster", "org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider");
String krb5Path = PropertiesUtil.getProperty("hive.krb5.path");
System.setProperty("hadoop.home.dir", "C:/ods-parities-server/hadoop-2.6.1");
System.setProperty("java.security.krb5.conf", krb5Path);
UserGroupInformation.setConfiguration(conf);
try {
UserGroupInformation.loginUserFromKeytab(userPrincipal, userKeytabPath);
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getMessage());
}
return conf;
}
说明:
其中String krb5Path =......System.setProperty("java.security.krb5.conf", krb5Path);为krb5认证。
其中hdfscluster字符串为集群配置文件hdfs-site.xml中的配置项dfs.nameservices的值。
其中dfs.ha.namenodes.hdfscluster、dfs.namenode.rpc-address.hdfscluster.nn1、dfs.namenode.rpc-address.hdfscluster.nn2、dfs.client.failover.proxy.provider.hdfscluster的hdfscluster字符串也是集群配置文件hdfs-site.xml中的配置项dfs.nameservices的值。
其中dfs.client.failover.proxy.provider.hdfscluster配置项对应的类应该是确保返回高可用中处于active状态的namenode节点。