【HDFS】hadoop2.x HDFS javaAPI

来源:互联网 发布:进销存数据库设计 编辑:程序博客网 时间:2024/05/01 09:17

Configuration conf = new Configuration();

conf.set("fs.defaultFS""hdfs://h6:9000");

FileSystem fileSystem = FileSystem.get(conf);

 

 

 

 

 

1.创建文件夹:

判断是否存在

不存在再创建

if (!fileSystem.exists(new Path("/weir01"))) {

        fileSystem.mkdirs(new Path("/weir01"));

      }

 

2.创建文件:

in - InputStream to read from 原文件路径

out - OutputStream to write to  hdfs 目录

the size of the buffer  缓冲大小

close - whether or not close the InputStream and OutputStream at the end. The streams are closed in the finally clause. 是否关闭流

 

 

 

FSDataOutputStream out =fileSystem.create(new Path("/d1"));

      FileInputStream in = new FileInputStream("f:/hadoop.zip");

      IOUtils.copyBytes(in, out, 1024, true);

 

 

3上传本地文件

 

delSrc - whether to delete the src是否删除源文件

overwrite - whether to overwrite an existing file是否覆盖已存在的文件

srcs - array of paths which are source 可以上传多个文件数组方式

dst – path 目标路径

 

fileSystem.copyFromLocalFile(src, dst);

      fileSystem.copyFromLocalFile(delSrc, src, dst);

      fileSystem.copyFromLocalFile(delSrc, overwrite, src, dst);

   fileSystem.copyFromLocalFile(delSrc, overwrite, srcs, dst);

 

 

4 重命名HDFS文件

 

fileSystem.rename(src, dst);

 

5.删除文件

 

True 表示递归删除

fileSystem.delete(new Path("/d1"), true);

 

6.查看目录及文件信息

 

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

      for (FileStatus f : fs) {

        String dir = f.isDirectory() ? "目录":"文件";

        String name = f.getPath().getName();

        String path = f.getPath().toString();

        System.out.println(dir+"----"+name+"  path:"+path);

        System.out.println(f.getAccessTime());

        System.out.println(f.getBlockSize());

        System.out.println(f.getGroup());

        System.out.println(f.getLen());

        System.out.println(f.getModificationTime());

        System.out.println(f.getOwner());

        System.out.println(f.getPermission());

        System.out.println(f.getReplication());

        System.out.println(f.getSymlink());

      }

 

 

7.查找某个文件在HDFS集群的位置

 

FileStatus fs = fileSystem.getFileStatus(new Path("/data"));

      BlockLocation[] bls=fileSystem.getFileBlockLocations(fs, 0, fs.getLen());

      for (int i = 0,h=bls.length; i < h; i++) {

        String[] hosts= bls[i].getHosts();

        System.out.println("block_"+i+"_location:  "+hosts[0]);

      }

 

 

8.获取HDFS集群上所有节点名称信息

 

 

DistributedFileSystem hdfs = (DistributedFileSystem) fileSystem;

      DatanodeInfo[] dns=hdfs.getDataNodeStats();

      for (int i = 0,h=dns.length; i < h; i++) {

        System.out.println("datanode_"+i+"_name:  "+dns[i].getHostName());

      }

 

0 0
原创粉丝点击