Java 如何从Hadoop序列文件中获取上次修改的日期?
我正在使用将二进制文件(JPEG)转换为Hadoop序列文件(HSF)的映射器: 然后我有第二个映射器读取HSF,因此:Java 如何从Hadoop序列文件中获取上次修改的日期?,java,date,hadoop,mapreduce,Java,Date,Hadoop,Mapreduce,我正在使用将二进制文件(JPEG)转换为Hadoop序列文件(HSF)的映射器: 然后我有第二个映射器读取HSF,因此: public class ImagePHashMapper extends Mapper<Text, BytesWritable, Text, Text>{ public void map(Text key, BytesWritable value, Context context) throws IOException,InterruptedExce
public class ImagePHashMapper extends Mapper<Text, BytesWritable, Text, Text>{
public void map(Text key, BytesWritable value, Context context) throws IOException,InterruptedException {
//get the PHash for this specific file
String PHashStr;
try {
PHashStr = calculatePhash(value.getBytes());
这一切都很好,但我希望calculatePhash写出每个jpeg的最后修改日期。我知道我可以使用
file.lastModified()
在文件中获取上次修改的日期,但有没有办法在map或CalculateFash中获取?我是爪哇大学的一名学生。蒂亚 没有太多使用Hadoop,但我认为您不应该使用file.lastModified()
。Hadoop在某种程度上抽象了文件系统
你试过在地图中使用吗?它将为您获取一个具有修改时间的对象。差不多
FileSystem fs = FileSystem.get(URI.create(uri), conf);
long moddificationTime = fs.getFileStatus(new Path(uri)).lastModified();
使用以下代码段获取在您提供的特定目录路径下修改的所有文件的映射:
private static HashMap lastModifiedFileList(FileSystem fs, Path rootDir) {
// TODO Auto-generated method stub
HashMap modifiedList = new HashMap();
try {
FileStatus[] status = fs.listStatus(rootDir);
for (FileStatus file : status) {
modifiedList.put(file.getPath(), file.getModificationTime());
}
} catch (IOException e) {
e.printStackTrace();
}
return modifiedList;
}
您好,我想您想要的是在您的映射器中输入的每个输入文件的修改时间。如果是这种情况,您只需在mpkorstanje解决方案中添加几行:
FileSystem fs=FileSystem.get(URI.create(URI),conf);
长ModDefinitionTime=fs
.getFileStatus((FileSplit)context.getInputSplit())
.getPath()).lastModified();
通过这几项更改,您可以获得每个inputSlipt的文件状态,并将其添加到密钥中,以便稍后在流程中使用,或者进行多次输出reduce,并在reduce阶段的其他地方写入
我希望这在Hadoop中有用,每个文件都由块组成。 通常Hadoop文件系统是指包org.apache.Hadoop.fs。 如果您的输入文件存在于HDFS中,则意味着您需要导入上述软件包
FileSystem fs = FileSystem.get(URI.create(uri), conf);
in = fs.open(new Path(uri));
org.apache.hadoop.fs.FileStatus fileStatus=fs.getFileStatus(new Path(uri));
long modificationDate = fileStatus.getModificationTime();
Date date=new Date(modificationDate);
SimpleDateFormat df2 = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
String dateText = df2.format(date);
我希望这能对您有所帮助。以上答案与您的答案有什么区别?看起来两个都一样。把它加到钥匙上!现在很明显。谢谢我可以使用
.lastModified
方法,但是我得到了错误:50:error:value lastModified不是org.apache.hadoop.fs.FileStatus fs.getFileStatus(新路径(f))的成员。lastModified()是否仅在org.apache.hadoop.fs
的某些版本中可用?
private static HashMap lastModifiedFileList(FileSystem fs, Path rootDir) {
// TODO Auto-generated method stub
HashMap modifiedList = new HashMap();
try {
FileStatus[] status = fs.listStatus(rootDir);
for (FileStatus file : status) {
modifiedList.put(file.getPath(), file.getModificationTime());
}
} catch (IOException e) {
e.printStackTrace();
}
return modifiedList;
}
FileSystem fs = FileSystem.get(URI.create(uri), conf);
in = fs.open(new Path(uri));
org.apache.hadoop.fs.FileStatus fileStatus=fs.getFileStatus(new Path(uri));
long modificationDate = fileStatus.getModificationTime();
Date date=new Date(modificationDate);
SimpleDateFormat df2 = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
String dateText = df2.format(date);