大資料學習——java代碼實現對HDFS檔案的read、append、write操作

來源:互聯網
上載者:User

標籤:dfsclient   項目   mon   lin   app   adl   設定   apache   使用   

在之前的環節進行了HDFS 的搭建過程,接下來學習的內容是通過java代碼實現對HDFS中檔案進行操作。

這部分學習是為了之後在使用到的MapRedce對HDFS 檔案進行操作。

在eclipse上編寫java代碼對HDFS上的檔案進行操作首先要在項目中進行jar包匯入。這裡我們通過建立maven項目,方便jar包的匯入。

需要注意的是在使用mave時匯入本地不存在的jar包需要在有網的環境下進行

這裡我們匯入的jar包有以下幾部分,在pom.xml中配置以下幾個部分,會自動下載相關jar包

<dependencies>        <dependency>            <groupId>org.apache.hadoop</groupId>            <artifactId>hadoop-common</artifactId>            <version>2.7.3</version>        </dependency>        <dependency>            <groupId>org.apache.hadoop</groupId>            <artifactId>hadoop-client</artifactId>            <version>2.7.3</version>        </dependency>        <dependency>            <groupId>jdk.tools</groupId>            <artifactId>jdk.tools</artifactId>            <version>1.8</version>            <scope>system</scope>            <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>        </dependency>    </dependencies>

 

 建立HDFSClient類,進行操作其中:

write:寫入操作,檔案不存在時自動建立檔案並寫入資料

append:追加寫入操作,寫入檔案需存在

read:讀取檔案操作

代碼如下:

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FSDataInputStream;import org.apache.hadoop.fs.FSDataOutputStream;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;public class HDFSClient {    public static void main(String[] args) {                write();         //append();          read();    }    //主加寫入檔案,必須是已有檔案    public static void append(){        Configuration conf=new Configuration();        conf.set("fs.defaultFS", "hdfs://192.168.98.141:9000");        conf.set("dfs.support.append", "true");        conf.set("dfs.client.block.write.replace-datanode-on-failure.policy", "NEVER");//配置許可權        conf.set("dfs.client.block.write.replace-datanode-on-failure.enable", "true");//配置許可權        try {            FileSystem fs=FileSystem.get(conf);            FSDataOutputStream outpustream = fs.append(new Path("/hp/abc.txt"));//FSDataOutputStream檔案輸出資料流 //追加檔案            outpustream.writeBytes("xxxxxx");//追加內容            outpustream.close();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    //寫入檔案    public static void write(){        Configuration conf=new Configuration();        conf.set("fs.defaultFS", "hdfs://192.168.98.141:9000");        try {            FileSystem fs=FileSystem.get(conf);            FSDataOutputStream outputStream = fs.create(new Path("/hp/abc.txt"));            outputStream.writeBytes("abcde");            outputStream.close();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }        public static void read(){        // 設定物件        Configuration conf=new Configuration();        // 在設定物件中設定 namenode        conf.set("fs.defaultFS", "hdfs://192.168.98.141:9000");        try {            // 建立 FileSystem             FileSystem fs=FileSystem.get(conf) ;            // 穿件輸入資料流            FSDataInputStream inputstream = fs.open(new Path("/hp/abc.txt"));            // 位元組流轉字元流            InputStreamReader isr=new InputStreamReader(inputstream);            BufferedReader br=new BufferedReader(isr);            String str=br.readLine();            while(str!=null){                System.out.println(str);                str=br.readLine();            }            br.close();            isr.close();            inputstream.close();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

註:這裡不能write append方法同時使用會報出錯誤,具體如何解決和原因我也沒有弄清楚

大資料學習——java代碼實現對HDFS檔案的read、append、write操作

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.