HDFS-API调用-基于MVC框架实现WEB操作记录

15 篇文章 0 订阅
13 篇文章 0 订阅

PS:完成时间是2016年3月23日,至今已有一段时间了,功能还未完善,忙于其它事情,所以难免有bug,只做一个砖头,引引玉吧!
Utils:

// 判断路径是否存在
    public static boolean isExistFile(String path, Configuration conf) throws Exception, IOException {
        FileSystem fs = FileSystem.get(conf);
        boolean res = fs.exists(new Path(path));
        return res;
    }

    public static boolean isDirectory(String path, Configuration conf) throws IOException{

        FileSystem fs = FileSystem.get(conf);
        boolean res = fs.isDirectory(new Path(path));
        return res;

    }

    // 上传文件
    public static void copyFromLocal(String localPath, String hdfsPath, Configuration conf) throws Exception {
        FileSystem fs = FileSystem.get(conf);
        fs.copyFromLocalFile(new Path(localPath), new Path(hdfsPath));
        fs.close(); // 需要关闭文件流

    }

    // 删除文件不递归删除
    public static void deleteFile(String hdfsPath, Configuration conf) throws Exception {
        FileSystem fs = FileSystem.get(conf);
        boolean res = fs.delete(new Path(hdfsPath), false);
        System.out.println(res ? "delete success" : "delete failed");
    }

    // 下载文件
    public static void downFromHdfs(String localPath, String hdfspath, Configuration conf) throws Exception {
        FileSystem fs = FileSystem.get(conf);
        fs.copyToLocalFile(new Path(hdfspath), new Path(localPath));
        fs.close();
    }

    // 创建文件夹
    public static boolean createDir(String dirName, Configuration conf) throws IOException {
        FileSystem fs = FileSystem.get(conf);
        Path dir = new Path(dirName);
        boolean res = fs.mkdirs(dir);
        fs.close();
        return res;
    }

    // 删除文件夹
    public static boolean deleteDir(String dirName, Configuration conf) throws Exception {
        FileSystem fs = FileSystem.get(conf);
        boolean res = fs.delete(new Path(dirName), true);
        return res;
    }

    // 文件重命名
    public static boolean reNameFile(String oldname, String rename, Configuration conf) throws Exception {
        FileSystem fs = FileSystem.get(conf);
        boolean res = fs.rename(new Path(oldname), new Path(rename));
        return res;
    }
    // 列出文件夹下文件

    public static RemoteIterator<LocatedFileStatus> readDir(String hdfspath, boolean recursive, Configuration conf)
            throws Exception {
        FileSystem fs = FileSystem.get(conf);
        RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path(hdfspath), recursive);
        return files;

    }

    // 读取文件内容
    public static String readFile(String hdfspath, Configuration conf) throws IOException {
        FileSystem fs = FileSystem.get(conf);
        InputStream in = null;
        OutputStream out = null;
        String fileContent = null;
        try {
            in = fs.open(new Path(hdfspath));
            out = new ByteArrayOutputStream(in.available());
            IOUtils.copyBytes(in, out, 4096);
            fileContent = out.toString();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        } finally {
            IOUtils.closeStream(in);
            IOUtils.closeStream(out);
        }
        return fileContent;
    }

    public static FileStatus[] listStatus(Configuration conf, String dirPath) throws IOException {
        FileSystem fs = FileSystem.get(conf);
        FileStatus[] fileStatuses = fs.listStatus(new Path(dirPath));
        fs.close();
        return fileStatuses;
    }

Controller:

@Controller
@RequestMapping("/data")
public class DataController {

    @RequestMapping(value="/home",method=RequestMethod.GET)
    public String indexHome(){
        return "index";
    }


