学习 HDFS(三):写文件

栏目: 服务器 · 发布时间: 5年前

内容简介:HDFS 客户端写流程:

写流程

HDFS 客户端写流程:

  1. 客户端连接 NameNode 请求打开写入文件,NameNode 创建文件元数据。此时,并没有与文件元数据相对应的数据块。

  2. 客户端向 NameNode 请求复制管道(Replication Pipeline),并向管道中的第一个 DataNode 写入数据。

  3. 复制管道中的第一个 DataNode 又将数据复制到管道下一个 DataNode。

  4. 关闭文件。

学习 HDFS(三):写文件

Java API

public static void write(String path, String content) {  
    Configuration conf = new Configuration();
    try (FileSystem fs = FileSystem.get(new URI("hdfs://nameservice1"), conf, "hdfs")) {
        Path p = new Path(path);

        if (!fs.exists(p)) {
            try (FSDataOutputStream out = fs.create(p)) {
                out.write(content.getBytes(Charsets.UTF_8));
            }
        } else {
            // 文件已存在
            logger.warn("文件 {} 已存在", path);
        }
    } catch (URISyntaxException | InterruptedException | IOException e) {
        logger.error("写文件错误", e);
        throw new RuntimeException(e);
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Algorithms in Java, Part 5

Algorithms in Java, Part 5

Robert Sedgewick / Addison-Wesley Professional / 2003-7-25 / USD 54.99

Algorithms in Java, Third Edition, Part 5: Graph Algorithms is the second book in Sedgewick's thoroughly revised and rewritten series. The first book, Parts 1-4, addresses fundamental algorithms, data......一起来看看 《Algorithms in Java, Part 5》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

MD5 加密
MD5 加密

MD5 加密工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试