FileSystem shell command
- Official document: HDFS Commands Reference
appendToFilecatchecksumchgrpchmodchowncopyFromLocalcopyToLocalcountcpcreateSnapshotdeleteSnapshotdfdudusexpungefindgetgetfaclgetfattrgetmergehelplslsrmkdirmoveFromLocalmoveToLocalmvputrenameSnapshotrmrmdirrmrsetfaclsetfattrsetrepstattailtesttexttouchztruncateusage
FileSystem Java API
Test code
PackageHDFsImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.net.URI;ImportOrg.apache.hadoop.conf.Configuration;ImportOrg.apache.hadoop.fs.FSDataInputStream;ImportOrg.apache.hadoop.fs.FSDataOutputStream;ImportOrg.apache.hadoop.fs.FileStatus;ImportOrg.apache.hadoop.fs.FileSystem;ImportOrg.apache.hadoop.fs.Path;ImportOrg.apache.hadoop.io.IOUtils; Public class hdfsoperator { Static FinalString PATH ="Hdfs://master:9000/";Static FinalString DIR ="/d1";Static FinalString FILE ="/d1/hello";Static FinalString LoadFile ="F:/readme.txt";Static FinalString Downfile ="F:/readme_tmp.txt";Private Static void getList(FileSystem FileSystem, String DIR)throwsException {Finalfilestatus[] Liststatus = Filesystem.liststatus (NewPath (DIR)); for(Filestatus filestatus:liststatus) {String isdir = Filestatus.isdirectory ()?"dir":"File";FinalString Permission = Filestatus.getpermission (). toString ();Final ShortReplication = Filestatus.getreplication ();Final LongLen = Filestatus.getlen ();FinalString path = Filestatus.getpath (). toString (); System.out.println (isdir+"\ T"+permission+"\ T"+replication+"\ T"+len+"\ T"+path); } }Private Static void GetData(FileSystem FileSystem, String FILE)throwsException {FinalFsdatainputstream in = Filesystem.open (NewPath (FILE));FinalFileOutputStream out =NewFileOutputStream (Downfile); Ioutils.copybytes (in, out,4096,true); System.out.println ("Get HDFs:"+file+"success!"+"Saved as"+ Downfile); }Private Static void PutData(FileSystem FileSystem, string src, string dest)throwsException {FinalFsdataoutputstream out = Filesystem.create (NewPath (dest));FinalFileInputStream in =NewFileInputStream (SRC); Ioutils.copybytes (in, out,4096,true); }Private Static void Remove(FileSystem FileSystem, String DIR)throwsException {BooleanFlag = Filesystem.delete (NewPath (DIR),true); System.out.println ("Del"+ DIR + (flag?)
"Success":"Failure") ); }Private Static void Create(FileSystem FileSystem, String DIR)throwsException {if(Filesystem.exists (NewPath (DIR)) = =true) {Remove (FileSystem, DIR); } filesystem.mkdirs (NewPath (DIR)); }Private StaticFileSystemGetfilesystem(String PATH)throwsException {returnFilesystem.get (NewURI (PATH),NewConfiguration ()); } Public Static void Main(string[] args)throwsException {FileSystem FileSystem = Getfilesystem (PATH); Create (FileSystem, DIR); PutData (FileSystem, LoadFile, FILE); GetData (FileSystem, FILE); GetList (FileSystem, PATH); Remove (FileSystem, FILE); }}
HDFs Learning Note (2) Hdfs_shell & Javaapi