HDFS API Learning: A few common APIs

Source: Internet
Author: User

1.hadoop-1.2.1 API Documentation: http://hadoop.apache.org/docs/r1.2.1/api/

2. Several APIs:

  create(Path f): Opens an fsdataoutputstream at the indicated Path.

  copyFromLocalFile(Path src, Path dst): The src file is on the local disk.

  create(Path f): Opens an fsdataoutputstream at the indicated Path.

  booleanexists(Path f): Check if exists.

  get(URI uri, Configuration conf):Returns the FileSystem for this URI‘s scheme and authority.

  listStatus(Path f): List the statuses of the files/directories in the given path if the path is a directory.

  mkdirs(Path f): Call with mkdirs(Path, FsPermission) default permission.

  rename(Path src, Path dst): Renames path src to path DST.

3. Code implementation:

Importjava.io.IOException;ImportJava.net.URI;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; Public classTesthdfs {StaticString HDFs = "hdfs://localhost:9000"; StaticConfiguration conf =NewConfiguration ();  Public Static voidCreateFolder ()throwsIOException {FileSystem fs=Filesystem.get (Uri.create (HDFS), conf); Path Path=NewPath ("/test");        Fs.mkdirs (path);    Fs.close (); }     Public Static voidCreateFile ()throwsIOException {FileSystem fs=Filesystem.get (Uri.create (HDFS), conf); Path Path=NewPath ("/test/test3.txt"); Fsdataoutputstream out=fs.create (path); Out.write ("Hello Hadoop.". GetBytes ()); }     Public Static voidRenameFile ()throwsIOException {FileSystem fs=Filesystem.get (Uri.create (HDFS), conf); Path Path=NewPath ("/test/test1.txt"); Path NewPath=NewPath ("/test/test2.txt");    System.out.println (Fs.rename (path, NewPath)); }     Public Static voidUploadfiletohdfs ()throwsIOException {FileSystem fs=Filesystem.get (Uri.create (HDFS), conf); Path src=NewPath ("/home/ares/test.txt"); Path DST=NewPath ("/test");    Fs.copyfromlocalfile (SRC, DST); }     Public Static voidListFile ()throwsIOException {FileSystem fs=Filesystem.get (Uri.create (HDFS), conf); Path Path=NewPath ("/test"); filestatus[] Files=fs.liststatus (path);  for(Filestatus file:files) {System.out.println (File.getpath (). toString ()); }    }     Public Static voidDeletefileonhdfs ()throwsIOException {FileSystem fs=Filesystem.get (Uri.create (HDFS), conf); Path Path=NewPath ("/test/test2.txt"); BooleanIsexists =fs.exists (path); if(isexists) {fs.delete (path, isexists); System.out.println ("File is deleted."); } Else{System.out.println ("File is exist."); }    }     Public Static voidMain (string[] args)throwsIOException {//CreateFolder (); //CreateFile (); //RenameFile (); //Uploadfiletohdfs (); //listfile ();Deletefileonhdfs (); }}

HDFS API Learning: A few common APIs

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.