java - 如何从 Hadoop 序列文件中获取最后修改日期?

标签 java date hadoop mapreduce

我正在使用将 BinaryFiles (jpegs) 转换为 Hadoop 序列文件 (HSF) 的映射器:

    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 菜鸟。 TIA!

最佳答案

您好,我认为您想要的是进入映射器的每个输入文件的修改时间。如果是这种情况,您只需在 mpkorstanje 解决方案中添加几行:

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

通过这几处更改,您可以获得每个 inputSlipt 的 fileStatus,您可以将其添加到您的 key 中以便稍后在您的流程中使用,或者使 multipleOutput 减少并在减少阶段写入其他地方。

希望对你有用

关于java - 如何从 Hadoop 序列文件中获取最后修改日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26936932/

相关文章:

java - 如何使用 xmlbeans 解析 XML 数据?

java - 如何处理异常并完成方法的功能而不提示用户已发生的异常

java - 有没有办法在 Quarkus 中动态设置 .env 文件的参数值?

python - 如何将字符串日期(YYYY-MM-DD)从 SQL 数据库转换为 python 中的 matplotlib 日期?

使用 Java Mapper/Reducer 的 Hadoop Streaming

apache-spark - 如何在单机上创建集群式hadoop环境?

java - ANTLR4 动态 token 类型

java - 在 Java 中创建日期

hadoop - 临时挂起 hadoop 节点 - 后台 hadoop 集群

ruby-on-rails - Ruby - 将星期几转换为整数