Java API 操作 HDFS

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.*;
import java.net.URI;
import java.util.Date;

/**
 * 实验
 */
public class experiment {

    private FileSystem fileSystem;

    @Before
    public void init() throws Exception {
        URI uri = new URI("hdfs://hadoop102:8020");
        fileSystem = FileSystem.get(uri, new Configuration());
    }

    @After
    public void close() throws IOException {
        fileSystem.close();
    }

    /**
     * 上传文件:
     *      如果文件不存在,就直接上传;
     *      如果文件存在,由用户决定是覆盖原文件还是追加到原文件末尾
     */
    @Test
    public void put() throws IOException {
        Path local = new Path("D:\\data\\a.txt");
        Path dfs = new Path("/input/a.txt");
        // 判断文件是否已经存在
        boolean exists = fileSystem.exists(dfs);
        if (exists) {
            // 已经存在,由用户决定是否覆盖
//            Scanner scanner = new Scanner(System.in);
//            String s = scanner.nextLine();
            String s = "true";
            if ("true".equals(s)) {
                // 覆盖
                fileSystem.copyFromLocalFile(false, true, local, dfs);
            } else {
                FileInputStream fileInputStream = new FileInputStream("D:\\data\\a.txt");
                BufferedReader reader = new BufferedReader(new InputStreamReader(fileInputStream));
                // 追加
                FSDataOutputStream outputStream = fileSystem.append(dfs);
                String line = reader.readLine();
                outputStream.writeUTF(line);
                // close stream
                fileInputStream.close();
                outputStream.close();
            }
        } else {
            // 不存在:直接上传
            fileSystem.copyFromLocalFile(local, dfs);
        }
    }

    /**
     * 下载文件到本地,如果本地已存在,则对文件重命名
     */
    @Test
    public void get() throws IOException {
        // 要下载的文件
        Path dfs = new Path("/input/a.txt");
        // 判断本地是否存在
        File file = new File("D:\\data\\a.txt");
        boolean exists = file.exists();
        if (exists) {
            // 已存在,重命名
            System.out.println("a.txt already exist!");
            Path local = new Path("D:\\data\\a_" + new Date().getTime() + ".txt");
            fileSystem.copyToLocalFile(dfs, local);
        } else {
            // 不存在
            Path local = new Path("D:\\data\\a.txt");
            fileSystem.copyToLocalFile(dfs, local);
        }
    }

    /**
     * 输出hdfs中的文件内容到控制台
     */
    @Test
    public void cat() throws IOException {
        FSDataInputStream inputStream = fileSystem.open(new Path("/in/hello.txt"));
        IOUtils.copyBytes(inputStream, System.out, new Configuration());
    }

    /**
     * 显示指定文件的权限、大小、创建时间、路径等信息
     */
    @Test
    public void fileInfo() throws IOException {
        FileStatus fileStatus = fileSystem.getFileStatus(new Path("/input/a.txt"));
        printInfo(fileStatus);
    }

    // 辅助打印文件信息
    private void printInfo(FileStatus fileStatus) {
        System.out.println("***********************************");
        System.out.println(fileStatus.getPath());
        System.out.println(fileStatus.getOwner());
        System.out.println(fileStatus.getPermission());
        System.out.println(fileStatus.getLen());
        System.out.println(fileStatus.getBlockSize() / 1024 / 1024 + "M");
        System.out.println(fileStatus.getGroup());
        System.out.println(fileStatus.getReplication());
        System.out.println(fileStatus.getAccessTime());
        System.out.println(fileStatus.getModificationTime());
    }

    /**
     * 显示目录下所有文件的信息
     */
    @Test
    public void dirInfo() throws IOException {
        // recursive:是否递归遍历
        RemoteIterator<LocatedFileStatus> iterator = fileSystem.listFiles(new Path("/input"), true);
        while (iterator.hasNext()) {
            LocatedFileStatus fileStatus = iterator.next();
            printInfo(fileStatus);
        }
    }

    /**
     * 创建目录
     */
    @Test
    public void createDir() throws IOException {
        Path path = new Path("/dir");
        boolean exists = fileSystem.exists(path);
        if (exists) {
            System.out.println("dir already exist!");
        } else {
            boolean mkdirs = fileSystem.mkdirs(path);
            if (mkdirs) {
                System.out.println("make success");
            } else {
                System.out.println("make fail");
            }
        }
    }

    /**
     * 删除目录
     */
    @Test
    public void deleteDir() throws IOException {
        Path path = new Path("/dir");
        boolean exists = fileSystem.exists(path);
        if (exists) {
            // recursive:是否递归删除
            fileSystem.delete(path, true);
        } else {
            System.out.println("dir not exist");
        }
    }

    /**
     * 创建文件
     */
    @Test
    public void createFile() throws IOException {
        Path path = new Path("/dir/a.txt");
        boolean exists = fileSystem.exists(path);
        if (exists) {
            System.out.println("file already exist!");
        } else {
            boolean newFile = fileSystem.createNewFile(path);
            if (newFile) {
                System.out.println("success");
            }
        }
    }

    /**
     * 删除文件
     */
    @Test
    public void deleteFile() throws IOException {
        Path path = new Path("/dir/a.txt");
        boolean exists = fileSystem.exists(path);
        if (exists) {
            boolean delete = fileSystem.delete(path, false);
        } else {
            System.out.println("file not exist");
        }
    }

    /**
     * 追加内容到文件的开头或末尾
     */
    @Test
    public void append() throws IOException {
        Path path = new Path("/dir/a.txt");
        boolean tail = false;
        if (tail) {
            // 追加内容到文件的末尾
            FSDataOutputStream outputStream = fileSystem.append(path);
            IOUtils.copyBytes(new ByteArrayInputStream("-tail content".getBytes()), outputStream, new Configuration());
        } else {
            // 追加到文件的开头
            Path tmp = new Path("D:\\data\\tmp.txt");
            // 先把dfs的文件move到local
            fileSystem.moveToLocalFile(path, tmp);
            // 新建该文件
            createFile();
            // 追加用户的输入
            FSDataOutputStream outputStream = fileSystem.append(path);
            IOUtils.copyBytes(new ByteArrayInputStream("-head content\n".getBytes()), outputStream, new Configuration());
            // 追加原先的内容
            FileInputStream fileInputStream = new FileInputStream("D:\\data\\tmp.txt");
            outputStream = fileSystem.append(path);
            IOUtils.copyBytes(fileInputStream, outputStream, new Configuration());
        }
    }

    /**
     * 移动文件
     */
    @Test
    public void move() throws IOException {
        fileSystem.rename(new Path("/dir/a.txt"), new Path("/in/a.txt"));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

beOkWithAnything

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值