java對Hadoop進行檔案的操作(二)

來源:互聯網
上載者:User

標籤:尋找   static   sys   users   size   cto   建立   data   files   

本次主要是簡單的對Hadoop中HDFS中檔案的操作,可自行先添加檔案,或者直接上傳檔案操作實驗。

去不代碼如下:

package hadoop1;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.net.MalformedURLException;import java.net.URL;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FSDataOutputStream;import org.apache.hadoop.fs.FileStatus;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IOUtils;public class hdfs {    public static void main(String[] args) throws MalformedURLException {        /*URL url=new URL("http://www.baidu.com");//用URL判斷路徑        try {            InputStream in=url.openStream();//路徑開啟一個輸入資料流檔案             IOUtils.copyBytes(in, System.out, 4096,true);//將檔案內容拷貝出來        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }*/                /*        URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());//用URL的方法先建立一個url的工廠        URL url=new URL("hdfs://192.168.83.100:9000/hello.txt");//擷取我們想要檔案的路徑        try {            InputStream in=url.openStream();//檔案開啟             IOUtils.copyBytes(in, System.out, 4096,true);//檔案內容輸出        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }*/                                Configuration conf =new Configuration();//configuration的對象建立        conf.set("fs.defaultFS", "hdfs://192.168.83.100:9000");//將要訪問的路徑放入        try {            FileSystem fileSystem=FileSystem.get(conf);//擷取路徑資訊                        /*boolean success=fileSystem.mkdirs(new Path("/asb"));//判斷是否建立一個目錄檔案            System.out.println(success);                        success=fileSystem.exists(new Path("\\hello.txt"));//判斷是否存在檔案            System.out.println(success);                        success=fileSystem.delete(new Path("/asb"));//刪除檔案            System.out.println(success);                        success=fileSystem.exists(new Path("/asb"));            System.out.println(success);*/                        /*FSDataOutputStream out =fileSystem.create(new Path("/test.data"),true);//重新建立一個data目錄            FileInputStream fis =new FileInputStream("C:\\Users\\ZB\\Desktop\\data\\Hamlet.txt");//將案頭檔案讀入            IOUtils.copyBytes(fis, out, 4096,true);*///讀取放入的檔案                        /*FSDataOutputStream out =fileSystem.create(new Path("/test.data"),true);//手動添加            FileInputStream fis =new FileInputStream("C:\\Users\\ZB\\Desktop\\data\\Hamlet.txt");            byte[] buf=new byte[4096];//一個位元組一個位元組的上傳            int len=fis.read(buf);            while(len!=-1) {                out.write(buf,0,len);                len=fis.read(buf);            }            fis.close();            out.close();*/                        FileStatus[] status=fileSystem.listStatus(new Path("/"));//尋找存在根目錄下的檔案            for(FileStatus status2 : status) {                System.out.println(status2.getPath());//擷取地址                System.out.println(status2.getPermission());//擷取認證資訊                System.out.println(status2.getReplication());//擷取響應                            }        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }                                                         }}

 

java對Hadoop進行檔案的操作(二)

聯繫我們

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