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

标签 shell hadoop copy hdfs distcp

我的 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'\''"$6" "$7"'\'' +%s"; cmd | getline WHEN; DIFF=NOW-SIXMON; if(WHEN > DIFF){print $8}}' >> TempFile.txt
cat TempFile.txt |while read line
do
    echo $i
    hdfs dfs -cp -p $line /user/can_anns/all_history_copy/;
done

可能是什么错误以及如何解决?

最佳答案

要将 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'\''"$6" "$7"'\'' +%s"; cmd | getline WHEN; DIFF=NOW-SIXMON; if(WHEN > DIFF){print $8}}' >> 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 机器,有时它可能无法显示语法错误。 为避免回车错误,将脚本复制到 linux 机器本地路径后运行以下命令。 sed -i 's/\r//' 然后运行脚本 >>> sh FileName.sh

关于shell - 通过使用 shell 脚本过滤修改日期,将文件从 hdfs 文件夹复制到另一个 hdfs 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57054087/

相关文章:

linux - 同时循环两个列表

bash - 删除函数定义(unalias 等效)

java - 从Sqoop到Sap Hana的连接失败

hadoop - Namenode如何决定在哪个datanode中写入一个 block

hadoop - 亚马逊弹性 map 减少 : Command Line Tools installation Issue?

c++ - 在 C++ ubuntu linux 中运行 shell 命令时出错

linux - 是否有任何约定可以将不同软件的部分可持续地放置在一个文件中

windows - Cmake 文件跨驱动器复制

c++ - 复制-nan为float和AVX __m256复制后显示0

java - java中的方法以数组作为参数,复制第一个元素,然后将长度加倍?