2014-11-14 96 views
7

我使用的转换BinaryFiles(JPEG文件)映射器到一个Hadoop序列文件(HSF):如何从Hadoop序列文件获取最后修改日期?

public void map(Object key, Text value, Context context) 
throws IOException, InterruptedException { 

    String uri = value.toString().replace(" ", "%20"); 
    Configuration conf = new Configuration(); 

    FSDataInputStream in = null; 
    try { 
     FileSystem fs = FileSystem.get(URI.create(uri), conf); 
     in = fs.open(new Path(uri)); 
     java.io.ByteArrayOutputStream bout = new ByteArrayOutputStream(); 
     byte buffer[] = new byte[1024 * 1024]; 

     while(in.read(buffer, 0, buffer.length) >= 0) { 
      bout.write(buffer); 
     } 
     context.write(value, new BytesWritable(bout.toByteArray())); 

我然后有一个第二映射器,其读取所述HSF,从而:

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是:

 static String calculatePhash(byte[] imageData) throws NoSuchAlgorithmException { 
     //get the PHash for this specific data 
     //PHash requires inputstream rather than byte array 
     InputStream is = new ByteArrayInputStream(imageData); 
     String ph; 
     try { 
      ImagePHash ih = new ImagePHash(); 
      ph = ih.getHash(is); 
      System.out.println ("file: " + is.toString() + " phash: " +ph); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      return "Internal error with ImagePHash.getHash"; 
     } 

     return ph; 

这一切工作正常,但我想calculatePhash写出每个JPEG图像的最后修改日期。我知道我可以使用file.lastModified()获取文件中的最后修改日期,但是有什么方法可以在map或calculatePhash中获取此日期吗?我是Java的noob。 TIA!

回答

9

嗨,我认为你要的是每个输入文件的修改时间进入你的映射器。如果是这样的话,你只需要几行添加到mpkorstanje解决方案:

FileSystem fs = FileSystem.get(URI.create(uri), conf); 
long moddificationTime = fs 
    .getFileStatus((FileSplit)context.getInputSplit()) 
    .getPath()).lastModified(); 

有了这几个变化,你可以得到每个inputSlipt的时间filestatus,你可以为了以后使用它添加到您的钥匙在你的过程中,或者使一个多输出减少并写入缩小阶段的其他地方。

我希望这将是有用的

+2

添加到关键字!现在很明显。谢谢!! – schoon 2014-11-25 11:19:34

5

没有使用Hadoop的很多,但我不认为你应该使用file.lastModified()。 Hadoop在某种程度上抽象了文件系统。

您是否尝试过在map使用FileSystem.getFileStatus(path)?它会为您带来一个修改时间为FileStatus的对象。像

FileSystem fs = FileSystem.get(URI.create(uri), conf); 
long moddificationTime = fs.getFileStatus(new Path(uri)).lastModified(); 
1

东西使用下面的代码片段,让你提供在特定目录路径修改的所有文件的地图:

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; 
} 
0

在Hadoop中的每个文件都包括BLOCK的。 通常Hadoop FileSystem被引用包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); 

我希望这会帮助你。

+0

工作正常 – Rengasamy 2014-11-25 09:36:32

+2

上述答案和你的答案有什么不同?看起来两者都是一样的。 – Kumar 2014-11-25 09:39:34

相关问题