Updated on 2022-09-14 GMT+08:00

Writing Data to a File

Function Description

The process of writing data to a file is as follows:

  1. Instantiate a FileSystem.
  2. Use the FileSystem instance to obtain various types of resources for writing data to files.
  3. Write the data to a specified file in HDFS.

Close all requested resources after writing data to the file.

Sample Code

The following provides code snippets for writing data to a file. For complete codes, see the HdfsMain and HdfsWriter classes in com.huawei.bigdata.hdfs.examples.

/**
 * Create a file and write data to the file.
 *
 * @throws IOException
 * @throws ParameterException 
 */
private void write() throws IOException, ParameterException {
   final String content = "hi, I am bigdata. It is successful if you can see me.";
   InputStream in = (InputStream) new ByteArrayInputStream(
content.getBytes());
   try {
      HdfsWriter writer = new HdfsWriter(fSystem, DEST_PATH
            + File.separator + FILE_NAME);
      writer.doWrite(in);
     System.out.println("success to write.");
   } finally {
      // Stream resources must be closed.
      close(in);
   }
}