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