Append data to existing file in HDFS Java

后端 未结 3 455
隐瞒了意图╮
隐瞒了意图╮ 2020-12-05 02:52

I\'m having trouble to append data to an existing file in HDFS. I want that if the file exists then append a line, if not, create a new file with the name given.

Her

3条回答
  •  醉话见心
    2020-12-05 03:46

    HDFS does not allow append operations. One way to implement the same functionality as appending is:

    • Check if file exists.
    • If file doesn't exist, then create new file & write to new file
    • If file exists, create a temporary file.
    • Read line from original file & write that same line to temporary file (don't forget the newline)
    • Write the lines you want to append to the temporary file.
    • Finally, delete the original file & move(rename) the temporary file to the original file.

提交回复
热议问题