我正在使用将 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/