如何从hadoop序列文件中获取最后修改的日期?

jjjwad0x  于 2021-05-30  发布在  Hadoop
关注(0)|答案(4)|浏览(541)

我使用的Map器将二进制文件(JPEG)转换为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()));

然后我有第二个Map器读取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());

计算结果是:

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 的笨蛋。蒂亚!

ffdz8vbo

ffdz8vbo1#

使用以下代码段获取在您提供的特定目录路径下修改的所有文件的Map:

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

inkz8wg92#

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

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

通过这几处更改,您可以获得每个inputslipt的文件状态,并可以将其添加到密钥中,以便稍后在您的过程中使用,或者进行多次输出reduce并在reduce阶段的其他地方写入。
我希望这会有用

scyqe7ek

scyqe7ek3#

在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);

我希望这对你有帮助。

yyhrrdl8

yyhrrdl84#

我没怎么用过hadoop,但我觉得你不应该用 file.lastModified() . hadoop在某种程度上抽象了文件系统。
你试过在中使用filesystem.getfilestatus(path)吗 map ? 它将为您提供一个具有修改时间的filestatus对象。像这样的

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

相关问题