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.
boolean
exists(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