You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by cu...@apache.org on 2006/05/31 00:41:39 UTC

svn commit: r410397 - in /lucene/hadoop/trunk: CHANGES.txt src/java/org/apache/hadoop/dfs/DFSShell.java

Author: cutting
Date: Tue May 30 15:41:39 2006
New Revision: 410397

URL: http://svn.apache.org/viewvc?rev=410397&view=rev
Log:
HADOOP-222.  Add -setrep option to the dfs commands that alters file replication levels.

Modified:
    lucene/hadoop/trunk/CHANGES.txt
    lucene/hadoop/trunk/src/java/org/apache/hadoop/dfs/DFSShell.java

Modified: lucene/hadoop/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/hadoop/trunk/CHANGES.txt?rev=410397&r1=410396&r2=410397&view=diff
==============================================================================
--- lucene/hadoop/trunk/CHANGES.txt (original)
+++ lucene/hadoop/trunk/CHANGES.txt Tue May 30 15:41:39 2006
@@ -73,10 +73,13 @@
     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

Modified: lucene/hadoop/trunk/src/java/org/apache/hadoop/dfs/DFSShell.java
URL: http://svn.apache.org/viewvc/lucene/hadoop/trunk/src/java/org/apache/hadoop/dfs/DFSShell.java?rev=410397&r1=410396&r2=410397&view=diff
==============================================================================
--- lucene/hadoop/trunk/src/java/org/apache/hadoop/dfs/DFSShell.java (original)
+++ lucene/hadoop/trunk/src/java/org/apache/hadoop/dfs/DFSShell.java Tue May 30 15:41:39 2006
@@ -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.*;
@@ -83,6 +82,83 @@
     }
 
     /**
+     * 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
      */
     public void ls(String src, boolean recursive) throws IOException {
@@ -224,7 +300,7 @@
                     " [-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 @@
                 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 @@
             fs.close();
         }
     }
+
 }