浏览代码

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
     that are being written to. The output of fsck is incompatible
     with previous release. (lohit vijayarenu via dhruba) 
     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
   NEW FEATURES
 
 
     HADOOP-3074. Provides a UrlStreamHandler for DFS and other FS,
     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.RPC;
 import org.apache.hadoop.ipc.RemoteException;
 import org.apache.hadoop.ipc.RemoteException;
 import org.apache.hadoop.util.ReflectionUtils;
 import org.apache.hadoop.util.ReflectionUtils;
-import org.apache.hadoop.util.StringUtils;
 import org.apache.hadoop.util.Tool;
 import org.apache.hadoop.util.Tool;
 import org.apache.hadoop.util.ToolRunner;
 import org.apache.hadoop.util.ToolRunner;
 import org.apache.hadoop.dfs.DistributedFileSystem;
 import org.apache.hadoop.dfs.DistributedFileSystem;
@@ -54,6 +53,7 @@ public class FsShell extends Configured implements Tool {
     new SimpleDateFormat("yyyy-MM-dd HH:mm");
     new SimpleDateFormat("yyyy-MM-dd HH:mm");
   protected static final SimpleDateFormat modifFmt =
   protected static final SimpleDateFormat modifFmt =
     new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+  static final int BORDER = 2;
   static {
   static {
     modifFmt.setTimeZone(TimeZone.getTimeZone("UTC"));
     modifFmt.setTimeZone(TimeZone.getTimeZone("UTC"));
   }
   }
@@ -632,19 +632,34 @@ public class FsShell extends Configured implements Tool {
           System.out.println("Found " + items.length + " items");
           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++) {
       for (int i = 0; i < items.length; i++) {
         FileStatus stat = items[i];
         FileStatus stat = items[i];
         Path cur = stat.getPath();
         Path cur = stat.getPath();
         String mdate = dateForm.format(new Date(stat.getModificationTime()));
         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()) {
         if (recursive && stat.isDir()) {
           ls(cur,srcFs, recursive, printHeader);
           ls(cur,srcFs, recursive, printHeader);
         }
         }
@@ -670,10 +685,17 @@ public class FsShell extends Configured implements Tool {
             + ": No such file or directory.");
             + ": No such file or directory.");
     } else {
     } else {
       System.out.println("Found " + items.length + " items");
       System.out.println("Found " + items.length + " items");
+      int maxLength = 10;
+      
       for (int i = 0; i < items.length; i++) {
       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());
       }
       }
     }
     }
   }
   }