前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDFS-常用API操作

HDFS-常用API操作

作者头像
栗筝i
发布2022-12-01 20:27:43
2130
发布2022-12-01 20:27:43
举报
文章被收录于专栏:迁移内容迁移内容

一、Maven

代码语言:javascript
复制
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>RELEASE</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.8.2</version>
</dependency>
<!--  2、Hadoop      -->
<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-common</artifactId>
    <version>3.2.1</version>
</dependency>
<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-client</artifactId>
    <version>3.2.1</version>
</dependency>

如果Eclipse/Idea打印不出日志,在控制台上只显示:

代码语言:javascript
复制
log4j:WARN No appenders could be found for logger (org.apache.hadoop.util.Shell).  
log4j:WARN Please initialize the log4j system properly.  
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

可以在项目的src/main/resources目录下,新建一个文件,命名为“log4j.properties”,在文件中填入

代码语言:javascript
复制
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

二、API操作

1、 读取某个目录下的所有文件
代码语言:javascript
复制
public class CatFiles {

    public static void main(String[] args) throws IOException, InterruptedException {
        // 0 自动快速地使用缺省Log4j环境。
        BasicConfigurator.configure();
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        // 2 配置在集群上运行
        FileSystem fs = FileSystem.get(URI.create("hdfs://Carlota1:9000"), configuration, "root");
        // 3 创建要读取的文件路径
        Path listf = new Path("/testa");
        // 4 创建FileStatus对象,调用listStatus方法  ***
        FileStatus stats[]=fs.listStatus(listf);
        for(int i=0;i<stats.length;i++){
            System.out.println(stats[i].getPath().toString());
        }
        fs.close();
        // 5 返回成功信息
        System.out.println(" ps: 目录文件查找完毕!!!");
    }
}
2、创建HDFS目录
代码语言:javascript
复制
public class MkdirList {

    public static void main(String[] args) throws IOException, InterruptedException{
        // 0 自动快速地使用缺省Log4j环境。
        BasicConfigurator.configure();
        // 1 获取文件系统
        Configuration conf = new Configuration();
        // 2 配置在集群上运行
        FileSystem fs = FileSystem.get(URI.create("hdfs://Carlota1:9000"), conf, "root");
        // 3 创建一个目录 *****
        Path path = new Path("/List");
        fs.mkdirs(path);
        // 4 关闭流
        fs.close();
        // 5 返回创建成功信息
        System.out.println(" ps: 目录创建成功!!!");
    }

}
3、判断文件会否存在
代码语言:javascript
复制
public class ifExistsFlie {

