HDFs Learning Note (2) Hdfs_shell & Javaapi

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.