Find the location of a file in the HDFS Cluster

Source: Internet
Author: User

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 ());
}
}
}

 

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.