2011-03-31 10 views
14

मैं जावा एपीआई का उपयोग कर एचडीएफएस में फ़ाइलों को स्थानांतरित करना चाहता हूं। मैं ऐसा करने का कोई तरीका नहीं समझ सकता। फ़ाइलसिस्टम क्लास केवल स्थानीय फाइल सिस्टम से और जाने की अनुमति देना चाहता है .. लेकिन मैं उन्हें एचडीएफएस में रखना चाहता हूं और उन्हें वहां ले जाना चाहता हूं।जावा एपीआई का उपयोग करके हडोप में फ़ाइलों को स्थानांतरित करना?

क्या मुझे कुछ बुनियादी याद आ रही है? ऐसा करने का एकमात्र तरीका यह है कि इसे इनपुट स्ट्रीम से पढ़ना और उसे वापस लिखना है ... और फिर पुरानी प्रतिलिपि (यक) हटा दें।

धन्यवाद

उत्तर

17

उपयोग FileSystem.rename():

public abstract boolean rename(Path src, Path dst) throws IOException 

Renames Path src to Path dst . Can take place on local fs or remote DFS.

Parameters:
src - path to be renamed
dst - new path after rename
Returns:
true if rename is successful
Throws:
IOException - on failure

+0

डांग करने के लिए ... कैसे मुझे लगता है कि याद आती है किया? धन्यवाद!!! – Wanderer

+1

'FileSystem.rename()' एक सार विधि है। तो यह कैसे काम करेगा? – zeekvfu

+0

आपको फ़ाइलसिस्टम कार्यान्वयन प्राप्त करना होगा जो उपयोग में फाइल सिस्टम से संबंधित है (उदा। एचडीएफएस)। 'फाइलसिस्टम fs = myPath.getFileSystem (config); fs.rename (myPath, otherPath); ' –

0
hdfsDirectory="hdfs://srcPath" 
val conf = new org.apache.hadoop.conf.Configuration() 
     val src:Path = new org.apache.hadoop.fs.Path(hdfsDirectory) 
     val fs = FileSystem.get(src.toUri,conf) 
     val srcPath: Path = new Path("hdfs://srcPath") 
     val srcFs =FileSystem.get(srcPath.toUri,conf) 
     val dstPath:Path =new Path("hdfs://targetPath/") 
     val dstFs =FileSystem.get(dstPath.toUri,conf) 
     val exists = fs.exists(new org.apache.hadoop.fs.Path(hdfsDirectory)) 
     val status:Array[FileStatus] = fs.listStatus(new Path(hdfsDirectory)) 
     if (status.length>0) { 
      status.foreach(x => { 
      println("My files: " + x.getPath) 
      FileUtil.copy(srcFs, x.getPath, dstFs, dstPath, true, conf) 
      println("Files moved !!" +x.getPath) 
      } 
     )} 
     else{ 
      println("No Files Found !!") 
     } 
3

java.nio। * दृष्टिकोण हमेशा HDFS पर कार्य न करें। तो निम्नलिखित समाधान मिल गया जो काम करता है।

एक निर्देशिका से ले जाएँ फ़ाइलें एक और org.apache.hadoop.fs.FileUtil.copy एपीआई का उपयोग कर

val fs = FileSystem.get(new Configuration()) 
     val conf = new org.apache.hadoop.conf.Configuration() 
     val srcFs = FileSystem.get(new org.apache.hadoop.conf.Configuration()) 
     val dstFs = FileSystem.get(new org.apache.hadoop.conf.Configuration()) 
     val dstPath = new org.apache.hadoop.fs.Path(DEST_FILE_DIR) 

     for (file <- fileList) { 
      // The 5th parameter indicates whether source should be deleted or not 
      FileUtil.copy(srcFs, file, dstFs, dstPath, true, conf) 
+0

देखें चाल फ़ाइलों के मामले में दिलचस्प दिखने में आसान दिखता है – Nitin

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