Get files which are created in last 5 minutes in hadoop using shell script Get files which are created in last 5 minutes in hadoop using shell script unix unix

Get files which are created in last 5 minutes in hadoop using shell script


How about this:

hdfs dfs -ls /tmp | tr -s " " | cut -d' ' -f6-8 | grep "^[0-9]" | awk 'BEGIN{ MIN=5; LAST=60*MIN; "date +%s" | getline NOW } { cmd="date -d'\''"$1" "$2"'\'' +%s"; cmd | getline WHEN; DIFF=NOW-WHEN; if(DIFF < LAST){ print $3 }}'

Explanation:

List all the files:

hdfs dfs -ls /tmp

Replace extra spaces:

tr -s " "

Get the required columns:

cut -d' ' -f6-8

Remove non-required rows:

grep "^[0-9]"

Processing using awk:

awk

Initialize the DIFF duration and current time:

MIN=5; LAST=60*MIN; "date +%s" | getline NOW

Create a command to get the epoch value for timestamp of the file on HDFS:

cmd="date -d'\''"$1" "$2"'\'' +%s";

Execute the command to get epoch value for HDFS file:

cmd | getline WHEN;

Get the time difference:

DIFF=NOW-WHEN;

Print the output depending upon the difference:

if(DIFF < LAST){ print $3 }

You just need to change the variable value for MIN depending upon your requirement (here its 5 minutes).HTH


I have done it using below command : it will give me files that are created between a five minute window :

hadoop fs -ls /tmp/logs/root/logs | awk '{ if ((($6 == "'"2016-08-18"'" && $7 <= "'"21:00"'") && ($6 == "'"2016-08-18"'" && $7 >= "'"20:55"'"))) print $8 } ' 

It can be modified accordingly with current time stamp.