소스 검색

HADOOP-738. Extend FsShell '-put' and '-get' commands to accept standard input and output, respectively. Contributed by Wendy.

git-svn-id: https://svn.apache.org/repos/asf/lucene/hadoop/trunk@489175 13f79535-47bb-0310-9956-ffa450edef68
Doug Cutting 18 년 전
부모
커밋
1d54b0c2e3
2개의 변경된 파일47개의 추가작업 그리고 8개의 파일을 삭제
  1. 4 0
      CHANGES.txt
  2. 43 8
      src/java/org/apache/hadoop/fs/FsShell.java

+ 4 - 0
CHANGES.txt

@@ -130,6 +130,10 @@ Trunk (unreleased changes)
 
 36. HADOOP-814.  Optimize locking in namenode. (Dhruba Borthakur via cutting) 
 
+37. HADOOP-738.  Change 'fs -put' and 'fs -get' commands to accept
+    standard input and output, respectively.  Standard i/o is
+    specified by a file named '-'.  (Wendy Chien via cutting)
+
 
 Release 0.9.2 - 2006-12-15
 

+ 43 - 8
src/java/org/apache/hadoop/fs/FsShell.java

@@ -38,11 +38,39 @@ public class FsShell extends ToolBase {
         conf.setQuietMode(true);
         this.fs = FileSystem.get(conf);
     }
+
+    /**
+     * Copies from stdin to the indicated file.
+     */
+    private void copyFromStdin(Path dst) throws IOException {
+      if (fs.isDirectory(dst)) {
+        throw new IOException("When source is stdin, destination must be a file.");
+      }
+      if (fs.exists(dst)) {
+        throw new IOException("Target " + dst.toString() + " already exists.");
+      }
+      FSDataOutputStream out = fs.create(dst); 
+      byte buf[] = new byte[conf.getInt("io.file.buffer.size", 4096)];
+      try {
+        int bytesRead = System.in.read(buf);
+        while (bytesRead >= 0) {
+          out.write(buf, 0, bytesRead);
+          bytesRead = System.in.read(buf);
+        }
+      } finally {
+        out.close();
+      }
+    }
+
     /**
      * Add a local file to the indicated FileSystem name. src is kept.
      */
     void copyFromLocal(Path src, String dstf) throws IOException {
+      if (src.toString().equals("-")) {
+        copyFromStdin(new Path(dstf));
+      } else {
         fs.copyFromLocalFile(src, new Path(dstf));
+      }
     }
 
     /**
@@ -74,14 +102,21 @@ public class FsShell extends ToolBase {
       }
       String srcf = argv[pos++];
       String dstf = argv[pos++];
-      Path [] srcs = fs.globPaths( new Path(srcf) );
-      if( srcs.length > 1 && !new File( dstf ).isDirectory()) {
-        throw new IOException( "When copy multiple files, " 
-            + "destination should be a directory." );
-      }
-      Path dst = new Path( dstf );
-      for( int i=0; i<srcs.length; i++ ) {
-        fs.copyToLocalFile( srcs[i], dst, copyCrc );
+      if( dstf.equals("-")) {
+        if (copyCrc) {
+          System.err.println("-crc option is not valid when destination is stdout.");
+        }
+        cat(srcf);
+      } else {
+        Path [] srcs = fs.globPaths( new Path(srcf) );
+        if( srcs.length > 1 && !new File( dstf ).isDirectory()) {
+          throw new IOException( "When copying multiple files, " 
+                                 + "destination should be a directory." );
+        }
+        Path dst = new Path( dstf );
+        for( int i=0; i<srcs.length; i++ ) {
+          fs.copyToLocalFile( srcs[i], dst, copyCrc );
+        }
       }
     }