有没有办法在HDFS上删除超过10天的文件?
在Linux中我会使用:
find /path/to/directory/ -type f -mtime +10 -name '*.txt' -execdir rm -- {} \;
Run Code Online (Sandbox Code Playgroud)
有没有办法在HDFS上执行此操作?(根据文件创建日期删除)
dae*_*n12 10
这个怎么样:
hdfs dfs -ls /tmp | tr -s " " | cut -d' ' -f6-8 | grep "^[0-9]" | awk 'BEGIN{ MIN=14400; LAST=60*MIN; "date +%s" | getline NOW } { cmd="date -d'\''"$1" "$2"'\'' +%s"; cmd | getline WHEN; DIFF=NOW-WHEN; if(DIFF > LAST){ print "Deleting: "$3; system("hdfs dfs -rm -r "$3) }}'
Run Code Online (Sandbox Code Playgroud)
详细说明是这里.
解决方案1:使用 daemon12回答的多个命令
hdfs dfs -ls /file/Path | tr -s " " | cut -d' ' -f6-8 | grep "^[0-9]" | awk 'BEGIN{ MIN=14400; LAST=60*MIN; "date +%s" | getline NOW } { cmd="date -d'\''"$1" "$2"'\'' +%s"; cmd | getline WHEN; DIFF=NOW-WHEN; if(DIFF > LAST){ print "Deleting: "$3; system("hdfs dfs -rm -r "$3) }}'
Run Code Online (Sandbox Code Playgroud)
解决方案2: 使用Shell脚本
today=`date +'%s'`
hdfs dfs -ls /file/Path/ | grep "^d" | while read line ; do
dir_date=$(echo ${line} | awk '{print $6}')
difference=$(( ( ${today} - $(date -d ${dir_date} +%s) ) / ( 24*60*60 ) ))
filePath=$(echo ${line} | awk '{print $8}')
if [ ${difference} -gt 10 ]; then
hdfs dfs -rm -r $filePath
fi
done
Run Code Online (Sandbox Code Playgroud)