java读取hdfs ha_java连接hdfs ha和调用mapreduce jar示例

本文提供了使用Java API连接HDFS高可用(HA)集群的示例,以及如何调用MapReduce程序。配置了HDFS的NameNode地址,并展示了如何在Java代码中设置HA相关参数。在读取HDFS文件后,还演示了如何通过Java调用MapReduce的wordcount任务,指定了YARN资源管理器和调度器地址。

Java API 连接 HDFS HA

public static void main(String[] args) {

Configuration conf = new Configuration();

conf.set("fs.defaultFS", "hdfs://hadoop2cluster");

conf.set("dfs.nameservices", "hadoop2cluster");

conf.set("dfs.ha.namenodes.hadoop2cluster", "nn1,nn2");

conf.set("dfs.namenode.rpc-address.hadoop2cluster.nn1", "10.0.1.165:8020");

conf.set("dfs.namenode.rpc-address.hadoop2cluster.nn2", "10.0.1.166:8020");

conf.set("dfs.client.failover.proxy.provider.hadoop2cluster",        "org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider");

FileSystem fs = null;

try {

fs = FileSystem.get(conf);

FileStatus[] list = fs.listStatus(new Path("/"));

for (FileStatus file : list) {

System.out.println(file.getPath().getName());

}

} catch (IOException e) {

e.printStackTrace();

} finally{

try {

fs.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

Java API调用MapReduce程序

String[] args = new String[24];

args[0] = “/usr/hadoop-2.2.0/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.2.0.jar";

args[1] = "wordcount";

args[2] = "-D";

args[3] = "yarn.resourcemanager.address=10.0.1.165:8032";

args[4] = "-D";

args[5] = "yarn.resourcemanager.scheduler.address=10.0.1.165:8030";

args[6] = "-D";

args[7] = "fs.defaultFS=hdfs://hadoop2cluster/";

args[8] = "-D";

args[9] = "dfs.nameservices=hadoop2cluster";

args[10] = "-D";

args[11] = "dfs.ha.namenodes.hadoop2cluster=nn1,nn2";

args[12] = "-D";

args[13] = "dfs.namenode.rpc-address.hadoop2cluster.nn1=10.0.1.165:8020";

args[14] = "-D";

args[15] = "dfs.namenode.rpc-address.hadoop2cluster.nn2=10.0.1.166:8020";

args[16] = "-D";

args[17] = "dfs.client.failover.proxy.provider.hadoop2cluster=org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider";

args[18] = "-D";

args[19] = "fs.hdfs.impl=org.apache.hadoop.hdfs.DistributedFileSystem";

args[20] = "-D";

args[21] = "mapreduce.framework.name=yarn";

args[22] = "/input";

args[23] = "/out01";

RunJar.main(args);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值