HDFS JAVA API

Source: Internet
Author: User

Http://hadoop.apache.org/docs/r2.5.2/api/index.html

1 Importorg.apache.hadoop.conf.Configuration;2 Importorg.apache.hadoop.fs.*;3 Importorg.apache.hadoop.io.IOUtils;4 ImportOrg.junit.Before;5 Importorg.junit.Test;6 7 ImportJavax.security.auth.login.AppConfigurationEntry;8 ImportJava.io.*;9 Ten /** One * Created by Administrator on 2017/7/22. A  */ -  Public classTesthdfs { -  the     PrivateFileSystem FS; -  - @Before -      Public voidSetUp ()throwsIOException { +Configuration conf =NewConfiguration (); -FS =filesystem.get (conf); +     } A  at @Test -      Public voidTestmakedir ()throwsIOException { -Path PATH =NewPath ("/user/jason/testmkdir"); - fs.mkdirs (path); -Filestatus Filestatus =fs.getfilestatus (path); - System.out.println (Filestatus.isdirectory ()); in System.out.println (Filestatus.getowner ()); - System.out.println (Filestatus.getgroup ()); to System.out.println (Filestatus.getlen ()); +     } -  the @Test *      Public voidTestliststatus ()throwsIOException { $Path PATH =NewPath ("/user/jason");Panax NotoginsengFilestatus[] statuses =fs.liststatus (path); -          for(Filestatus status:statuses) { the System.out.println (Status.getpath ()); +         } A     } the  + @Test -      Public voidTestglobalstatus ()throwsIOException { $Path PATH =NewPath ("/user/jason/test*"); $Filestatus[] statuses =fs.globstatus (path); -          for(Filestatus status:statuses) { - System.out.println (Status.getpath ()); the         } -     }Wuyi  the @Test -      Public voidTestfileupload ()throwsIOException { WuString putfilename = "/user/jason/rhdsetup_hdfs.log"; -Path Writepath =NewPath (putfilename); AboutFsdataoutputstream OutputStream =fs.create (writepath); $FileInputStream InputStream =NewFileInputStream (NewFile ("C:\\rhdsetup.log")); -Ioutils.copybytes (InputStream, outputstream,4096,false); - Ioutils.closestream (inputstream); - Ioutils.closestream (outputstream); A     } +  the @Test -      Public voidTestfiledownload ()throwsIOException { $String downloadfilename = "/user/jason/wcinput"; thePath Readpath =NewPath (downloadfilename); theFsdatainputstream InputStream =Fs.open (readpath); theFileOutputStream FileOutputStream =NewFileOutputStream (NewFile ("F:\\testspace\\wcinput.txt")); theIoutils.copybytes (inputstream,fileoutputstream,4096,false); - Ioutils.closestream (inputstream); in Ioutils.closestream (fileoutputstream); the     } the  About @Test the      Public voidTestcreatefile ()throwsIOException { theString newfilename = "/user/jason/newfile.txt"; thePath PATH =NewPath (newfilename); +Fsdataoutputstream Fsdataoutputstream =fs.create (path); -Fsdataoutputstream.write ("This is a new file". GetBytes ()); the fsdataoutputstream.close ();Bayi     } the  the @Test -      Public voidTestmodifyfile ()throwsIOException { -String modifyfilename = "/user/jason/newfile.txt"; thePath PATH =NewPath (modifyfilename); theFsdataoutputstream Fsdataoutputstream =fs.append (path); theFsdataoutputstream.writeutf ("Add one more row into it\r\n"); the fsdataoutputstream.close (); -     } the  the @Test the      Public voidTestdeletefile ()throwsIOException {94String modifyfilename = "/user/jason/newfile.txt"; thePath PATH =NewPath (modifyfilename); theFs.delete (Path,true); the     }98  About}

HDFS JAVA API

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.