2014-11-14 5 views
7

मैं एक Hadoop अनुक्रम फ़ाइल के लिए एक नक्शाकार कि धर्मान्तरित BinaryFiles (JPEG) का उपयोग कर रहा (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() का उपयोग कर सकता हूं लेकिन क्या इसे मानचित्र या गणना में लाने का कोई तरीका है? मैं जावा पर एक नोब हूँ। TIA!

उत्तर

9

हाय मुझे लगता है कि जो आप चाहते प्रत्येक इनपुट फ़ाइल के संशोधन का समय है जो आपके मैपर में प्रवेश करता है। आप प्रत्येक inputSlipt की fileStatus प्राप्त कर सकते हैं इस में कुछ परिवर्तन करके

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

और आप बाद में उपयोग करने के लिए अपने प्रमुख में जोड़ सकते हैं: यदि यह मामला है तो आप सिर्फ mpkorstanje समाधान के लिए कुछ लाइनें जोड़ने के लिए अपनी प्रक्रिया में या एकाधिक कम्यूट को कम करें और अपने कम चरण में कहीं और लिखें।

मुझे आशा है कि यह उपयोगी होगा

+2

इसे कुंजी में जोड़ें! तो अब स्पष्ट है। धन्यवाद!! – schoon

5

हैडोप का बहुत उपयोग नहीं किया है, लेकिन मुझे नहीं लगता कि आपको file.lastModified() का उपयोग करना चाहिए। हडोप ने कुछ हद तक फाइल सिस्टम को सारणीबद्ध किया।

क्या आपने FileSystem.getFileStatus(path)map में उपयोग करने का प्रयास किया है? यह आपको 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 में प्रत्येक फ़ाइलें ब्लॉक से मिलकर कर रहे हैं। आम तौर पर हैडोप फ़ाइल सिस्टम को पैकेज 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

+2

उपरोक्त उत्तरों और आपके बीच क्या अंतर है? ऐसा लगता है कि दोनों एक ही हैं। – Kumar

संबंधित मुद्दे