Reply
New Contributor
Posts: 3
Registered: ‎08-24-2017

How to get information on file last accesstime in hdfs on hadoop 2.6.0 CDH 5.10

Hi

 

I need to get information about the last access times for files on HDFS. My version is Hadoop 2.6.0-cdh5.10.

I have dfs.access.time.precision set to 1hr. I can see in 2.7.1 there is -u parameter for "hdfs dfs ls" but it doesn't work on 2.6.0.

 

Please share info on what is possible in 2.6.0 and if the accessstime is being marked on files.

 

Regards

Champion
Posts: 545
Registered: ‎05-16-2016

Re: How to get information on file last accesstime in hdfs on hadoop 2.6.0 CDH 5.10

you enable audit but its going to generate  more logs or you could use the below code that comes handy 

import java.io.*;
import java.util.*;
import java.net.*;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;
 
 
// For Date Conversion from long to readable.
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
public class FileStatusChecker {
    public static void main (String [] args) throws Exception {
        try{
            FileSystem fs = FileSystem.get(new Configuration());
            String hdfsFilePath = "hdfs://My-NN-HA/Demos/SparkDemos/inputFile.txt";
            FileStatus[] status = fs.listStatus(new Path(hdfsFilePath));  // you need to pass in your hdfs path
 
            for (int i=0;i<status.length;i++){
                long lastAccessTimeLong = status[i].getAccessTime();
                Date lastAccessTimeDate = new Date(lastAccessTimeLong);
                DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
                System.out.println("The file '"+ hdfsFilePath + "' was accessed last at: "+ df.format(lastAccessTimeDate));
            }
        }catch(Exception e){
            System.out.println("File not found");
            e.printStackTrace();
        }
    }
}

hope this is suffice. 

Highlighted
New Contributor
Posts: 3
Registered: ‎08-24-2017

Re: How to get information on file last accesstime in hdfs on hadoop 2.6.0 CDH 5.10

Thank you. I will use it as a scaffolding to build a small program over it.

Announcements