Deze pagina is nog niet beschikbaar in uw eigen taal. We werken er hard aan om meer taalversies toe te voegen. Bedankt voor uw steun.

On this page

Writing Data into a File

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

Function

The process of writing data into a file is

  1. Use the create method in the FileSystem instance to obtain the output stream of writing files.
  2. Uses this data stream to write content into a specified file in the HDFS.
NOTE:

Close all requested resources after writing files.

Example Codes

The following is code snippets. For complete codes, see HdfsMain and HdfsWriter classes in com.huawei.bigdata.hdfs.examples.

/**
 * Write a file, write the data
 *
 * @throws IOException
 * @throws 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);
  }
}
Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback