Hadoop HDFS Tools

Source: Internet
Author: User
Tags readfile

Hadoop HDFS Tools
 PackageCN.BUAA;ImportJava.io.ByteArrayOutputStream;ImportJava.io.IOException;ImportJava.io.InputStream;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;ImportOrg.apache.hadoop.fs.RemoteIterator;ImportOrg.apache.hadoop.io.IOUtils;/ * * @author LZXYZQ * HDFs Some common commands, can refer to the Hadoop API FileSystem */ Public  class hdfstools {     Public Hdfstools(){ }//Determine if a file exists in the HDFs file system     Public Static Boolean exists(Configuration config,string Path)throwsioexception{//File system objects are formed according to profile informationFileSystem fs = filesystem.get (config);returnFs.exists (Newpath);//Convert file path to path        //Determine if the file already exists}//give a filename and content, we create the file in HDFs     Public Static void CreateFile(Configuration config,string Path,byte[] buf)throwsioexception{//config-->fs-->fos-->write-->closeFileSystem fs = filesystem.get (config); Path path2 =NewPath (path);        Fsdataoutputstream fos = fs.create (path2);        Fos.write (BUF);        Fos.close ();    Fs.close (); } Public Static void CreateFile(Configuration config,string path,string info)throwsioexception{The difference between//and the above function is that the above function passes in the BUF data, and the following string is passed inCreateFile (Config,path,info.getbytes ()); }//Copy a file on HDFs to another location on HDFs     Public Static void copyfromlocal(Configuration config,string path1,string path2)throwsioexception{FileSystem fs = filesystem.get (config); Path P1 =NewPath (path1); Path P2 =NewPath (path2); Fs.copyfromlocalfile (true,true, p1, p2);//delsrc, overwrite, SRCs, DSTFs.close (); }//Delete one of the files on HDFs     Public Static Boolean DeleteFile(Configuration config,string Path,BooleanMarkthrowsioexception{//config-->fs-->delete-->close--> returnFileSystem fs = filesystem.get (config); Path P1 =NewPath (path);BooleanFlag = Fs.delete (P1, Mark); Fs.close ();returnFlag } Public Static Boolean DeleteFile(Configuration config,string Path)throwsioexception{returnDeleteFile (Config,path,true); }//Modify the file name on HDFs string-->path-->fs.renamefile     Public Static Boolean RenameFile(Configuration config,string path1,strign path2) {//config-->fs-->delete-->close--> returnFileSystem fs = filesystem.get (config); Path oldname =NewPath (path1); Path NewName =NewPath (path2);BooleanFlag = Fs.rename (Oldname, newName); Fs.close ();returnFlag }//Create a directory on HDFs     Public Static Boolean MakeDir(Configuration config,string Path)throwsioexception{FileSystem fs = filesystem.get (config); Path name =NewPath (path);BooleanFlag = fs.mkdirs (name); Fs.close ();returnFlag } Public StaticRemoteiterator<locatedfilestatus>Listfiles(Configuration config,string Path,BooleanMarkthrowsioexception{FileSystem fs = filesystem.get (config); remoteiterator<locatedfilestatus> r1 = Fs.listfiles (NewPath (path), Mark);returnR1; } Public StaticRemoteiterator<locatedfilestatus>Listfiles(Configuration config,string Path)throwsioexception{returnListfiles (Config,path,false); } Public StaticFilestatus[]Liststatus(Configuration config, String path)throwsioexception{FileSystem fs = filesystem.get (config); Path path1 =NewPath (path);        Filestatus status[] = Fs.liststatus (path1); Fs.close ();returnStatus }//Read file contents, return string     Public StaticStringReadFile(Configuration config,string Path)throwsioexception{FileSystem fs = filesystem.get (config);        String temp; InputStream is =NULL;//input streamBytearrayoutputstream BAOs =NULL;//output streamPath p =NewPath (path);Try{is =fs.open (p);//Open input stream            //input stream output stream dockingBAOs =NewBytearrayoutputstream (Is.available ());//Import the contents of the input stream into the output streamIoutils.copybytes (is, BAOs, config); temp = baos.tostring ();//Turn the contents of the output stream into a string}finally{//Turn off the input and output stream and file systemIoutils.closestream (IS);            Ioutils.closestream (BAOs);        Fs.close (); }returnTemp } Public Static Boolean CreateDictionary(Configuration config,string Path)throwsioexception{FileSystem fs = filesystem.get (config); Path p =NewPath (path);BooleanFlag = Fs.mkdirs (p); Fs.close ();returnFlag } Public Static void Main(string[] args)throwsioexception{Configuration config =NewConfiguration (); String dir ="/test";BooleanFlag = exists (Config,dir);if(flag==true) {System.out.println ("Sorry It ' s exists"); }Else{flag = CreateDictionary (Config,dir);if(flag==true) {System.out.println ("created successfully"); }Else{System.out.println ("Create failed"); }} String info ="Beihang University!" "; String FileName = dir+"/buaa.txt";        CreateFile (Config,filename,info); System.out.println ("Create file succeeded");        String message = ReadFile (config,filename);        SYSTEM.OUT.PRINTLN (message); Filestatus all[] = liststatus (config,"/"); for(intI=0; i<all.length;i++) {System.out.println (all[i]);        } FileSystem fs = filesystem.get (config); Remoteiterator ri = listfiles (config,"/",true); while(Ri.hasnext ())            {String str = Ri.next (). toString ();        System.out.println (str);        } deletefile (Config,filename); System.out.println (filename+"is deleted"); }}

Hadoop HDFS Tools

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.