소스 검색

HADOOP-2865. FsShell.ls() printout format changed to print file names in the end of the line. Contributed by Edward J. Yoon.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/trunk@648886 13f79535-47bb-0310-9956-ffa450edef68
Konstantin Shvachko 17 년 전
부모
커밋
0d06f3915e
2개의 변경된 파일38개의 추가작업 그리고 13개의 파일을 삭제
  1. 3 0
      CHANGES.txt
  2. 35 13
      src/java/org/apache/hadoop/fs/FsShell.java

+ 3 - 0
CHANGES.txt

@@ -9,6 +9,9 @@ Trunk (unreleased changes)
     that are being written to. The output of fsck is incompatible
     with previous release. (lohit vijayarenu via dhruba) 
 
+    HADOOP-2865. FsShell.ls() printout format changed to print file names
+    in the end of the line. (Edward J. Yoon via shv)
+
   NEW FEATURES
 
     HADOOP-3074. Provides a UrlStreamHandler for DFS and other FS,

+ 35 - 13
src/java/org/apache/hadoop/fs/FsShell.java

@@ -40,7 +40,6 @@ import org.apache.hadoop.io.WritableComparable;
 import org.apache.hadoop.ipc.RPC;
 import org.apache.hadoop.ipc.RemoteException;
 import org.apache.hadoop.util.ReflectionUtils;
-import org.apache.hadoop.util.StringUtils;
 import org.apache.hadoop.util.Tool;
 import org.apache.hadoop.util.ToolRunner;
 import org.apache.hadoop.dfs.DistributedFileSystem;
@@ -54,6 +53,7 @@ public class FsShell extends Configured implements Tool {
     new SimpleDateFormat("yyyy-MM-dd HH:mm");
   protected static final SimpleDateFormat modifFmt =
     new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+  static final int BORDER = 2;
   static {
     modifFmt.setTimeZone(TimeZone.getTimeZone("UTC"));
   }
@@ -632,19 +632,34 @@ public class FsShell extends Configured implements Tool {
           System.out.println("Found " + items.length + " items");
         }
       }
+      
+      int maxReplication = 3, maxLen = 10, maxOwner = 0,maxGroup = 0;
+
+      for(int i = 0; i < items.length; i++) {
+        FileStatus stat = items[i];
+        int replication = String.valueOf(stat.getReplication()).length();
+        int len = String.valueOf(stat.getLen()).length();
+        int owner = String.valueOf(stat.getOwner()).length();
+        int group = String.valueOf(stat.getGroup()).length();
+        
+        if (replication > maxReplication) maxReplication = replication;
+        if (len > maxLen) maxLen = len;
+        if (owner > maxOwner)  maxOwner = owner;
+        if (group > maxGroup)  maxGroup = group;
+      }
+      
       for (int i = 0; i < items.length; i++) {
         FileStatus stat = items[i];
         Path cur = stat.getPath();
         String mdate = dateForm.format(new Date(stat.getModificationTime()));
-        System.out.println(cur.toUri().getPath() + "\t" 
-                           + (stat.isDir() ? 
-                              "<dir>\t" : 
-                              ("<r " + stat.getReplication() 
-                               + ">\t" + stat.getLen()))
-                           + "\t" + mdate 
-                           + "\t" + stat.getPermission()
-                           + "\t" + stat.getOwner() 
-                           + "\t" + stat.getGroup());
+        System.out.printf("%-"+ (maxReplication + BORDER)
+            +"s", (!stat.isDir() ? stat.getReplication() : ""));
+        System.out.printf("%-"+ (maxLen + BORDER) +"d", stat.getLen());
+        System.out.print(mdate + "  " 
+            + (stat.isDir() ? "d" : "-") + stat.getPermission() + "  ");
+        System.out.printf("%-"+ (maxOwner + BORDER) +"s", stat.getOwner());
+        System.out.printf("%-"+ (maxGroup + BORDER) +"s", stat.getGroup());
+        System.out.println(cur.toUri().getPath());
         if (recursive && stat.isDir()) {
           ls(cur,srcFs, recursive, printHeader);
         }
@@ -670,10 +685,17 @@ public class FsShell extends Configured implements Tool {
             + ": No such file or directory.");
     } else {
       System.out.println("Found " + items.length + " items");
+      int maxLength = 10;
+      
       for (int i = 0; i < items.length; i++) {
-        System.out.println(items[i].getPath() + "\t" + 
-                           srcFs.getContentSummary(items[i].getPath()).
-                           getLength());
+        String size = String.valueOf(srcFs.getContentSummary(items[i]
+                                                                   .getPath()).getLength());
+        if (size.length() > maxLength) maxLength = size.length();
+      }
+      for(int i = 0; i < items.length; i++) {
+        System.out.printf("%-"+ (maxLength + BORDER) +"d", 
+            srcFs.getContentSummary(items[i].getPath()).getLength());
+        System.out.println(items[i].getPath());
       }
     }
   }