更新时间:2022-07-19 GMT+08:00
分享

追加文件内容

功能简介

追加文件内容,是指在HDFS的某个指定文件后面,追加指定的内容。过程为:

  1. 实例化一个FileSystem。
  2. 由此FileSystem实例获取各类相关资源。
  3. 将待追加内容添加到HDFS的指定文件后面。

在完成后,需关闭所申请资源。

代码样例

如下是代码片段,详细代码请参考com.huawei.bigdata.hdfs.examples中的HdfsMain类和HdfsWriter类。

 /**
 * 追加文件内容
 *
 * @throws IOException
 */
private void append() throws Exception {
     final String content = "I append this content.";
     InputStream in = (InputStream) new ByteArrayInputStream(
     content.getBytes());
   try {
     HdfsWriter writer = new HdfsWriter(fSystem, DEST_PATH
        + File.separator + FILE_NAME);
     writer.doAppend(in);
    System.out.println("success to append.");
    } finally {
//务必要关闭流资源.
    close(in);
    }
}
分享:

    相关文档

    相关产品