Thursday, 25 July 2013

Hadoop file manipulations in java

  



            Configuration config = new Configuration();
            FileSystem hdfs = FileSystem.get(config);
            Path path = new Path(fileName);
            FileStatus fileStatus = hdfs.getFileStatus(path);


In order to get the last modification and access time of a file in Hadoop file system:
 
            long modificationTime = fileStatus.getModificationTime();
            long accessTime = fileStatus.getAccessTime();
            


In order to get the replication and block size of a file in Hadoop file system:
            short replica = fileStatus.getReplication();
            long blockSize = fileStatus.getBlockSize();
            


In order to get the group and owner of a file in Hadoop file system:
            String group= fileStatus.getGroup();
            String owner =fileStatus.getOwner();


List All Files in the directory:
            if (fileStatus.isDir())
            {               
              FileStatus[] status = fs.listStatus(path);
              for (int i=0;i<status.length;i++){
                 Path cur =
status[i].getPath(); 
                 System.out.println("cur.toUri().getPath()");            
               }

            }else{

                 System.out.println(fileName+": is not a directory");
            }

No comments:

Post a Comment