HDFS Java API Common operations

Source: Internet
Author: User

 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

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.