基本的文件系統命令操做, 經過hadoop fs-help能夠獲取全部的命令的詳細幫助文件。java
Java抽象類org.apache.hadoop.fs.FileSystem定義了hadoop的一個文件系統接口。Hadoop中關於文件操做類基本上所有是在"org.apache.hadoop.fs"包中,這些API可以支持的操做包含:打開文件,讀寫文件,刪除文件等。node
Hadoop類庫中最終面向用戶提供的接口類是FileSystem,該類是個抽象類,只能經過來類的get方法獲得具體類。linux
構造方法apache
該類是一個抽象類,經過如下兩種靜態工廠方法能夠過去FileSystem實例:瀏覽器
public staticFileSystem.get(Configuration conf) throws IOExceptionide
public staticFileSystem.get(URI uri, Configuration conf) throws IOExceptionoop
具體方法實現spa
1、publicboolean mkdirs(Path f) throws IOException.net
一次性新建全部目錄(包括父目錄), f是完整的目錄路徑。3d
2、publicFSOutputStream create(Path f) throws IOException
建立指定path對象的一個文件,返回一個用於寫入數據的輸出流
create()有多個重載版本,容許咱們指定是否強制覆蓋已有的文件、文件備份數量、寫入文件緩衝區大小、文件塊大小以及文件權限。
3、publicboolean copyFromLocal(Path src, Path dst) throws IOException
將本地文件拷貝到文件系統
4、publicboolean exists(Path f) throws IOException
檢查文件或目錄是否存在
5、publicboolean delete(Path f, Boolean recursive)
永久性刪除指定的文件或目錄,若是f是一個空目錄或者文件,那麼recursive的值就會被忽略。只有recursive=true時,一個非空目錄及其內容纔會被刪除。
6、FileStatus類封裝了文件系統中文件和目錄的元數據,包括文件長度、塊大小、備份、修改時間、全部者以及權限信息。
經過"FileStatus.getPath()"可查看指定HDFS中某個目錄下全部文件。
packagehdfsTest; importjava.io.IOException; importorg.apache.hadoop.conf.Configuration; importorg.apache.hadoop.fs.FSDataOutputStream; importorg.apache.hadoop.fs.FileStatus; importorg.apache.hadoop.fs.FileSystem; importorg.apache.hadoop.fs.Path; public classOperatingFiles { //initialization static Configuration conf = newConfiguration(); static FileSystem hdfs; static { String path ="/usr/java/hadoop-1.0.3/conf/"; conf.addResource(newPath(path + "core-site.xml")); conf.addResource(newPath(path + "hdfs-site.xml")); conf.addResource(newPath(path + "mapred-site.xml")); path ="/usr/java/hbase-0.90.3/conf/"; conf.addResource(newPath(path + "hbase-site.xml")); try { hdfs =FileSystem.get(conf); } catch (IOException e) { e.printStackTrace(); } } //create a direction public void createDir(String dir)throws IOException { Path path = new Path(dir); hdfs.mkdirs(path); System.out.println("newdir \t" + conf.get("fs.default.name") + dir); } //copy from local file to HDFS file public void copyFile(String localSrc,String hdfsDst) throws IOException{ Path src = newPath(localSrc); Path dst = new Path(hdfsDst); hdfs.copyFromLocalFile(src,dst); //list all the files in thecurrent direction FileStatus files[] =hdfs.listStatus(dst); System.out.println("Uploadto \t" + conf.get("fs.default.name") + hdfsDst); for (FileStatus file : files){ System.out.println(file.getPath()); } } //create a new file public void createFile(String fileName,String fileContent) throws IOException { Path dst = newPath(fileName); byte[] bytes =fileContent.getBytes(); FSDataOutputStream output =hdfs.create(dst); output.write(bytes); System.out.println("newfile \t" + conf.get("fs.default.name") + fileName); } //list all files public void listFiles(String dirName)throws IOException { Path f = new Path(dirName); FileStatus[] status =hdfs.listStatus(f); System.out.println(dirName +" has all files:"); for (int i = 0; i<status.length; i++) { System.out.println(status[i].getPath().toString()); } } //judge a file existed? and delete it! public void deleteFile(String fileName)throws IOException { Path f = new Path(fileName); boolean isExists =hdfs.exists(f); if (isExists) { //if exists, delete boolean isDel =hdfs.delete(f,true); System.out.println(fileName+ " delete? \t" + isDel); } else { System.out.println(fileName+ " exist? \t" + isExists); } } public static void main(String[] args)throws IOException { OperatingFiles ofs = newOperatingFiles(); System.out.println("\n=======createdir======="); String dir ="/test"; ofs.createDir(dir); System.out.println("\n=======copyfile======="); String src ="/home/ictclas/Configure.xml"; ofs.copyFile(src, dir); System.out.println("\n=======createa file======="); String fileContent ="Hello, world! Just a test."; ofs.createFile(dir+"/word.txt",fileContent); } }
上傳本地文件
經過"FileSystem.copyFromLocalFile(Path src,Patch dst)"可將本地文件上傳到HDFS的制定位置上,其中src和dst均爲文件的完整路徑。具體事例以下:
packagecom.hebut.file; importorg.apache.hadoop.conf.Configuration; importorg.apache.hadoop.fs.FileStatus; importorg.apache.hadoop.fs.FileSystem; importorg.apache.hadoop.fs.Path; public classCopyFile { public static void main(String[] args)throws Exception { Configuration conf=new Configuration(); FileSystem hdfs=FileSystem.get(conf); //本地文件 Path src =newPath("D:\\HebutWinOS"); //HDFS爲止 Path dst =new Path("/"); hdfs.copyFromLocalFile(src, dst); System.out.println("Uploadto"+conf.get("fs.default.name")); FileStatusfiles[]=hdfs.listStatus(dst); for(FileStatus file:files){ System.out.println(file.getPath()); } } }
運行結果能夠經過控制檯、項目瀏覽器和Linux查看,如圖所示。
1、控制檯結果
2、項目瀏覽器
3、linux結果詳情
經過"FileSystem.create(Path f)"可在HDFS上建立文件,其中f爲文件的完整路徑。具體實現以下:
package com.hebut.file; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class CreateFile { public static void main(String[] args) throws Exception { Configuration conf=new Configuration(); FileSystem hdfs=FileSystem.get(conf); byte[] buff="hello hadoop world!\n".getBytes(); Path dfs=new Path("/test"); FSDataOutputStream outputStream=hdfs.create(dfs); outputStream.write(buff,0,buff.length); } }
運行結果如圖所示。
1)項目瀏覽器
2)Linux結果
經過"FileSystem.mkdirs(Path f)"可在HDFS上建立文件夾,其中f爲文件夾的完整路徑。具體實現以下:
package com.hebut.dir; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class CreateDir { public static void main(String[] args) throws Exception{ Configuration conf=new Configuration(); FileSystem hdfs=FileSystem.get(conf); Path dfs=new Path("/TestDir"); hdfs.mkdirs(dfs); } }
運行結果如圖所示。
1)項目瀏覽器
2)Linux結果
經過"FileSystem.rename(Path src,Path dst)"可爲指定的HDFS文件重命名,其中src和dst均爲文件的完整路徑。具體實現以下:
package com.hebut.file; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class Rename{ public static void main(String[] args) throws Exception { Configuration conf=new Configuration(); FileSystem hdfs=FileSystem.get(conf); Path frpaht=new Path("/test"); //舊的文件名 Path topath=new Path("/test1"); //新的文件名 boolean isRename=hdfs.rename(frpaht, topath); String result=isRename?"成功":"失敗"; System.out.println("文件重命名結果爲:"+result); } }
運行結果如圖所示。
1)項目瀏覽器
經過"FileSystem.delete(Path f,Boolean recursive)"可刪除指定的HDFS文件,其中f爲須要刪除文件的完整路徑,recuresive用來肯定是否進行遞歸刪除。具體實現以下:
package com.hebut.file; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class DeleteFile { public static void main(String[] args) throws Exception { Configuration conf=new Configuration(); FileSystem hdfs=FileSystem.get(conf); Path delef=new Path("/test1"); boolean isDeleted=hdfs.delete(delef,false); //遞歸刪除 //boolean isDeleted=hdfs.delete(delef,true); System.out.println("Delete?"+isDeleted); } }
運行結果如圖所示。
1)控制檯結果
2)項目瀏覽器
同刪除文件代碼同樣,只是換成刪除目錄路徑便可,若是目錄下有文件,要進行遞歸刪除。
經過"FileSystem.exists(Path f)"可查看指定HDFS文件是否存在,其中f爲文件的完整路徑。具體實現以下:
package com.hebut.file; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class CheckFile { public static void main(String[] args) throws Exception { Configuration conf=new Configuration(); FileSystem hdfs=FileSystem.get(conf); Path findf=new Path("/test1"); boolean isExists=hdfs.exists(findf); System.out.println("Exist?"+isExists); } }
運行結果如圖所示。
1)控制檯結果
2)項目瀏覽器
經過"FileSystem.getModificationTime()"可查看指定HDFS文件的修改時間。具體實現以下:
package com.hebut.file; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class GetLTime { public static void main(String[] args) throws Exception { Configuration conf=new Configuration(); FileSystem hdfs=FileSystem.get(conf); Path fpath =new Path("/user/hadoop/test/file1.txt"); FileStatus fileStatus=hdfs.getFileStatus(fpath); long modiTime=fileStatus.getModificationTime(); System.out.println("file1.txt的修改時間是"+modiTime); } }
運行結果如圖所示。
經過"FileStatus.getPath()"可查看指定HDFS中某個目錄下全部文件。具體實現以下:
package com.hebut.file; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class ListAllFile { public static void main(String[] args) throws Exception { Configuration conf=new Configuration(); FileSystem hdfs=FileSystem.get(conf); Path listf =new Path("/user/hadoop/test"); FileStatus stats[]=hdfs.listStatus(listf); for(int i = 0; i < stats.length; ++i) { System.out.println(stats[i].getPath().toString()); } hdfs.close(); } }
運行結果如圖所示。
1)控制檯結果
2)項目瀏覽器
經過"FileSystem.getFileBlockLocation(FileStatus file,long start,long len)"可查找指定文件在HDFS集羣上的位置,其中file爲文件的完整路徑,start和len來標識查找文件的路徑。具體實現以下:
package com.hebut.file; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.BlockLocation; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class FileLoc { public static void main(String[] args) throws Exception { Configuration conf=new Configuration(); FileSystem hdfs=FileSystem.get(conf); Path fpath=new Path("/user/hadoop/cygwin"); FileStatus filestatus = hdfs.getFileStatus(fpath); BlockLocation[] blkLocations = hdfs.getFileBlockLocations(filestatus, 0, filestatus.getLen()); int blockLen = blkLocations.length; for(int i=0;i String[] hosts = blkLocations[i].getHosts(); System.out.println("block_"+i+"_location:"+hosts[0]); } } }
運行結果如圖所示。
1)控制檯結果
2)項目瀏覽器
經過"DatanodeInfo.getHostName()"可獲取HDFS集羣上的全部節點名稱。具體實現以下:
package com.hebut.file; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.hdfs.DistributedFileSystem; import org.apache.hadoop.hdfs.protocol.DatanodeInfo; public class GetList { public static void main(String[] args) throws Exception { Configuration conf=new Configuration(); FileSystem fs=FileSystem.get(conf); DistributedFileSystem hdfs = (DistributedFileSystem)fs; DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats(); for(int i=0;ilength;i++){ System.out.println("DataNode_"+i+"_Name:"+dataNodeStats[i].getHostName()); } } }
運行結果如圖所示。