hdfs-javaAPI操作

创建一个普通的java项目

导入jar包

附:jar包如何来的

代码阶段

上传下载文件

package com.sxuek;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.net.URI; // 是net包下的URI
import java.net.URISyntaxException;

/*
操作HDFS
 */
public class Demo {
    public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {
        // 1.创建一个HDFS的连接配置对象 -- 指定本地里连接的参数
        // 参数是hdfs-site.xml中配置的参数
        Configuration conf = new Configuration();
        // 设置配置项 -- hdfs的地址
        // conf.set("fs.defaultFS", "hdfs://192.168.200.225:9000");
        // 在该目录下C:\Windows\System32\drivers\etc,添加ip--名 即可写下面的了
//        conf.set("fs.defaultFS", "hdfs://node1:9000");
        // 连接文件系统
        // uri:hdfs地址  conf:配置
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://node1:9000"), conf, "root");

        // 设置块的大小
        // 在代码中的配置的参数优先级高于我们Hadoop软件安装的时候配置的参数
        // conf.set("dfs.blocksize", "104857600");

        // 上传文件 并且不能删除本地文件
        fileSystem.copyFromLocalFile(false, new Path("G:\\shixun\\test1.txt"), new Path("/newDirectory/c"));
        System.out.println("上传完成!");

        // 下载文件 从hdfs上下载文件
//        fileSystem.copyToLocalFile(false, new Path("/hadoop-2.8.5.tar.gz"), new Path("G://"));
//        System.out.println("下载完成!");

        System.out.println(fileSystem);
    }
}

操作java-api的方法

package com.sxuek;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.codehaus.jackson.map.util.ISO8601Utils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/*

 【重点记忆】
  listFiles和listStatus方法的区别:
  1、listFiles代表的是查找当前路径下的所有文件,不包括文件夹,而且可以设置递归查找去把子文件夹中的文件也找出来,而listStatus代表的是查找当前路径下的所有文件和文件夹,不能找子文件夹中的内容
  2、listFiles返回的是一个类似于集合的数据类型,而listStatus返回的是一个数组
  3、listFiles需要两个参数,第二参数代表是否查找子文件夹中的内容
    listStatus只有一个参数


按道理来说,path如果是HDFS路径,应该是这样写路径hdfs://node1:9000/a/b
但是由于init方法中已经指定了路径,所以在test方法中直接写路径即可
 */
public class TestHDFS {
    public FileSystem fileSystem;

    @Before
    public void init() {
        Configuration conf = new Configuration();
        try {
            fileSystem = this.fileSystem.get(new URI("hdfs://node1:9000"), conf, "root");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    // 创建文件夹
    @Test
    public void test() {
        // 等同于mkdir -p, 即可以递归创建
        try {
            boolean mkdirs = fileSystem.mkdirs(new Path("/a/b/c"));
            System.out.println(mkdirs);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    // 删除文件或文件夹
    @Test
    public void test1() {
        try {
            // 第二个参数是false,代表如果指定路径下还有文件夹就不删除了
             boolean delete = fileSystem.delete(new Path("/a/b/c"), false);
//            boolean delete = fileSystem.delete(new Path("/a"),true);
            System.out.println(delete);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 判断指定的HDFS路径是否存在
    @Test
    public void test2() {
        try {
            boolean exists = fileSystem.exists(new Path("/parent"));
            System.out.println(exists);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 判断指定的HDFS路径是文件还是目录
    @Test
    public void test3() {
        try {
            boolean isFile = fileSystem.isFile(new Path("/a"));
            boolean isDirectory = fileSystem.isDirectory(new Path("/a"));
            System.out.println(isFile);
            System.out.println(isDirectory);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 文件名的更改
    @Test
    public void test4() {
        boolean rename = false;
        try {
            rename = fileSystem.rename(new Path("/a"), new Path("/newDirectory"));
            System.out.println(rename);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 查看某个文件的详细信息
    @Test
    public void test5() {
        try {
            FileStatus fileStatus = fileSystem.getFileStatus(new Path("/newDirectory"));
            /**
             * fileStatus类中封装很多用于获取文件信息的方法
             */
            System.out.println("文件的作者为"+fileStatus.getOwner());
            System.out.println("文件的所属组为"+fileStatus.getGroup());
            System.out.println("文件的权限为"+fileStatus.getPermission());
            System.out.println("文件的路径为"+fileStatus.getPath());
            System.out.println("文件的最后一次修改时间为"+fileStatus.getModificationTime());
            System.out.println("文件是否为文件"+fileStatus.isFile());
            System.out.println("文件是否为目录"+fileStatus.isDirectory());
            System.out.println("文件的block块的大小为"+fileStatus.getBlockSize());
            System.out.println("文件的大小为"+fileStatus.getLen());
            System.out.println("文件的副本数为"+fileStatus.getReplication());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    /**
     * 查看某个目录下的所有文件(不获取文件夹),包括子目录中的所有文件
     *      listFiles(Path,boolean)
     *      第二个参数:boolean代表的是否查找子文件夹中的文件
     *      true表示递归子文件夹中的文件
     *      false表示不递归子文件夹中的文件
     */
    @Test
    public void test6() throws IOException {
        RemoteIterator<LocatedFileStatus> list = fileSystem.listFiles(new Path("/p"), true);
        while (list.hasNext()){
            LocatedFileStatus status = list.next();
            System.out.println("文件路径"+status.getPath());
        }
    }

    // 查看某个目录下的所有文件和文件夹信息(不获取文件夹里的内容),不包括子目录的所有文件和文件夹
    @Test
    public void test7() {
        try {
            /*
                存放的是当前路径下的所有的文件和文件夹,不包括子文件中的内容
             */
            FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/newDirectory"));
            for (FileStatus fileStatus: fileStatuses) {
                System.out.println(fileStatus.getPath());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @After
    public void destory() {
        if (fileSystem != null) {
            try {
                fileSystem.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

练习题

package com.sxuek;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
/*
   作业:指定一个HDFS路径,然后将这个路径下的所有文件和文件夹打印出来,包括子文件中的文件和文件夹
    */
public class Practice {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        try {
            FileSystem fileSystem = FileSystem.get(new URI("hdfs://node1:9000"), conf, "root");
            FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/newDirectory"));
            find(fileSystem, fileStatuses);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    public static void find(FileSystem fileSystem, FileStatus[] fileStatuses) {
        for (FileStatus fileStatus : fileStatuses) {
            Path path = fileStatus.getPath();
            try {
                if (fileSystem.isDirectory(path)) {
                    System.out.println(path);
                    FileStatus[] fileStatuses1 = fileSystem.listStatus(path);
                    find(fileSystem, fileStatuses1);
                } else {
                    System.out.println(path);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
posted @ 2022-07-21 09:49  jsqup  阅读(91)  评论(0编辑  收藏  举报