HDFS文件上传下载HDFSfile.java用于复制
  oIa1edJoFmXP 2023年11月02日 54 0

 

HDFSfile.java

package com.imddysc;


/************************************************************
 FileName: HDFSfile.java
 Description:以通过hadoop中的fileSystem API进行文件的操作// 模块描述
 实现了对hdfs文件的大部分操作
 Function List:     // 主要函数及其功能
 1 创建目录mkdir("/idea/");
 2.创建文件create("/idea/haha.txt");
 3.查看hdfs文件内容read("/idea/text.txt");
 4文件重命名moveFile("/idea/haha.txt","/idea/hello.txt");
 5.上传文件putFile("G://text.txt","/idea/");
 6.下载文件getFile("/idea/abc.txt","G://");
 7.查询目录下的所有文件listStatus("/idea/");
 8.删除文件deleteFile("/idea/hello.txt");
 ***********************************************************/

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.*;
import java.net.URI;

public class HdfsFile {
    Configuration conf;
    FileSystem filesystem;
    String DEFNAME = "fs.defaultFS";
    String HDFSURL = "hdfs://10.193.196.160:12000";

    @Before
    public void before() throws IOException, InterruptedException {
        conf = new Configuration();
        conf.set(DEFNAME, HDFSURL);
//        filesystem = FileSystem.get(conf);
        // 指定用户
        filesystem = FileSystem.get(URI.create(HDFSURL), conf, "yinquan.zhang");

    }

    /**
     * junit测试函数
     *
     * @throws IOException
     */
    @Test
    public void Text() throws IOException {
        //创建目录
//        mkdir("cloud");

        //创建文件
//        create("/idea/haha.txt");

        //查看hdfs文件内容
        //read("/idea/text.txt");

        //文件重命名
        //moveFile("/idea/haha.txt","/idea/hello.txt");

        //上传文件
//        putFile("E:/codes/a.txt","cloud/");

        //下载文件
        getFile("cloud/a.txt","E:/codes/");

        //查询目录下的所有文件
        //listStatus("/idea/");

        //删除文件
        //deleteFile("/idea/hello.txt");
    }

    /**
     * 创建目录
     *
     * @param path 创建目录的地址(例:/hadoop/)
     * @throws IOException
     */
    public void mkdir(String path) throws IOException {
        //创建hdfs目录
        if (filesystem.exists(new Path(path))) {
            System.out.println("目录已存在");
        } else {
            boolean result = filesystem.mkdirs(new Path(path));
            System.out.println(result);
        }

    }

    /**
     * 创建文件
     *
     * @param path hdfs文件地址(例:/hadoop/abc.txt)
     * @throws IOException
     */
    public void create(String path) throws IOException {
        //创建文件
        if (filesystem.exists(new Path(path))) {
            System.out.println("文件已存在");
        } else {
            FSDataOutputStream outputStream = filesystem.create(new Path(path));
            System.out.println("文件创建成功");
        }
    }

    /**
     * 查看文件内容
     *
     * @param dst hdfs文件地址(例:/hadoop/abc.txt)
     * @throws IOException
     */
    public void read(String dst) throws IOException {
        if (filesystem.exists(new Path(dst))) {
            FSDataInputStream inputstream = filesystem.open(new Path(dst));
            InputStreamReader isr = new InputStreamReader(inputstream);
            BufferedReader br = new BufferedReader(isr);
            String str = br.readLine();
            while (str != null) {
                System.out.println(str);
                str = br.readLine();
            }
            br.close();
            isr.close();
            inputstream.close();
        } else {
            System.out.println("文件不存在");
        }
    }

    /**
     * 将dst1重命名为dst2,也可以进行文件的移动
     *
     * @param oldpath 旧名
     * @param newpath 新名
     */
    public void moveFile(String oldpath, String newpath) {
        Path path1 = new Path(oldpath);
        Path path2 = new Path(newpath);
        try {
            if (!filesystem.exists(path1)) {
                System.out.println(oldpath + " 文件不存在!");
                return;
            }
            if (filesystem.exists(path2)) {
                System.out.println(newpath + "已存在!");
                return;
            }
            // 将文件进行重命名,可以起到移动文件的作用
            filesystem.rename(path1, path2);
            System.out.println("文件已重命名!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 上传文件到hdfs
     *
     * @param local
     * @param dst
     */
    public void putFile(String local, String dst) {
        try {
            // 从本地将文件拷贝到HDFS中,如果目标文件已存在则进行覆盖
            filesystem.copyFromLocalFile(new Path(local), new Path(dst));
            System.out.println("上传成功!");
            // 关闭连接
        } catch (IOException e) {
            System.out.println("上传失败!");
            e.printStackTrace();
        }
    }

    /**
     * 下载文件到本地
     *
     * @param dst
     * @param local
     */
    public void getFile(String dst, String local) {
        try {
            if (!filesystem.exists(new Path(dst))) {
                System.out.println("文件不存在!");
            } else {
                filesystem.copyToLocalFile(false, new Path(dst), new Path(local), true);
                System.out.println("下载成功!");
            }
        } catch (IOException e) {
            System.out.println("下载失败!");
            e.printStackTrace();
        }
    }


    /**
     * 显示目录下所有文件
     *
     * @param dst
     */
    public void listStatus(String dst) {
        try {
            if (!filesystem.exists(new Path(dst))) {
                System.out.println("目录不存在!");
                return;
            }
            // 得到文件的状态
            FileStatus[] status = filesystem.listStatus(new Path(dst));
            for (FileStatus s : status) {
                System.out.println(s.getPath().getName());
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 删除hdfs中的文件
     *
     * @param dst
     */
    public void deleteFile(String dst) {
        try {
            if (!filesystem.exists(new Path(dst))) {
                System.out.println("文件不存在!");
            } else {
                filesystem.delete(new Path(dst), true);
                System.out.println("删除成功!");
            }
        } catch (IOException e) {
            System.out.println("删除失败!");
            e.printStackTrace();
        }
    }


    /**
     * 关闭filesyatem
     */
    @After
    public void destory() {
        try {
            filesystem.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
oIa1edJoFmXP