前言
hadoopV2版本及以后都是HA架构,active namenode和standby namenode, 当前active namenode 出现问题之后,zkfc会切换standby namenode 为active 状态保证hadoop集群正常运行,所以java api需要通过配置HA的方式来访问hdfs集群;
使用方式
对于namenode是HA高可用集群时,客户端远程访问hdfs有两种实现方法:
(1)将所有namenode ha相关的参数写入Configuration对象中
(2)将配置文件core-site.xml和hdfs-site.xml文件复制到项目的目录下
使用方式一样例代码:
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class HADemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://HACluster");
conf.set("dfs.nameservices", "HACluster");
conf.set("dfs.ha.namenodes.HACluster", "nn1,nn2");
conf.set("dfs.namenode.rpc-address.HACluster.nn1", "namenode1_ip:8020");
conf.set("dfs.namenode.rpc-address.HACluster.nn2", "namenode2_ip:8020");
conf.set("dfs.client.failover.proxy.provider.HACluster",
"org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider");
try {
FileSystem fs = FileSystem.get(new URI("hdfs://HACluster"), conf,"root");
boolean b=fs.mkdirs(new Path("/tmp/a"));
fs.copyFromLocalFile(new Path("D:\\test\\a.txt"), new Path("/tmp/a/a.txt"));
fs.close();
} catch (Exception e) {
System.out.println(e);
}
}
}