HDFS檔案內容追加(Append) hdfs2.x版支援

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   strong   

HDFS設計之處並不支援給檔案追加內容,這樣的設計是有其背景的(如果想瞭解更多關於HDFS的append的曲折實現,可以參考《File Appends in HDFS》:http://blog.cloudera.com/blog/2009/07/file-appends-in-hdfs/),但從HDFS2.x開始支援給檔案追加內容,可以參見https://issues.apache.org/jira/browse/HADOOP-8230。可以再看看http://www.quora.com/HDFS/Is-HDFS-an-append-only-file-system-Then-how-do-people-modify-the-files-stored-on-HDFS。正如HADOOP-8230所述,只需要將hdfs-site.xml中的以下屬性修改為true就行。

<property>    <name>dfs.support.append</name>    <value>true</value></property>

  目前如何在命令行裡面給HDFS檔案中追加內容我還沒找到相應的方法。但是,我們可以通過Hadoop提供的API實現檔案內容追加,如何??這裡我寫了一個簡單的測試程式:

package com.wyp;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IOUtils;import java.io.*;import java.net.URI;/** * blog: http://www.iteblog.com/ * Date: 14-1-2 * Time: 下午6:09 */public class AppendContent {    public static void main(String[] args) {        String hdfs_path = "hdfs://mycluster/home/wyp/wyp.txt";//檔案路徑        Configuration conf = new Configuration();        conf.setBoolean("dfs.support.append", true);        String inpath = "/home/wyp/append.txt";        FileSystem fs = null;        try {            fs = FileSystem.get(URI.create(hdfs_path), conf);            //要追加的檔案流,inpath為檔案            InputStream in = new                   BufferedInputStream(new FileInputStream(inpath));            OutputStream out = fs.append(new Path(hdfs_path));            IOUtils.copyBytes(in, out, 4096, true);        } catch (IOException e) {            e.printStackTrace();        }    }}

將上面的代碼打包成jar(這裡我取名為hdfs.jar)檔案,然後上傳到機器中,比如我上傳到我的home目錄,在程式運行前,我們來看看HDFS中wyp.txt檔案中的內容有什麼

[[email protected] ~]$ /home/q/hadoop-2.2.0/bin/hadoop fs                                              -cat /home/wyp/wyp.txt123456[[email protected] ~]$

好,我們再來看看/home/wyp/append.txt檔案中的內容:

[[email protected] ~]$ vim append.txtwyp  append test

看完代碼中所涉及到的兩個檔案之後,我們再運行hdfs.jar

[[email protected] ~]$ /home/q/hadoop-2.2.0/bin/hadoop jar                                        hdfs.jar com.wyp.AppendContent

運行完之後,看看wyp.txt內容

[[email protected] ~]$ /home/q/hadoop-2.2.0/bin/hadoop fs                                          -cat /home/wyp/wyp.txt123456wyp  append test

好了,wyp.txt檔案已經追加了append.txt檔案中的內容了。本部落格文章除特別聲明,全部都是原創!

尊重原創,轉載請註明: 轉載自過往記憶(http://www.iteblog.com/)
本文連結地址: 《HDFS檔案內容追加(Append)》(http://www.iteblog.com/archives/881)
E-mail:[email protected]    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.