更新时间:2024-10-18 GMT+08:00
创建HDFS文件并写入内容
功能简介
写文件过程为:
- 使用FileSystem实例的create方法获取写文件的输出流。
- 使用该数据流将内容写入到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);
}
}
父主题: 开发HDFS应用