Writing Data to a File
Function Description
The process of writing data to a file is as follows:
- Instantiate a FileSystem.
- Use the FileSystem instance to obtain various types of resources for writing data to files.
- 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); } }
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.