hadoop

HDFS(三):HDFS API操作

2021-12-05  本文已影响0人  codeMover

首先新建一个maven工程,然后编辑pom文件,新增hadoop客户端以及junit的maven坐标。注意hadoop客户端版本号与服务器上的版本号一致。

  <dependencies>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-client</artifactId>
      <version>3.1.3</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
  </dependencies>

编写客户端测试文件,其中特别注意的是副本数量优先级设置问题。
hdfs-default.xml < hdfs-site.xml < resource配置文件优先级 < Configuration的配置
随着优先级逐步递增。

public class HdfsClient {

    private FileSystem fs;

    @Before
    public void init() throws Exception {
        URI uri = new URI("hdfs://localhost:8020");
        Configuration entries = new Configuration();
        entries.set("dfs.replication","2");
        fs = FileSystem.get(uri, entries, "hadoop");
    }

    @After
    public void close() throws Exception {
        fs.close();
    }

    /**
     * 创建目录
     */
    @Test
    public void testMkdir() throws Exception {
        fs.mkdirs(new Path("/xiyou/a.txt"));
    }

    /**
     * 参数副本数优先级
     * hdfs-default.xml < hdfs-site.xml < resource配置文件优先级 < Configuration的配置
     *
     */
    @Test
    public void testPut() throws IOException {
        boolean delSrc = false;
        boolean overwrite = false;
        Path src = new Path("./b.txt");
        Path dst = new Path("/xiyou");
        fs.copyFromLocalFile(delSrc,overwrite,src,dst);
    }

    /**
     * crc文件校验
     */
    @Test
    public void testGet() throws IOException {
        boolean delSrc = false;
        Path src = new Path("/xiyou/b.txt");
        Path dst = new Path("./");
        // 本地校验
        boolean useRawLocalFileSystem = false;
        fs.copyToLocalFile(delSrc,src,dst,useRawLocalFileSystem);
    }

    /**
     * 删除文件目录、递归深处
     */
    @Test
    public void testRm() throws IOException {
        Path path = new Path("/xiyou/b.txt");
        fs.delete(path,true);
    }

    /**
     * 文件目录移动、改名
     */
    @Test
    public void testMv() throws IOException {
        Path src = new Path("/xiyou/b.txt");
        Path dst = new Path("./");
        fs.rename(src,dst);
    }

    /**
     * 获取文件详情
     */
    @Test
    public void testDetail() throws IOException {
        Path src = new Path("/xiyou/b.txt");
        RemoteIterator<LocatedFileStatus> locatedFileStatusRemoteIterator = fs.listFiles(src, true);
        while (locatedFileStatusRemoteIterator.hasNext()){
            LocatedFileStatus locatedFileStatus = locatedFileStatusRemoteIterator.next();
            System.out.println(locatedFileStatus.getPermission());
            System.out.println(locatedFileStatus.getOwner());
            System.out.println(locatedFileStatus.getGroup());
            System.out.println(locatedFileStatus.getLen());
            System.out.println(locatedFileStatus.getModificationTime());
            System.out.println(locatedFileStatus.getReplication());
            System.out.println(locatedFileStatus.getBlockSize());
            System.out.println(locatedFileStatus.getPath().getName());
            BlockLocation[] blockLocations = locatedFileStatus.getBlockLocations();
            System.out.println(Arrays.asList(blockLocations));
        }
    }

    /**
     * 文件和文件夹判断
     */
    @Test
    public void testFile() throws IOException {
        FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
        for (FileStatus fileStatus : fileStatuses) {
            if(fileStatus.isFile()){
                System.out.println("文件:"+fileStatus.getPath().getName());
            }
            if(fileStatus.isDirectory()){
                System.out.println("目录:"+fileStatus.getPath().getName());
            }
        }
    }
}

小结

api操作,非重点。需要注意的是,副本优先级的问题,在代码Configuration的优先级最高。

上一篇 下一篇

猜你喜欢

热点阅读