Sto usando un mapper che converte BinaryFiles (jpeg) ad una sequenza di file Hadoop (HSF):Come ottengo la data dell'ultima modifica da un file di sequenza 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()));
Ho poi hanno una seconda mapper che legge il HSF, in tal modo:
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());
e 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;
Questo tutto funziona benissimo, ma voglio calculatePhash di scrivere data dell'ultima modifica di ogni jpeg. So che posso usare file.lastModified()
per ottenere l'ultima data di modifica in un file, ma c'è un modo per ottenere questo in una mappa o in un calcoloPhash? Sono un noob in Java. TIA!
Aggiungilo alla chiave! Così ovvio ora. Grazie!! – schoon