標籤:尋找 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進行檔案的操作(二)