Hadoop-09-HDFS集群 JavaClient 代码上手实战!详细附代码 安装依赖 上传下载文件 扫描列表 PUT GET 进度条显示(二)

简介: Hadoop-09-HDFS集群 JavaClient 代码上手实战!详细附代码 安装依赖 上传下载文件 扫描列表 PUT GET 进度条显示(二)

接上篇:https://developer.aliyun.com/article/1621726?spm=a2c6h.13148508.setting.20.49764f0eWaXqej

展示列表

public static void listList() throws IOException {
    Configuration configuration = new Configuration();
    configuration.set("fs.defaultFS", "hdfs://h121.wzk.icu:9000");
    FileSystem fileSystem = FileSystem.get(configuration);
    RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(new Path("/"), true);
    while (listFiles.hasNext()) {
        LocatedFileStatus status = listFiles.next();
        System.out.println("文件名字: " + status.getPath().getName());
        System.out.println("文件长度: " + status.getLen());
        System.out.println("文件块大小: " + status.getBlockSize());
        System.out.println("权限: " + status.getPermission());
        System.out.println("分组: " + status.getGroup());
        BlockLocation[] blockLocations = status.getBlockLocations();
        for (BlockLocation blockLocation : blockLocations) {
            String[] hosts = blockLocation.getHosts();
            for (String host : hosts) {
                System.out.println(host);
            }
        }
        System.out.println("==========================");
    }
    fileSystem.close();
}

扫描路径

public static void listStatus() throws IOException {
    Configuration configuration = new Configuration();
    configuration.set("fs.defaultFS", "hdfs://h121.wzk.icu:9000");
    FileSystem fileSystem = FileSystem.get(configuration);
    FileStatus[] listStatus = fileSystem.listStatus(new Path("/"));
    for (FileStatus fileStatus : listStatus) {
        if (fileStatus.isFile()) {
            System.out.println("文件: " + fileStatus.getPath().getName());
        } else {
            System.out.println("文件夹: " + fileStatus.getPath().getName());
        }
    }
    fileSystem.close();
}

PUT 操作

public static void putFile() throws IOException {
    Configuration configuration = new Configuration();
    configuration.set("fs.defaultFS", "hdfs://h121.wzk.icu:9000");
    FileSystem fileSystem = FileSystem.get(configuration);
    try (FileInputStream fis= new FileInputStream("wzk02.txt")) {
        FSDataOutputStream fos = fileSystem.create(new Path("/wzk02_io.txt"));
        IOUtils.copyBytes(fis, fos, configuration);
        IOUtils.closeStream(fos);
        IOUtils.closeStream(fis);
        fileSystem.close();
    }
}

GET 操作

public static void getFile() throws IOException {
    Configuration configuration = new Configuration();
    configuration.set("fs.defaultFS", "hdfs://h121.wzk.icu:9000");
    FileSystem fileSystem = FileSystem.get(configuration);
    try (FileOutputStream fos = new FileOutputStream("wzk02_io_get.txt")) {
        FSDataInputStream fis = fileSystem.open(new Path("/wzk02_io.txt"));
        IOUtils.copyBytes(fis, fos, configuration);
        IOUtils.closeStream(fos);
        IOUtils.closeStream(fis);
        fileSystem.close();
    }
}

Seek操作

public static void seek() throws IOException {
    Configuration configuration = new Configuration();
    configuration.set("fs.defaultFS", "hdfs://h121.wzk.icu:9000");
    FileSystem fileSystem = FileSystem.get(configuration);
    FSDataInputStream in = null;
    try {
        in  = fileSystem.open(new Path("/wzk02_io.txt"));
        IOUtils.copyBytes(in, System.out, 4096, false);
        in.seek(0);
        IOUtils.copyBytes(in, System.out, 4096, false);
    } finally {
        IOUtils.closeStream(in);
    }
}

进度显示

