Java API操作HA方式下的Hadoop

  

通过java api连接Hadoop集群时,如果集群支持HA方式,那么可以通过如下方式设置来自动切换到活动的master节点上。其中,ClusterName 是可以任意指定的,跟集群配置无关,dfs.ha.namenodes.ClusterName也可以任意指定名称,有几个master就写几个,后面根据相应的设置添加master节点地址即可。

	static String ClusterName = "nsstargate";
	private static final String HADOOP_URL = "hdfs://"+ClusterName;
	public static Configuration conf;

    static {
        conf = new Configuration();
        conf.set("fs.defaultFS", HADOOP_URL);
        conf.set("dfs.nameservices", ClusterName);
        conf.set("dfs.ha.namenodes."+ClusterName, "nn1,nn2");
        conf.set("dfs.namenode.rpc-address."+ClusterName+".nn1", "172.16.50.24:8020");
        conf.set("dfs.namenode.rpc-address."+ClusterName+".nn2", "172.16.50.21:8020");
        //conf.setBoolean(name, value);
        conf.set("dfs.client.failover.proxy.provider."+ClusterName, 
        		"org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider");
        conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
    }

    

上传文件到HDFS的代码如下,至于读取等其他操作,可以参考网络上其他文章。

    /**
     * 上传文件到HDFS上去
     */
    private static void uploadToHdfs() throws IOException {
        String localSrc = "E:\\test\\article01.txt";
        String dst = "/user/test/article04.txt";
        FileSystem fs = FileSystem.get(URI.create(HADOOP_URL), conf);
        long start = new Date().getTime();

       /* InputStream in = new FileInputStream(localSrc);
        InputStreamReader isr = new InputStreamReader(in, "GBK");
        OutputStream out = fs.create(new Path(HADOOP_URL+dst), true);
        IOUtils.copy(isr, out, "UTF8");*/
        
        //该方法更快
		FSDataOutputStream outputStream=fs.create(new Path(dst));
		String fileContent = FileUtils.readFileToString(new File(localSrc), "GBK");
		outputStream.write(fileContent.getBytes());
        outputStream.close();
        
        long end = new Date().getTime();
        System.out.println("use:"+(end-start));
        
    }

注:如果需要指定hdfs目录或者文件的权限、属主,可以使用如下代码:

System.setProperty("HADOOP_USER_NAME", "username"); //指定属主
fs.setPermission(path, new FsPermission(FsAction.ALL, FsAction.ALL, FsAction.ALL)); //给文件path设置权限

//以下方式也可以设置文件属主
FileSystem fs = FileSystem.get(URI.create(hdfsFile), conf, "hdfs");

另外也可以使用如下方式写文件:

        FSDataOutputStream fos = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        String path = "/tmp/aa.txt";
        String data="这是需要写入的数据";
        try{
            Path hdfsPath = new Path(path);
            FileSystem hdfs = FileSystem.get(conf);
            fos = hdfs.create(hdfsPath);
            //指定文件编码
            osw = new OutputStreamWriter(fos, "UTF-8");
            bw = new BufferedWriter(osw);
            bw.append(data);
            bw.flush();
            log.info("write data to hdfs finish.");
        }finally {
            closeIO(bw,osw,fos);
        }

使用到的jar包如下图,大部分都可以在hadoop的安装包里找到,也可以在maven上下载。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值