file - 从 UNIX 更改 HDFS 文件创建日期

标签 file unix hadoop hdfs

我希望在 UNIX 中使用类似于 touch 命令的命令来更改文件的最后修改日期。 触摸-d 20120101

hdfs 的等效 touchz 命令不支持此功能。 hadoop fs -touchz -d 20120101

有没有什么方法可以使用 UNIX 或其他任何工具更改 Hadoop 中文件的最后修改日期?

最佳答案

如果您不想按照@SCouto 的建议编写 java 代码,您可以通过一个简单的解决方法来实现,下面是我对如何实现它的解释。

#Changing the file timestamp to 201708210100 in local unix file system
[root@quickstart TestFolder]# touch -t 201708210100 SomeTestFile.txt 

[root@quickstart TestFolder]# ls -lh
total 0
-rw-r--r-- 1 root root 0 Aug 21 01:00 SomeTestFile.txt

#when copying the file to hdfs i'm using -p option which preserves the file timestamp
[root@quickstart TestFolder]# hdfs dfs -copyFromLocal -p SomeTestFile.txt /Temp

#After copying the file if you look at the below TS its reflected the same way in as in local
[root@quickstart TestFolder]# hdfs dfs -ls /Temp/SomeTestFile.txt
-rw-r--r--   1 root root          0 2017-08-21 01:00 /Temp/SomeTestFile.txt

P.S - 更改本地文件系统时间,在将文件复制到 hdfs 时使用 -p,这将在 HDFS 中保留和反射(reflect)相同的时间。

如果您担心创建一个新文件并每次都更新它,您可以使用 -f 执行类似下面的操作,这会覆盖/强制文件

#HDFS FILE SomeTestFile.txt
hdfs dfs -ls /Temp/SomeTestFile.txt

#To change the file TS for SomeTestFile.txt #Get it to local
hdfs dfs -get /Temp/SomeTestFile.txt /SomeFolderInLinux/

#Change the time in local with touch
touch -t 201701010100 /SomeFolderInLinux/SomeTestFile.txt

#Here is the main part of preserving the time and overwriting the file in hdfs
hdfs dfs -copyFromLocal -p -f /SomeFolderInLinux/SomeTestFile.txt /Temp/

关于file - 从 UNIX 更改 HDFS 文件创建日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48379743/

相关文章:

java - 如何使用文件列表中的一个文件

linux - 数 = 'ps -ef | grep -v grep| grep BatchName| wc -l'

apache - 向 Oozie 工作流通知添加授权

ubuntu - 安装 HBase 资源管理器

hadoop - 如何为开源 apache-hadoop 集成/安装 Beeline UI Editor

python - 从文本文件中提取字符

c - 为什么 C 文件流的开头称为 `SEEK_SET` ?

java - Java从csv文件读取数据

linux - 删除在脚本中不起作用

linux - 如何比较 unix (Linux) 中的 2 个符号链接(symbolic link)?