    public static void main(String[] args) throws IOException, InterruptedException {
        // 0 自动快速地使用缺省Log4j环境。
        BasicConfigurator.configure();
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        // 2 配置在集群上运行
        FileSystem fs = FileSystem.get(URI.create("hdfs://Carlota1:9000"), configuration, "root");
        // 3 创建要读取的文件路径
        Path File = new Path("/testa");
        // 4 调用exists方法  返回boolean类型 ***
        boolean isExists = fs.exists(File);
        System.out.println(isExists);
        fs.close();
        // 5 返回成功信息
        System.out.println(" ps: 确认是否存在完毕!!!");
    }
}
4、查找某个文件的状态信息
代码语言:javascript
复制
ublic class FindFile {

    public static void main(String[] args) throws IOException, InterruptedException {
        // 0 自动快速地使用缺省Log4j环境。
        BasicConfigurator.configure();
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        // 2 配置在集群上运行
        FileSystem fs = FileSystem.get(URI.create("hdfs://Carlota1:9000"), configuration, "root");
        // 3 对文件名进行操作   ***
        Path File = new Path("/testa/part-m-00000");
        // 4 创建FileStatus对象,调用getFileStatus方法
        FileStatus filestatus = fs.getFileStatus(File);
        System.out.println(filestatus);
        // 5 返回成功信息
        System.out.println(" ps: 查找信息成功!!!");
    }
代码语言:javascript
复制
FileStatus字段解析
    private Path path;                  - Path路径
    private long length;                - 文件长度
    private boolean isdir;              - 是不是目录
    private short block_replication;    - 块的复本数
    private long blocksize;             - 块大小
    private long modification_time;     - 修改时间
    private long access_time;           - 访问时间
    private FsPermission permission;    - 权限
    private String owner;               - 所有者
    private String group;               - 所在组
    private Path symlink;               - 符号链接,如果isdir为true那么symlink必须为null
5、上传本地文件
代码语言:javascript
复制
public class UploadFiles {

    public static void main(String[] args) throws IOException, InterruptedException {
        // 0 自动快速地使用缺省Log4j环境。
        BasicConfigurator.configure();
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        // 2 配置在集群上运行
        FileSystem fs = FileSystem.get(URI.create("hdfs://Carlota1:9000"), configuration, "root");
        // 3 创建源目的文件路径和文件上传操作 *****
        Path src = new Path("src/main/resources/HdfsCommand.txt");
        Path dst = new Path("/List/HdfsCommand.txt");
        fs.copyFromLocalFile(src, dst);
        // 4 关闭流
        fs.close();
        // 5 返回创建成功信息
        System.out.println(" ps: 文件上传成功!!!");
    }
}
6、文件拷贝到本地
代码语言:javascript
复制
public class CopyFile {

    public static void main(String[] args) throws IOException, InterruptedException {
        // 0 自动快速地使用缺省Log4j环境。
        BasicConfigurator.configure();
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        // 2 配置在集群上运行
        FileSystem fs = FileSystem.get(URI.create("hdfs://Carlota1:9000"), configuration, "root");
        // 3 创建源目的文件路径和文件上传操作 *****
        Path src = new Path("/testa/part-m-00000");
        Path dst = new Path("src/main/");
        fs.copyToLocalFile(src,dst);
        // 4 关闭流
        fs.close();
        // 5 返回创建成功信息
        System.out.println(" ps: 文件拷贝成功!!!!");
    }
}
7、删除文件/目录
代码语言:javascript
复制
public class DeleteFile {

    public static void main(String[] args) throws IOException, InterruptedException {
        // 0 自动快速地使用缺省Log4j环境。
        BasicConfigurator.configure();
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        // 2 配置在集群上运行
        FileSystem fs = FileSystem.get(URI.create("hdfs://Carlota1:9000"), configuration, "root");
        // 3 将要删除的文件/目录路径
        String File = "/List";
        // 4 删除文件 返回boolean类型   ***
        fs.delete(new Path(File), true);
        fs.close();
        // 5 返回成功信息
        System.out.println(" ps: 文件删除成功!!!");
    }
}
8、读文件
代码语言:javascript
复制
public class ReadFile {

    public static void main(String[] args) throws IOException, InterruptedException {
        // 0 自动快速地使用缺省Log4j环境。
        BasicConfigurator.configure();
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        // 2 配置在集群上运行
        FileSystem fs = FileSystem.get(URI.create("hdfs://Carlota1:9000"), configuration, "root");
        // 3 文件路径
        Path File = new Path("hdfs://Carlota1:9000/b.txt");
        // 4 创建FSDataInputStream对象
        FSDataInputStream in = fs.open(File);
        // 6 读取数据
        String info = in.readUTF();
        System.out.println(info);
        // 7 关闭流
        fs.close();
        // 8 返回创建成功信息
        System.out.println(" ps: 文件读取数据成功!!!");
    }

}
9、重命名文件/目录
代码语言:javascript
复制
public class RenameFile {

    public static void main(String[] args) throws IOException, InterruptedException {
        // 0 自动快速地使用缺省Log4j环境。
        BasicConfigurator.configure();
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        // 2 配置在集群上运行
        FileSystem fs = FileSystem.get(URI.create("hdfs://Carlota1:9000"), configuration, "root");
        // 3 对文件名进行操作   ***
        Path old = new Path("/test");
        Path now = new Path("/testa");
        // 4 调用hdfs的rename重命名方法,返回值为boolean类型    ***
        fs.rename(old, now);
        // 5 返回成功信息
        System.out.println(" ps: 文件重命名成功!!!");
    }

}
10、写文件
代码语言:javascript
复制
public class WriteFile {

    public static void main(String[] args) throws IOException, InterruptedException {
        // 0 自动快速地使用缺省Log4j环境。
        BasicConfigurator.configure();
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        // 2 配置在集群上运行
        FileSystem fs = FileSystem.get(URI.create("hdfs://Carlota1:9000"), configuration, "root");
        // 3 文件路径 ***
        Path File=new Path("hdfs://Carlota1:9000/b.txt");
        // 4 创建FSDataOutputStream对象 ***
        FSDataOutputStream out = fs.create(File);
        // 6 写入数据 ***
        out.writeUTF("Hello world!!");
        // 7 关闭流
        fs.close();
        // 8 返回创建成功信息
        System.out.println(" ps: 文件写入数据成功!!");
    }

}
11、返回文件/目录上次修改的时间
代码语言:javascript
复制
public class ReviseTime {
    public static void main(String[] args) throws IOException, InterruptedException {
        // 0 自动快速地使用缺省Log4j环境。
        BasicConfigurator.configure();
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        // 2 配置在集群上运行
        FileSystem fs = FileSystem.get(URI.create("hdfs://Carlota1:9000"), configuration, "root");
        // 3 创建要读取的文件路径
        Path File = new Path("/testa");
        // 4 创建FileStatus对象,调用listStatus方法
        FileStatus filestatus = fs.getFileStatus(File);
        // 5 调用getModificationTime方法 返回值类型为long
        long time = filestatus.getModificationTime();
        // 6 转换long类型为Data
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(time);
        System.out.println(simpleDateFormat.format(date));
        fs.close();
        // 6 返回成功信息
        System.out.println(" ps: 返回信息成功!!!");
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、Maven
  • 二、API操作
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档