public static void uploadProgress() throws IOException {
    Configuration configuration = new Configuration();
    configuration.set("fs.defaultFS", "hdfs://h121.wzk.icu:9000");
    FileSystem fileSystem = FileSystem.get(configuration);
    try (FileInputStream fis = new FileInputStream("music.mp3")) {
        FSDataOutputStream fos = fileSystem.create(new Path("/wzk/music.mp3"),
                () -> System.out.print("="));
        IOUtils.copyBytes(fis, fos, configuration);
        IOUtils.closeStream(fos);
        IOUtils.closeStream(fis);
        fileSystem.close();
        System.out.println("done!");
    }
}

目录
打赏
0
3
3
0
103
分享
相关文章
大数据-82 Spark 集群模式启动、集群架构、集群管理器 Spark的HelloWorld + Hadoop + HDFS
大数据-82 Spark 集群模式启动、集群架构、集群管理器 Spark的HelloWorld + Hadoop + HDFS
292 6
|
3月前
|
java实现从HDFS上下载文件及文件夹的功能,以流形式输出,便于用户自定义保存任何路径下
java实现从HDFS上下载文件及文件夹的功能,以流形式输出,便于用户自定义保存任何路径下
134 34
Hadoop-35 HBase 集群配置和启动 3节点云服务器 集群效果测试 Shell测试
Hadoop-35 HBase 集群配置和启动 3节点云服务器 集群效果测试 Shell测试
127 4
Hadoop-37 HBase集群 JavaAPI 操作3台云服务器 POM 实现增删改查调用操作 列族信息 扫描全表
Hadoop-37 HBase集群 JavaAPI 操作3台云服务器 POM 实现增删改查调用操作 列族信息 扫描全表
63 3
Hadoop-36 HBase 3节点云服务器集群 HBase Shell 增删改查 全程多图详细 列族 row key value filter
Hadoop-36 HBase 3节点云服务器集群 HBase Shell 增删改查 全程多图详细 列族 row key value filter
90 3
从“笨重大象”到“敏捷火花”:Hadoop与Spark的大数据技术进化之路
从“笨重大象”到“敏捷火花”:Hadoop与Spark的大数据技术进化之路
121 79
大数据-80 Spark 简要概述 系统架构 部署模式 与Hadoop MapReduce对比
大数据-80 Spark 简要概述 系统架构 部署模式 与Hadoop MapReduce对比
131 2
Flume+Hadoop:打造你的大数据处理流水线
本文介绍了如何使用Apache Flume采集日志数据并上传至Hadoop分布式文件系统(HDFS)。Flume是一个高可用、可靠的分布式系统,适用于大规模日志数据的采集和传输。文章详细描述了Flume的安装、配置及启动过程,并通过具体示例展示了如何将本地日志数据实时传输到HDFS中。同时,还提供了验证步骤,确保数据成功上传。最后,补充说明了使用文件模式作为channel以避免数据丢失的方法。
129 4
数据湖技术:Hadoop与Spark在大数据处理中的协同作用
【10月更文挑战第27天】在大数据时代,数据湖技术凭借其灵活性和成本效益成为企业存储和分析大规模异构数据的首选。Hadoop和Spark作为数据湖技术的核心组件,通过HDFS存储数据和Spark进行高效计算,实现了数据处理的优化。本文探讨了Hadoop与Spark的最佳实践,包括数据存储、处理、安全和可视化等方面,展示了它们在实际应用中的协同效应。
265 2
数据湖技术:Hadoop与Spark在大数据处理中的协同作用
【10月更文挑战第26天】本文详细探讨了Hadoop与Spark在大数据处理中的协同作用,通过具体案例展示了两者的最佳实践。Hadoop的HDFS和MapReduce负责数据存储和预处理,确保高可靠性和容错性;Spark则凭借其高性能和丰富的API,进行深度分析和机器学习,实现高效的批处理和实时处理。
233 1

相关实验场景

更多
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等