【发布时间】:2018-06-30 23:24:57
【问题描述】:
我希望在 UNIX 中使用类似 touch 命令来更改文件的最后修改日期。 触摸-d 20120101
hdfs 的等效 touchz 命令不支持此功能。 hadoop fs -touchz -d 20120101
有什么方法可以使用 UNIX 或其他任何方式更改 Hadoop 中文件的最后修改日期?
【问题讨论】:
我希望在 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 中保留并反映相同的时间。
如果您担心每次创建新文件并更新它,您可以使用-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/
【讨论】:
据我所知,没有 shell 命令可以做到这一点。
但是可以通过JavaAPI来完成
public void setTimes(路径 p, 很长的时间, 好久不见) 抛出 IOException
设置文件的访问时间。
参数: p - 路径 mtime - 设置此文件的修改时间。自 1970 年 1 月 1 日以来的毫秒数。值 -1 表示此调用 不应设置修改时间。 atime - 设置此文件的访问时间。自 1970 年 1 月 1 日以来的毫秒数。值 -1 表示此调用 不应设置访问时间。
【讨论】:
Hadoop 确实提供了这种功能
一般的命令行语法是: 命令 [genericOptions] [commandOptions]
用法:hadoop fs [通用选项] -touch [-a] [-m] [-t TIMESTAMP ] [-c] ...
【讨论】: