This article provides several simple examples of how to use the Java API of Hadoop for file upload, create, rename, delete operations on HDFs
This article address: http://www.cnblogs.com/archimedes/p/hdfs-api-operations.html, reprint please indicate source address.
1. Uploading local files to HDFs
The local file is uploaded to the location specified in HDFs via filesystem.copyfromlocalfile (path src, path DST) , where Src and DST are the full paths to the file
Create a new file Copyfile.java, add code under the Myhelloworld project in the article "Hadoop Combat-build a development environment and write Hello World":
ImportJava.util.*;Importjava.io.IOException;ImportJava.util.StringTokenizer;Importorg.apache.hadoop.conf.Configuration;Importorg.apache.hadoop.fs.*;ImportOrg.apache.hadoop.fs.FileStatus;ImportOrg.apache.hadoop.fs.FileSystem;ImportOrg.apache.hadoop.fs.Path; Public classCopyFile { Public Static voidMain (string[] args)throws Exception {Configuration conf=NewConfiguration (); FileSystem HDFs=filesystem.get (conf); Path src=NewPath ("/home/wu/copy.txt");//Full pathPath DST =NewPath ("hdfs://localhost:9000/user/wu/in/"); Hdfs.copyfromlocalfile (SRC, DST); System.out.println ("Upload to" + Conf.get ("Fs.default.name")); Filestatus files[]=Hdfs.liststatus (DST); for(Filestatus file:files) {System.out.println (File.getpath ()); } }}
Copy.txt for a new test file for itself, about the DST path, you can double-click the test1.txt in the file, the full pathname will appear (hdfs://localhost:9000/user/wu/in)
Note: Add throws Exception after main, otherwise you will get an error
The results of the operation can be verified in the console and folder, and the console displays:
Upload tohdfs://localhost:9000/
Hdfs://localhost:9000/user/wu/in/copy.txt
Hdfs://localhost:9000/user/wu/in/test1.txt
Hdfs://localhost:9000/user/wu/in/test2.txt
Refresh the project, you can see the in file folder more than one Copy.txt file
2. Create an HDFs file
You can create a file on HDFs with filesystem.creat (path F) , where F is the full path to the file.
Importorg.apache.hadoop.conf.Configuration;ImportOrg.apache.hadoop.fs.FSDataOutputStream;ImportOrg.apache.hadoop.fs.FileSystem;ImportOrg.apache.hadoop.fs.Path; Public classCreateFile { Public Static voidMain (string[] args)throwsexception{Configuration conf=NewConfiguration (); byte[] buff = "Hello World, Hello wu!". GetBytes (); FileSystem HDFs=filesystem.get (conf); Path DFS=NewPath ("Hdfs://localhost:9000/user/wu/in/copy.txt"); Fsdataoutputstream OutputStream=hdfs.create (DFS); Outputstream.write (Buff,0, buff.length); }}
Double-click to view the Copy.txt file in the In folder as expected:
3. Renaming an HDFs file
The specified HDFs file can be renamed with filesystem.rename (path src, path DST) , where Src and DST are the full paths to the file.
Importorg.apache.hadoop.conf.Configuration;ImportOrg.apache.hadoop.fs.FileSystem;ImportOrg.apache.hadoop.fs.Path; Public classRenameFile { Public Static voidMain (string[] args)throwsException {//TODO auto-generated Method StubConfiguration conf =NewConfiguration (); FileSystem HDFs=filesystem.get (conf); Path Frpath=NewPath ("Hdfs://localhost:9000/user/wu/in/copy.txt"); Path Topath=NewPath ("Hdfs://localhost:9000/user/wu/in/renamefile.txt"); BooleanIsrenamefile =hdfs.rename (Frpath, Topath); }}
After running the results such as:
4. Delete files on HDFs
The specified HDFs file can be deleted by filesystem.delete (path F,boolean recursive) , where F is the full path to the file that needs to be deleted, and recursive is used to determine whether recursive deletion is possible.
Importorg.apache.hadoop.conf.Configuration;ImportOrg.apache.hadoop.fs.FileSystem;ImportOrg.apache.hadoop.fs.Path; Public classRenameFile { Public Static voidMain (string[] args)throwsException {//TODO auto-generated Method StubConfiguration conf =NewConfiguration (); FileSystem HDFs=filesystem.get (conf); Path Delpath=NewPath ("Hdfs://localhost:9000/user/wu/in/renamefile.txt"); BooleanisDeleted = Hdfs.delete (Delpath,false); System.out.println ("Delete?" +isDeleted); }}
After running the results such as:
5. View the last modified time of the HDFs file
You can view the modified time of the specified HDFs file through filestatus.getmodificationtime () .
Importorg.apache.hadoop.conf.Configuration;ImportOrg.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.FileStatus; ImportOrg.apache.hadoop.fs.Path; Public classGetltime { Public Static voidMain (string[] args)throwsexception{//TODO auto-generated Method StubConfiguration conf =NewConfiguration (); FileSystem HDFs=filesystem.get (conf); Path Fpath=NewPath ("Hdfs://localhost:9000/user/wu/in/hdfstest.txt"); Filestatus Filestatus=Hdfs.getfilestatus (Fpath); LongModificationtime =Filestatus.getmodificationtime (); System.out.println ("Modification Time is" +modificationtime); }}
The results of the operation are as follows:
Modification Time is 1418719100449
6. See if an HDFs file exists
by filesystem.exists (path f) , you can see whether the specified HDFs file exists, where F is the full path to the file.
Importorg.apache.hadoop.conf.Configuration;ImportOrg.apache.hadoop.fs.FileSystem;ImportOrg.apache.hadoop.fs.FileStatus;ImportOrg.apache.hadoop.fs.Path; Public classCheckfile { Public Static voidMain (string[] args)throwsException {//TODO auto-generated Method StubConfiguration conf =NewConfiguration (); FileSystem HDFs=filesystem.get (conf); Path FindFile=NewPath ("Hdfs://localhost:9000/user/wu/in/hdfstest.txt"); BooleanIsexists =hdfs.exists (FindFile); System.out.println ("is exists?" +isexists); }}
The results of the operation are as follows:
is exists? True
7. View the HDFs cluster location for a file
by Filesystem.getfileblocklocation (Filestatus file,long start,long len), you can find the location of the specified file on the HDFs cluster, where file is the full path Start and Len to identify the path to the lookup file.
Importorg.apache.hadoop.conf.Configuration;Importorg.apache.hadoop.fs.BlockLocation;ImportOrg.apache.hadoop.fs.FileSystem;ImportOrg.apache.hadoop.fs.FileStatus;ImportOrg.apache.hadoop.fs.Path; Public classFileLoc { Public Static voidMain (string[] args)throwsexception{//TODO auto-generated Method StubConfiguration conf =NewConfiguration (); FileSystem HDFs=filesystem.get (conf); Path Fpath=NewPath ("Hdfs://localhost:9000/user/wu/in"); Filestatus Filestatus=Hdfs.getfilestatus (Fpath); Blocklocation[] Blklocations= Hdfs.getfileblocklocations (filestatus, 0, Filestatus.getlen ()); intBlocklen =blklocations.length; System.out.println (Blocklen); for(inti = 0; i < Blocklen; i++) {string[] hosts=blklocations[i].gethosts (); System.out.println ("Block" + i + "Location:" +Hosts[i]); } }}References
"Combat HADOP: Open the way to cloud computing." Liu Peng
HDFS API Operation Practices