今天思考到一個問題,HDFS文件怎麼實現的在線編輯?
hdfs 有個 -put 命令,允許攜帶一些參數完成特殊的需求:
- 上傳文件
hdfs dfs -put - 在文件中追加
hdfs dfs -appendToFile - 強制更新
hdfs dfs -put -f
首先上傳了一個文件到HDFS上:hdfs dfs -put test1File /haddop/test1File
然後需要在這個文件中進行內容追加:hdfs dfs -appendToFile test2File /hadoop/test1File
最後需要重置hdfs上的文件內容:hdfs dfs -put -f test1File /hadoop/test1File
下面是測試的過程:
[hdfs@potter hdfs]$ cat test1File
hello
[hdfs@potter hdfs]$ cat test2File
Hey
[hdfs@potter hdfs]$ hdfs dfs -mkdir hdfs://cluster:8020/data/test11
[hdfs@potter hdfs]$ hdfs dfs -put test1File hdfs://cluster:8020/data/test11/
[hdfs@potter hdfs]$ hdfs dfs -cat hdfs://cluster:8020/data/test11/test1File
hello
[hdfs@potter hdfs]$ hdfs dfs -appendToFile test2File hdfs://cluster:8020/data/test11/test1File
[hdfs@potter hdfs]$ hdfs dfs -cat hdfs://cluster:8020/data/test11/test1File
hello
Hey
[hdfs@potter hdfs]$ hdfs dfs -put -f test1File hdfs://cluster:8020/data/test11/test1File
[hdfs@potter hdfs]$ hdfs dfs -cat hdfs://cluster:8020/data/test11/test1File
hello
最後,在線編輯文件的方式,可以通過覆蓋命令實現:hdfs dfs -put -f <修改過的文件> <線上已經存在的文件>