Bladeren bron

HADOOP-222. Add -setrep option to the dfs commands that alters file replication levels.

git-svn-id: https://svn.apache.org/repos/asf/lucene/hadoop/trunk@410397 13f79535-47bb-0310-9956-ffa450edef68
Doug Cutting 19 jaren geleden
bovenliggende
commit
fa5a73f4cd
2 gewijzigde bestanden met toevoegingen van 85 en 3 verwijderingen
  1. 4 1
      CHANGES.txt
  2. 81 2
      src/java/org/apache/hadoop/dfs/DFSShell.java

+ 4 - 1
CHANGES.txt

@@ -73,11 +73,14 @@ Trunk (unreleased)
     nodes.  This, together with HADOOP-195, greatly improves the
     performance of these transfers.  (omalley via cutting)
 
-20. HADOOP-163.  Cause datanodes that are unable to either read or
+20. HADOOP-163.  Cause datanodes that\ are unable to either read or
     write data to exit, so that the namenode will no longer target
     them for new blocks and will replicate their data on other nodes.
     (Hairong Kuang via cutting)
 
+21. HADOOP-222.  Add a -setrep option to the dfs commands that alters
+    file replication levels.  (Johan Oskarson via cutting)
+
 
 Release 0.2.1 - 2006-05-12
 

+ 81 - 2
src/java/org/apache/hadoop/dfs/DFSShell.java

@@ -16,7 +16,6 @@
 package org.apache.hadoop.dfs;
 
 import java.io.*;
-import java.util.*;
 
 import org.apache.hadoop.conf.*;
 import org.apache.hadoop.fs.*;
@@ -82,6 +81,83 @@ public class DFSShell {
       }
     }
 
+    /**
+     * Parse the incoming command string
+     * @param cmd
+     * @param pos ignore anything before this pos in cmd
+     * @throws IOException 
+     */
+    private void setReplication(String[] cmd, int pos) throws IOException {
+      if(cmd.length-pos<2 || (cmd.length-pos==2 && cmd[pos].equalsIgnoreCase("-R"))) {
+        System.err.println("Usage: [-R] <repvalue> <path>");
+        System.exit(-1);
+      }
+      
+      boolean recursive = false;
+      short rep = 3;
+      
+      if("-R".equalsIgnoreCase(cmd[pos])) {
+        recursive=true;
+        pos++;
+        
+      }
+      
+      try {
+        rep = Short.parseShort(cmd[pos]);
+        pos++;
+      } catch (NumberFormatException e) {
+        System.err.println("Cannot set replication to: " + cmd[pos]);
+        System.exit(-1);
+      }
+      
+      setReplication(rep, new Path(cmd[pos]), recursive);
+    }
+    
+    /**
+     * Set the replication for the path argument
+     * if it's a directory and recursive is true,
+     * set replication for all the subdirs and those files too
+     */
+    public void setReplication(short newRep, Path src, boolean recursive) throws IOException {
+  	
+    	if(!fs.isDirectory(src)) {
+    		setFileReplication(src, newRep);
+    		return;
+    	}
+    	
+      Path items[] = fs.listPaths(src);
+      if (items == null) {
+      	System.out.println("Could not get listing for " + src);
+      } else {
+
+      	for (int i = 0; i < items.length; i++) {
+      		Path cur = items[i];
+       		if(!fs.isDirectory(cur)) {
+       			setFileReplication(cur, newRep);
+       		} else if(recursive) {
+       			setReplication(newRep, cur, recursive);
+       		}
+       	}
+       }
+    }
+    
+    /**
+     * Actually set the replication for this file
+     * If it fails either throw IOException or print an error msg
+     * @param file
+     * @param newRep
+     * @throws IOException
+     */
+    private void setFileReplication(Path file, short newRep) throws IOException {
+    	
+    	if(fs.setReplication(file, newRep)) {
+    		System.out.println("Replication " + newRep + " set: " + file);
+    	} else {
+    		System.err.println("Could not set replication for: " + file);
+    	}
+    }
+    
+    
     /**
      * Get a listing of all files in DFS at the indicated name
      */
@@ -224,7 +300,7 @@ public class DFSShell {
                     " [-ls <path>] [-lsr <path>] [-du <path>] [-mv <src> <dst>] [-cp <src> <dst>] [-rm <src>]" +
                     " [-put <localsrc> <dst>] [-copyFromLocal <localsrc> <dst>] [-moveFromLocal <localsrc> <dst>]" + 
                     " [-get <src> <localdst>] [-cat <src>] [-copyToLocal <src> <localdst>] [-moveToLocal <src> <localdst>]" +
-                    " [-mkdir <path>] [-report]");
+                    " [-mkdir <path>] [-report] [-setrep [-R] <rep> <path/file>]");
             return;
         }
 
@@ -245,6 +321,8 @@ public class DFSShell {
                 tc.cat(argv[i++]);
             } else if ("-moveToLocal".equals(cmd)) {
                 tc.moveToLocal(argv[i++], new Path(argv[i++]));
+            } else if ("-setrep".equals(cmd)) {
+            		tc.setReplication(argv, i);           
             } else if ("-ls".equals(cmd)) {
                 String arg = i < argv.length ? argv[i++] : "";
                 tc.ls(arg, false);
@@ -273,4 +351,5 @@ public class DFSShell {
             fs.close();
         }
     }
+
 }