通过使用 shell 脚本过滤修改日期,将文件从 hdfs 文件夹复制到另一个 hdfs 位置

Copy files from a hdfs folder to another hdfs location by filtering with modified date using shell script

我的 hdfs 位置有 1 年的数据,我想将过去 6 个月的数据复制到另一个 hdfs 位置。 是否可以直接从 hdfs 命令复制 6 个月的数据,或者我们是否需要编写 shell 脚本来复制最近 6 个月的数据?

我已经尝试使用 hdfs 命令来执行此操作,但没有成功。

我尝试使用下面的 shell 脚本,在创建 TempFile 之前它工作正常但抛出错误

$ sh scriptnew.sh
scriptnew.sh: line 8: syntax error: unexpected end of file

并且脚本不会进一步执行。

下面是我使用的 shell 脚本。

#!/bin/bash
hdfs dfs -ls /hive/warehouse/data.db/all_history/ |awk 'BEGIN{ SIXMON=60*60*24*180; "date +%s" | getline NOW } { cmd="date -d'\''"" ""'\'' +%s"; cmd | getline WHEN; DIFF=NOW-SIXMON; if(WHEN > DIFF){print }}' >> TempFile.txt
cat TempFile.txt |while read line
do
    echo $i
    hdfs dfs -cp -p $line /user/can_anns/all_history_copy/;
done

可能是什么错误以及如何解决这个问题?

我认为您可以通过如下所示的 shell 脚本在三个 运行 秒内完成。这只是您脚本的修改版本。我试过了,它对我有用。

在每个运行中,需要修改grep条件为三个月所需的月份。 (2019-03, 2019-02, 2019-01)

脚本:

hdfs dfs -ls /hive/warehouse/data.db/all_history/|grep "2019-03"|awk '{print }' >> Files.txt
cat Files.txt |while read line
do
    echo $i
    hdfs dfs -cp $line /user/can_anns/all_history_copy/;
done

希望对您有所帮助!

我假设数据集有日期列。因此,您可以在该数据集上创建一个外部配置单元 table 并仅提取所需的数据。

如果给定日期有大量记录,shell 脚本运行速度非常慢。

要将 6 个月的文件从 hdfs 位置复制到另一个位置,我们可以使用以下脚本。

脚本应 运行 来自您当地的 linux 位置。

#!/bin/bash
hdfs dfs -ls /hive/warehouse/data.db/all_history/ |awk 'BEGIN{ SIXMON=60*60*24*180; "date +%s" | getline NOW } { cmd="date -d'\''"" ""'\'' +%s"; cmd | getline WHEN; DIFF=NOW-SIXMON; if(WHEN > DIFF){print }}' >> TempFile.txt
cat TempFile.txt |while read line
do
   echo $i
   hdfs dfs -cp -p $line /user/can_anns/all_history_copy/;
done

Line 2 : We are copying list of files which are of max 180 days to a TempFile. Then we iterate through this Temp file and if match is found then copy the file.

如果您从 windows 编写脚本并复制到 linux 机器,有时它可能无法工作并显示语法错误。 为避免回车 return 错误,将脚本复制到 linux 机器本地路径 运行 后执行以下命令。 sed -i 's/\r//' 然后 运行 脚本 >>> sh FileName.sh