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

写文件

功能简介

写文件过程为:

  1. 使用FileSystem实例的create方法获取写文件的输出流。
  2. 使用该输出流将内容写入到HDFS的指定文件中。

在写完文件后,需关闭所申请资源。

代码样例

如下是写文件的代码片段,详细代码请参考com.huawei.bigdata.hdfs.examples中的HdfsExample类。

/**
 * 创建文件,写文件
 *
 * @throws java.io.IOException
 * @throws com.huawei.bigdata.hdfs.examples.ParameterException
 */
private void write() throws IOException {
   final String content = "hi, I am bigdata. It is successful if you can see me.";
   FSDataOutputStream out = null;
   try {
       out = fSystem.create(new Path(DEST_PATH + File.separator + FILE_NAME));
       out.write(content.getBytes());
       out.hsync();
       LOG.info("success to write.");
   } finally {
    // make sure the stream is closed finally.
    IOUtils.closeStream(out);
  }
}
分享:

    相关文档

    相关产品