HDFS常用方法(Java版本)

本文介绍了Hadoop分布式文件系统(HDFS)的基本操作方法,包括创建目录、文件、复制文件、移动文件、列出文件目录及获取文件元数据等,并提供了详细的Java代码实现。

HDFS是Hadoop文件分布式系统,常用的HDFS操作方法如下:

//创建FileSystem 对象
Configuration conf = new Configuration();
URI uri = new URI("hdfs://hadoop001:9000");//在core-site.xml中配置
FileSystem fs = FileSystem.get(uri, conf, "root");
//创建目录
public static void mkdir(String dirName) {
    if (!checkFileExist(dirName)) {
        try {
            Path f = new Path(dirName);
            fs.mkdirs(f);
        } catch (Exception e) {
            e.printStackTrace();
    }
}
//判断文件是否存在
private static boolean checkFileExist(String dirName) {
    Path path = new Path(dirName);
    try {
        return fs.exists(path);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}
//创建文件
public static void mkfile(String filePath) {
    try {
        Path f = new Path(filePath);
        FSDataOutputStream os = fs.create(f, true);
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
// 本地文件复制到hdfs
public static void copyFromLocalFile(String srcfile, String desfile) {
    Path src = new Path(srcfile);
    Path des = new Path(desfile);
    try {
        fs.copyFromLocalFile(src, des);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
// 从hdfs复制到本地
public static void copyToLocalFile(String srcfile, String desfile) {
    Path src = new Path(srcfile);
    Path des = new Path(desfile);
    try {
        fs.copyToLocalFile(src, des);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

注意:此方法直接执行会报如下错误则需要做如下操作

org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.hdfs.server.namenode.SafeModeException): Zero blocklocations for /test3/1.txt. Name node is in safe mode.
It was turned on manually. Use "hdfs dfsadmin -safemode leave" to turn safe mode off.

原因:启动了namenode,未启动datanode,则文件系统处于安全模式,只可以查看文件不能读写文件
解决办法:hadoop dfsadmin -safemode leave

//本地文件移动到hdfs
public static void move(String srcfile, String desfile) {
    Path src = new Path(srcfile);
    Path des = new Path(desfile);
    try {
        fs.moveFromLocalFile(src, des);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
// 列出某个路径下的文件
public static void listFileDirectory(String path) {
    Path p = new Path(path);
    try {
        listStatus = fs.listStatus(p);
        Path[] paths = FileUtil.stat2Paths(listStatus);
        for (Path filepath : paths) {
            System.out.println(filepath.toString());
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
// 获取某个文件或者目录的元数据
public static void listFileInfo(String path) {
    Path p = new Path(path);
    try {
        fileStatus = fs.getFileStatus(p);
        System.out.println("文件路径:" + fileStatus.getPath());
        System.out.println("文件路径:" + fileStatus);
        System.out.println("块的大小:" + fileStatus.getBlockSize());
        System.out.println("文件所有者:" + fileStatus.getOwner() + ":"+ fileStatus.getGroup());
        System.out.println("文件权限:" + fileStatus.getPermission());
        System.out.println("文件长度:" + fileStatus.getLen());
        System.out.println("备份数:" + fileStatus.getReplication());
        System.out.println("修改时间:" + fileStatus.getModificationTime());
        } catch (IOException e) {
            e.printStackTrace();
        }
}
// 重命名文件或文件夹
public static void moveFile(String src,String dest) {
    Path srcP = new Path(src);
    Path destP = new Path(dest);
    try {
        fs.rename(srcP, destP);
    } catch (IOException e) {
         e.printStackTrace();
    }   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值