• hdfs文件操作的api


    package Utils;
    
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.LocatedFileStatus;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.fs.RemoteIterator;
    
    import java.io.IOException;
    import java.net.URI;
    import java.net.URISyntaxException;
    
    public class HdfsUtil {
        private FileSystem fs;
        HdfsUtil(String user){
            Configuration cfg = new Configuration();
            URI uri = null;
            try {
                uri = new URI("hdfs://**:9000");
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            try {
                // 根据配置文件,实例化成DistributedFileSystem
                fs = FileSystem.get(uri, cfg, user); // 得到fs句柄
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 上传文件
         */
        public void upload(String src, String dst){
            try {
                // 上传
                fs.copyFromLocalFile(new Path(src), new Path(dst));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 下载文件
         */
        public void download(String src, String dst){
            try {
                // 下载
                fs.copyToLocalFile(new Path(src), new Path(dst));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 创建文件夹
         */
        public void mkdir(String dir){
            try {
                fs.mkdirs(new Path(dir));
            } catch (IOException e) {
                // 创建目录
                e.printStackTrace();
            }
        }
    
        /**
         * 删除文件
         */
        public void delete(String name, boolean recursive){
            try {
                fs.delete(new Path(name), recursive);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 重命名
         */
        public void rename(String source, String dst){
            try {
                fs.rename(new Path(source), new Path(dst));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 列出文件信息
         */
        public void list(String dir, boolean recursive){
            try {
                RemoteIterator<LocatedFileStatus> iter = fs.listFiles(new Path(dir), recursive);
                while (iter.hasNext()){
                    LocatedFileStatus file = iter.next();
                    System.out.println(file.getPath().getName());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    https://necydcy.me/
  • 相关阅读:
    死锁
    不能复制文件到服务器
    JWT
    身份验证
    依赖注入
    ml.net
    swift 枚举、结构、类
    nginx 负载均衡
    sql 时间函数大全
    更新SVN时提示要清理,但清理失败,乱码得解决方案
  • 原文地址:https://www.cnblogs.com/miria-486/p/9962007.html
Copyright © 2020-2023  润新知