Pass"Filesystem. getfileblocklocation (filestatus file, long start, long Len)"You can find the location of the specified file on the HDFS cluster. file is the complete path of the file, and start and Len are used to identify the path of the file to be searched.
The following are JavaCodeImplementation:
Package com. njupt. hadoop;
Import org. Apache. hadoop. conf. configuration;
Import org. Apache. hadoop. fs. blocklocation;
Import org. Apache. hadoop. fs. filestatus;
Import org. Apache. hadoop. fs. filesystem;
Import org. Apache. hadoop. fs. path;
Import org. Apache. hadoop. HDFS. distributedfilesystem;
Import org. Apache. hadoop. HDFS. Protocol. datanodeinfo;
Public class findfileonhdfs {
Public static void main (string [] ARGs) throws exception {
Gethdfsnodes ();
Getfilelocal ();
}
Public static void getfilelocal () throws exception {
Configuration conf = new configuration ();
Filesystem HDFS = filesystem. Get (CONF );
Path fpath = New Path ("/user/root/20120722/word.txt ");
Filestatus = HDFS. getfilestatus (fpath );
Blocklocation [] blklocations = HDFS. getfileblocklocations (filestatus, 0, filestatus. getlen ());
Int blocklen = blklocations. length;
For (INT I = 0; I <blocklen; ++ I ){
String [] hosts = blklocations [I]. gethosts ();
System. Out. println ("Block _" + I + "_ location:" + hosts [I]);
}
}
Public static void gethdfsnodes () throws exception {
Configuration conf = new configuration ();
Filesystem FS = filesystem. Get (CONF );
Distributedfilesystem HDFS = (distributedfilesystem) FS;
Datanodeinfo [] datanodestats = HDFS. getdatanodestats ();
For (INT I = 0; I <datanodestats. length; ++ I ){
System. Out. println ("datanode _" + I + "_ node:" + datanodestats [I]. gethostname ());
}
}
}