    //显示HDFS下所有文件夹
    @RequestMapping(value="/list",method=RequestMethod.GET)
    public String listFiles(Configuration conf,String root_dir,Model model) throws Exception{
        List<FileInfo> files = new ArrayList<FileInfo>();
        root_dir = conf.get("fs.defaultFS");
        FileStatus[] filestatus = HDFSUtils.listStatus(conf, root_dir+"/");

        for(FileStatus s:filestatus){
            files.add(new FileInfo(s));
        }
        model.addAttribute("data", files);
        model.addAttribute("path", root_dir);
        return "list";
    } 
    //显示目录下的所有文件
    @RequestMapping(value="/show/{path}",method=RequestMethod.GET)
    public String listDir(Configuration conf,Model model,@PathVariable String path) throws IOException{
        List<FileInfo> files = new ArrayList<FileInfo>();
        String root_dir = conf.get("fs.defaultFS");
        /*if(path==null)
            path="/";*/
        FileStatus[] filestatus = HDFSUtils.listStatus(conf, root_dir+"/"+path);
        //System.out.println(path+"-----------------------");
        for(FileStatus s:filestatus){
            files.add(new FileInfo(s));
        }
        model.addAttribute("data", files);
        return "show";
    }
    //读取文件
    @RequestMapping(value="/showFile/{filepath}",method=RequestMethod.GET)
    public String readFile(Configuration conf,@PathVariable String filePath,Model model) throws IOException{
        String root_dir = conf.get("fs.defaultFS");
        String content = HDFSUtils.readFile(root_dir+"/"+filePath, conf);
        model.addAttribute("content", content);
        return "showFile";
    }


    @RequestMapping(value="delete/{path}",method=RequestMethod.POST)
    public String deleteFile(Configuration conf,@PathVariable String path) throws Exception{
        String root_dir = conf.get("fs.defaultFS");
        String path_t = root_dir+"/"+path;
        System.out.println("---------------"+path_t);
        boolean res = HDFSUtils.isDirectory(path_t,conf);
        if(res)
            HDFSUtils.deleteDir(path_t, conf);
        else
            HDFSUtils.deleteFile(path_t, conf);
        return "redirect:/data/list";
    }
}

DTO:

public class FileInfo {
    private FileStatus fileStatus;
    public FileInfo(FileStatus fileStatus) {
          this.fileStatus=fileStatus;
    }
    public FileStatus getFileStatus() {
        return fileStatus;
    }
    public void setFileStatus(FileStatus fileStatus) {
        this.fileStatus = fileStatus;
    }
    public String getPath(){
        return fileStatus.getPath().toUri().getPath();
    }
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    public Date getModificationTime(){
        return new Date(fileStatus.getModificationTime());
    }

    public long getLen(){
        return fileStatus.getLen();
    }

    public String getSize(){
        return convertToSize(fileStatus.getLen());
    }

    public short getReplication(){
        return fileStatus.getReplication();
    }

    public FsPermission getPermission(){
        return fileStatus.getPermission();
    }

    public String getOwner(){
        return fileStatus.getOwner();
    }


    public String getGroup(){
        return fileStatus.getGroup();

    }

    public String getBlockSize(){
        return convertToSize(fileStatus.getBlockSize());
    }

    private static String formatNumber(Number num, String pattern) {
        if (num == null) {
            return null;
        }
        NumberFormatter formatter = new NumberFormatter();
        formatter.setPattern(pattern);
        return formatter.print(num, Locale.SIMPLIFIED_CHINESE);
    }



    private String convertToSize(long b) {
        double unit = 1024;
        double k = 0;
        double m = 0;
        double g = 0;
        if (b < unit) {
            return b + " Byte";
        } else {
            k = b / unit;
            if (k < unit) {
                return formatNumber(k, "#.##") + " KB";
            } else {
                m = k / unit;
                if (m < unit) {
                    return formatNumber(m, "#.##") + " MB";
                } else {
                    g = m / unit;
                    return formatNumber(g, "#.##") + " GB";
                }
            }
        }
    }
}

Test:
主界面:


文件显示界面:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值