Packagelinlintest;ImportJava.io.File;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.net.URI;Importorg.apache.hadoop.conf.Configuration;ImportOrg.apache.hadoop.fs.FSDataInputStream;ImportOrg.apache.hadoop.fs.FileStatus;ImportOrg.apache.hadoop.fs.FileSystem;ImportOrg.apache.hadoop.fs.Path;Importorg.apache.hadoop.io.IOUtils; Public classHdfsfilesystem { Public Static voidMain (string[] args)throwsException {//String uri= "HDFs://ll-167:8020/"; //HDFs Address//String remote= "HDFs://ll-167:8020/lin/in1/1.txt ";//Path on HDFsString uri= "hdfs://192.168.0.151:8020/";//HDFs AddressString local= "C:/users/administrator/desktop/a.txt";//Local PathString remote= "Hdfs://192.168.0.151:8020/workspace/houlinlin"; Configuration conf=NewConfiguration (); //Cat (conf, Uri, "HDFs://ll-167:8020/lin/in1/1.txt "); //Download (conf, uri,remote,local); //Delete (conf, Uri, "HDFs://192.168.0.173:8020/workspace/houlinlin "); //markdir (conf, Uri, "HDFs://192.168.0.151:8020/workspace/houlinlin/file/apply/2014/8a8380c7459dd8b90145a1fafb500235 "); //Checkdir (URI, "D:/file"); //getFile (conf, Uri, "", "HDFs://192.168.0.151:8020/workspace/houlinlin/a.txt ");CopyFile (Conf,uri, "C:/users/administrator/desktop/8a8380c745d05d370145d06719aa3c89.txt", "hdfs:// 192.168.0.151:8020/workspace/houlinlin/file/apply/2014/8a8380c7459dd8b90145a1fafb500235 "); //ls (conf, "HDFs://Fulonghadoop "," HDFs://fulonghadoop/"); } /*** Upload Files *@paramconf *@paramLocal *@paramRemote *@throwsIOException*/ Public Static voidCopyFile (Configuration conf, string URI, String local, string remote)throwsIOException {FileSystem fs=Filesystem.get (Uri.create (URI), conf); Fs.copyfromlocalfile (NewPath (local),NewPath (remote)); System.out.println ("Copy from:" + local + "to" +remote); Fs.close (); } /*** Get file stream on HDFs *@paramconf *@paramURI *@paramLocal *@paramRemote *@throwsIOException*/ Public Static voidGetFileStream (Configuration conf, string URI, String local, string remote)throwsioexception{FileSystem FS=Filesystem.get (Uri.create (URI), conf); Path Path=NewPath (remote); Fsdatainputstream in= Fs.open (path);//Get file StreamFileOutputStream fos =NewFileOutputStream ("C:/users/administrator/desktop/b.txt");//output Stream intCH = 0; while((Ch=in.read ())! =-1) {fos.write (CH); } System.out.println ("-----"); In.close (); Fos.close (); } /*** Create folder *@paramconf *@paramURI *@paramRemoteFile *@throwsIOException*/ Public Static voidMarkdir (Configuration conf, string uri, string remotefile)throwsioexception{FileSystem FS=Filesystem.get (Uri.create (URI), conf); Path Path=NewPath (remotefile); Fs.mkdirs (path); System.out.println ("Create folder" +remotefile); } /*** View Files *@paramconf *@paramURI *@paramRemoteFile *@throwsIOException*/ Public Static voidCat (Configuration conf, string uri, string remotefile)throwsIOException {path Path=NewPath (remotefile); FileSystem FS=Filesystem.get (Uri.create (URI), conf); Fsdatainputstream Fsdis=NULL; System.out.println ("Cat:" +remotefile); Try{Fsdis=Fs.open (path); Ioutils.copybytes (Fsdis, System.out,4096,false); } finally{ioutils.closestream (Fsdis); Fs.close (); } } /*** Download files on HDFs *@paramconf *@paramURI *@paramRemote *@paramLocal *@throwsIOException*/ Public Static voidDownload (Configuration conf, string URI, string remote, String local)throwsIOException {path Path=NewPath (remote); FileSystem FS=Filesystem.get (Uri.create (URI), conf); Fs.copytolocalfile (Path,NewPath (local)); System.out.println ("Download:from" + Remote + "to" +local); Fs.close (); } /*** Delete files or folders *@paramconf *@paramURI *@paramFilePath *@throwsIOException*/ Public Static voidDelete (Configuration conf, String uri,string FilePath)throwsIOException {path Path=NewPath (FilePath); FileSystem FS=Filesystem.get (Uri.create (URI), conf); Fs.deleteonexit (path); System.out.println ("Delete:" +FilePath); Fs.close (); } /*** View the files under the directory *@paramconf *@paramURI *@paramfolder *@throwsIOException*/ Public Static voidLS (Configuration conf, string URI, String folder)throwsIOException {path Path=NewPath (folder); FileSystem FS=Filesystem.get (Uri.create (URI), conf); Filestatus[] List=fs.liststatus (path); System.out.println ("LS:" +folder); System.out.println ("=========================================================="); for(Filestatus f:list) {System.out.printf ("Name:%s, folder:%s, size:%d\n", F.getpath (), F.isdirectory (), F.getlen ()); } System.out. println ("=========================================================="); Fs.close (); } /** * * @paramparentname Absolute Path address *@throwsException*/ Public Static voidCheckdir (String uri,string parentname)throwsexception{//D:\fileConfiguration conf =NewConfiguration (); File File=NewFile (ParentName); BooleanFlag =true; while(flag) {//find out all the files under ParentNamefile[] FileNames = File.listfiles (NewFileFilter ()); if(FileNames! =NULL) { for(inti = 0; i < filenames.length; i++) {File F=Filenames[i]; //System.out.println ("Parent directory:" +f.getparent () + ", File name:" +f.getname ());SYSTEM.OUT.PRINTLN ("Parent directory:" +f.getparent (). replace ("\ \", "/"). Substring (2) + ", File name:" +f.getname ()); String remotefolrd= "Hdfs://192.168.0.173:8020/workspace/houlinlin" +f.getparent (). replace ("\ \", "/"). SUBSTRING (2); Markdir (conf, uri,remotefolrd); CopyFile (conf, uri,f.getparent ()+"\\"+f.getname (), remotefolrd); } } //Find out all the directories under ParentNamefile[] directories = File.listfiles (NewDirectortyfilter ()); if(Directories! =NULL) { for(inti = 0; i < directories.length; i++) {File dir=Directories[i]; //Absolute PathString Path =Dir.getabsolutepath (); //RecursiveCheckdir (Uri,path); }} Flag=false; } } }
Hadoop HDFs file system for uploading and downloading via Java FileSystem