Hadoop uses the Filestatus class to view meta information for files or directories in HDFs

Source: Internet
Author: User
Tags date file system tostring file permissions

The Filestatus class in Hadoop can be used to view the meta information of files or directories in HDFs, any file or directory can get the corresponding filestatus, and here is a simple demo of the relevant API for this class:

* */package COM.CHARLES.HADOOP.FS; 
Import Java.net.URI; 
     
Import Java.sql.Timestamp; 
Import org.apache.hadoop.conf.Configuration; 
Import Org.apache.hadoop.fs.FileStatus; 
Import Org.apache.hadoop.fs.FileSystem; 
     
Import Org.apache.hadoop.fs.Path; /** * * Description: This class demonstrates how to obtain the Filestatus object * and then query the metadata of the file or directory through the FileSystem Getfilestatus () method * * We do 2 experiments here, in order to get HD The meta information of a file in FS, obtaining meta information for a directory in HDFs * * @author Charles.wang * @created May, 1:43:01 PM * */public class Filem  
        Etadataquerier {/** * @param args/public static void main (string[] args) throws Exception { 
        TODO auto-generated Method Stub//read the configuration of the Hadoop file system 
        Configuration conf = new Configuration (); 
             
        Conf.set ("Hadoop.job.ugi", "Hadoop-user,hadoop-user"); 
        Experiment 1: View the meta information of a file in HDFs System.out.println ("Experiment 1: View meta information for a file in HDFs"); 
String Fileuri = args[0];        FileSystem filefs = Filesystem.get (Uri.create (Fileuri), conf); 
        Filestatus filestatus = filefs.getfilestatus (new Path (Fileuri)); 
        Gets the basic information for this file if (Filestatus.isdir () ==false) {System.out.println ("This is a file"); 
        } System.out.println ("File path:" +filestatus.getpath ()); 
        System.out.println ("File Length:" +filestatus.getlen ()); 
        SYSTEM.OUT.PRINTLN ("File modification Date:" +new Timestamp (Filestatus.getmodificationtime ()). ToString ()); 
        System.out.println ("File Last accessed Date:" +new Timestamp (Filestatus.getaccesstime ()). ToString ()); 
        System.out.println ("Number of File Backups:" +filestatus.getreplication ()); 
        System.out.println ("Block size of File:" +filestatus.getblocksize ()); 
        System.out.println ("File owner:" +filestatus.getowner ()); 
        System.out.println ("The group in which the file is located:" +filestatus.getgroup ()); 
        System.out.println ("File Permissions:" +filestatus.getpermission (). toString ()); 
             
        System.out.println (); 
Experiment 2: View meta information for a file in HDFs        SYSTEM.OUT.PRINTLN ("Experiment 2: View meta information for a directory in HDFs"); 
        String Diruri = args[1]; 
        FileSystem dirfs = Filesystem.get (Uri.create (Diruri), conf); 
        Filestatus dirstatus = dirfs.getfilestatus (new Path (Diruri)); 
        Gets the basic information for this directory if (Dirstatus.isdir () ==true) {System.out.println ("This is a directory"); 
        } System.out.println ("Directory Path:" +dirstatus.getpath ()); 
        System.out.println ("Directory Length:" +dirstatus.getlen ()); 
        SYSTEM.OUT.PRINTLN ("Directory Modification Date:" +new Timestamp (Dirstatus.getmodificationtime ()). ToString ()); 
        System.out.println ("Directory Last accessed Date:" +new Timestamp (Dirstatus.getaccesstime ()). ToString ()); 
        System.out.println ("Number of directory Backups:" +dirstatus.getreplication ()); 
        System.out.println ("Block Size of directory:" +dirstatus.getblocksize ()); 
        System.out.println ("Directory Owner:" +dirstatus.getowner ()); 
        System.out.println ("The group where the directory is located:" +dirstatus.getgroup ()); 
      System.out.println ("Permissions for Directory:" +dirstatus.getpermission (). toString ());  SYSTEM.OUT.PRINTLN ("This directory contains the following files or directories:"); 
        For (Filestatus fs:dirFS.listStatus (New Path (Diruri))) {System.out.println (Fs.getpath ()); } 
             
             
    } 
     
}

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.