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!");
    }
}

目录
相关文章
|
8天前
|
分布式计算 Kubernetes Hadoop
大数据-82 Spark 集群模式启动、集群架构、集群管理器 Spark的HelloWorld + Hadoop + HDFS
大数据-82 Spark 集群模式启动、集群架构、集群管理器 Spark的HelloWorld + Hadoop + HDFS
48 6
|
9天前
|
分布式计算 Hadoop Shell
Hadoop-35 HBase 集群配置和启动 3节点云服务器 集群效果测试 Shell测试
Hadoop-35 HBase 集群配置和启动 3节点云服务器 集群效果测试 Shell测试
33 4
|
9天前
|
SQL 分布式计算 Hadoop
Hadoop-37 HBase集群 JavaAPI 操作3台云服务器 POM 实现增删改查调用操作 列族信息 扫描全表
Hadoop-37 HBase集群 JavaAPI 操作3台云服务器 POM 实现增删改查调用操作 列族信息 扫描全表
24 3
|
9天前
|
分布式计算 Hadoop Shell
Hadoop-36 HBase 3节点云服务器集群 HBase Shell 增删改查 全程多图详细 列族 row key value filter
Hadoop-36 HBase 3节点云服务器集群 HBase Shell 增删改查 全程多图详细 列族 row key value filter
29 3
|
9天前
|
分布式计算 Java Hadoop
Hadoop-30 ZooKeeper集群 JavaAPI 客户端 POM Java操作ZK 监听节点 监听数据变化 创建节点 删除节点
Hadoop-30 ZooKeeper集群 JavaAPI 客户端 POM Java操作ZK 监听节点 监听数据变化 创建节点 删除节点
19 1
|
9天前
|
分布式计算 监控 Hadoop
Hadoop-29 ZooKeeper集群 Watcher机制 工作原理 与 ZK基本命令 测试集群效果 3台公网云服务器
Hadoop-29 ZooKeeper集群 Watcher机制 工作原理 与 ZK基本命令 测试集群效果 3台公网云服务器
23 1
|
9天前
|
分布式计算 Hadoop Unix
Hadoop-28 ZooKeeper集群 ZNode简介概念和测试 数据结构与监听机制 持久性节点 持久顺序节点 事务ID Watcher机制
Hadoop-28 ZooKeeper集群 ZNode简介概念和测试 数据结构与监听机制 持久性节点 持久顺序节点 事务ID Watcher机制
24 1
|
9天前
|
SQL 分布式计算 监控
Hadoop-20 Flume 采集数据双写至本地+HDFS中 监控目录变化 3个Agent MemoryChannel Source对比
Hadoop-20 Flume 采集数据双写至本地+HDFS中 监控目录变化 3个Agent MemoryChannel Source对比
24 3
|
9天前
|
SQL 分布式计算 Hadoop
Hadoop-14-Hive HQL学习与测试 表连接查询 HDFS数据导入导出等操作 逻辑运算 函数查询 全表查询 WHERE GROUP BY ORDER BY(一)
Hadoop-14-Hive HQL学习与测试 表连接查询 HDFS数据导入导出等操作 逻辑运算 函数查询 全表查询 WHERE GROUP BY ORDER BY(一)
20 4
|
10天前
|
存储 分布式计算 资源调度
大数据-04-Hadoop集群 集群群起 NameNode/DataNode启动 3台公网云 ResourceManager Yarn HDFS 集群启动 UI可视化查看 YarnUI(一)
大数据-04-Hadoop集群 集群群起 NameNode/DataNode启动 3台公网云 ResourceManager Yarn HDFS 集群启动 UI可视化查看 YarnUI(一)
48 5

热门文章

最新文章