PackageCom.luogankun.hadoop.hdfs.api;ImportJava.io.BufferedInputStream;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.InputStream;ImportJava.net.URI;Importorg.apache.hadoop.conf.Configuration;Importorg.apache.hadoop.fs.BlockLocation;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;Importorg.apache.hadoop.util.Progressable;ImportOrg.junit.After;ImportOrg.junit.Before;Importorg.junit.Test;/*** HDFS Java API Operation *@authorLUOGK **/ Public classHdfsapp { Public Static FinalString Hdfs_path = "hdfs://hadoop000:8020"; Configuration Configuration=NULL; FileSystem FileSystem=NULL; @Before Public voidSetUp ()throwsexception{System.out.println ("Hdfsapp.setup ()"); Configuration=NewConfiguration (); FileSystem= Filesystem.get (NewURI (Hdfs_path), configuration); } /*** Create a directory*/@Test Public voidmkdir ()throwsException {filesystem.mkdirs (NewPath ("/hdfsapi/test")); } /*** Create a file*/@Test Public voidCreate ()throwsException {fsdataoutputstream output= Filesystem.create (NewPath ("/hdfsapi/test/a.txt")); Output.write ("Hello World". GetBytes ()); Output.flush (); Output.close (); } /*** Rename*/@Test Public voidRename ()throwsException {Path OldPath=NewPath ("/hdfsapi/test/a.txt"); Path NewPath=NewPath ("/hdfsapi/test/b.txt"); System.out.println (Filesystem.rename (OldPath, NewPath)); } /*** Upload local files to HDFs*/@Test Public voidCopyfromlocalfile ()throwsException {Path src=NewPath ("D:/case2.sql"); Path Dist=NewPath ("/hdfsapi/test/"); Filesystem.copyfromlocalfile (SRC, dist); } /*** Upload local files to HDFs*/@Test Public voidCopybytes ()throwsException {InputStream in=NewBufferedinputstream (NewFileInputStream (NewFile ("D:/software/apache/spark/spark-1.1.0-snapshot-bin-2.3.0-cdh5.0.0.tgz"))); Fsdataoutputstream out= Filesystem.create (NewPath ("/hdfsapi/test/spark.tgz"),Newprogressable () {@Override Public voidProgress () {System.out.print (".");//with progress tip information } }); Ioutils.copybytes (in, Out,4096); } /*** View all files in a directory*/@Test Public voidListfiles ()throwsException {filestatus[] liststatus= Filesystem.liststatus (NewPath ("/hdfsapi/test")); for(Filestatus filestatus:liststatus) {String Isdir= Filestatus.isdirectory ()? " Folder ":" File ";//Files/FoldersString Permission = Filestatus.getpermission (). toString ();//Permissions ShortReplication = Filestatus.getreplication ();//replica Factor LongLen = Filestatus.getlen ();//lengthString path = Filestatus.getpath (). toString ();//PathSystem.out.println (isdir+ "\ t" +permission+ "\ T" +replication+ "\ T" +len+ "\ T" +path); } } /*** View File block information*/@Test Public voidGetfileblocklocations ()throwsException {filestatus filestatus= Filesystem.getfilestatus (NewPath ("/hdfsapi/test/spark.tgz")); Blocklocation[] Blocks= Filesystem.getfileblocklocations (filestatus, 0, Filestatus.getlen ()); for(blocklocation block:blocks) { for(String host:block.getHosts ()) {System.out.println (host); } }} @After Public voidTearDown ()throwsexception{FileSystem=NULL; Configuration=NULL; System.out.println ("Hdfsapp.teardown ()"); }}
HDFS Java API Common operations