Package org.apache.hadoop.fs.shell
Class Command
java.lang.Object
org.apache.hadoop.conf.Configured
org.apache.hadoop.fs.shell.Command
- All Implemented Interfaces:
Configurable
- Direct Known Subclasses:
FsCommand
An abstract class for the execution of a file system command
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected String[]static final Stringfield name indicating the command's long descriptionstatic final Stringfield name indicating the default name of the commandstatic final Stringfield name indicating the command's usage switches and arguments formatallows stderr to be captured if necessaryprotected intprotected Stringprotected intallows stdout to be captured if necessaryprotected boolean -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCommand()ConstructorprotectedCommand(Configuration conf) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidDisplay an exception prefaced with the command name.voiddisplayError(String message) Display an error string prefaced with the command name.voiddisplayWarning(String message) Display an warning string prefaced with the command name.protected intThe exit code to be returned if any errors occur during execution.expandArgument(String arg) Expand the given argument into a list ofPathDataobjects.protected LinkedList<PathData>expandArguments(LinkedList<String> args) Expands a list of arguments intoPathDataobjects.protected CommandFactoryretrieves the command factory.abstract Stringprotected intgetDepth()The long usage suitable for help outputprotected intWhile using iterator method for listing for a path, whether to group items and process as array?getName()The name of the command.The replacement for a deprecated commandgetUsage()The short usage suitable for the synopsisfinal booleanIs the command deprecated?protected booleanisPathRecursable(PathData item) Determines whether aPathDataitem is recursable.protected booleanprotected booleanisSorted()Whether the directory listing for a path should be sorted.?protected voidpostProcessPath(PathData item) Hook for commands to implement an operation to be applied on each path for the command after being processed successfullyprotected voidprocessArgument(PathData item) Processes aPathDataitem, callingprocessPathArgument(PathData)orprocessNonexistentPath(PathData)on each item.protected voidprocessArguments(LinkedList<PathData> args) Processes the command's list of expanded arguments.protected voidProvides a hook for handling paths that don't exist.protected voidprocessOptions(LinkedList<String> args) Must be implemented by commands to process the command line flags and check the bounds of the remaining arguments.protected voidprocessPath(PathData item) Hook for commands to implement an operation to be applied on each path for the command.protected voidprocessPathArgument(PathData item) This is the last chance to modify an argument before going into the (possibly) recursiveprocessPaths(PathData, PathData...)->processPath(PathData)loop.protected voidprocessPaths(PathData parent, RemoteIterator<PathData> itemsIterator) Iterates over the given expanded paths and invokesprocessPath(PathData)on each element.protected voidprocessPaths(PathData parent, PathData... items) Iterates over the given expanded paths and invokesprocessPath(PathData)on each element.protected voidprocessRawArguments(LinkedList<String> args) Allows commands that don't use paths to handle the raw arguments.protected voidrecursePath(PathData item) Gets the directory listing for a path and invokesprocessPaths(PathData, PathData...)intInvokes the command handler.protected abstract voidExecute the command on the input pathprotected voidExecute the command on the input path data.intrunAll()For each source path, execute the commandvoidsetCommandFactory(CommandFactory factory) sets the command factory for later use.voidDefine the name of the command.protected voidsetRecursive(boolean flag) Methods inherited from class org.apache.hadoop.conf.Configured
getConf, setConf
-
Field Details
-
COMMAND_NAME_FIELD
field name indicating the default name of the command- See Also:
-
COMMAND_USAGE_FIELD
field name indicating the command's usage switches and arguments format- See Also:
-
COMMAND_DESCRIPTION_FIELD
field name indicating the command's long description- See Also:
-
args
-
name
-
exitCode
protected int exitCode -
numErrors
protected int numErrors -
recursive
protected boolean recursive -
exceptions
-
out
allows stdout to be captured if necessary -
err
allows stderr to be captured if necessary
-
-
Constructor Details
-
Command
protected Command()Constructor -
Command
Constructor.- Parameters:
conf- configuration.
-
-
Method Details
-
getCommandName
- Returns:
- the command's name excluding the leading character -
-
setRecursive
protected void setRecursive(boolean flag) -
isRecursive
protected boolean isRecursive() -
getDepth
protected int getDepth() -
run
Execute the command on the input path- Parameters:
path- the input path- Throws:
IOException- if any error occurs
-
run
Execute the command on the input path data. Commands can override to make use of the resolved filesystem.- Parameters:
pathData- The input path with resolved filesystem- Throws:
IOException- raised on errors performing I/O.
-
runAll
public int runAll()For each source path, execute the command- Returns:
- 0 if it runs successfully; -1 if it fails
-
setCommandFactory
sets the command factory for later use.- Parameters:
factory- factory.
-
getCommandFactory
retrieves the command factory.- Returns:
- command factory.
-
run
Invokes the command handler. The default behavior is to process options, expand arguments, and then process each argument.run |->
Most commands will chose to implement justprocessOptions(LinkedList)\->processRawArguments(LinkedList)|->expandArguments(LinkedList)| \->expandArgument(String)* \->processArguments(LinkedList)|->processArgument(PathData)* | |->processPathArgument(PathData)| \->processPaths(PathData, PathData...)| \->processPath(PathData)* \->processNonexistentPath(PathData)processOptions(LinkedList)andprocessPath(PathData)- Parameters:
argv- the list of command line arguments- Returns:
- the exit code for the command
- Throws:
IllegalArgumentException- if called with invalid arguments
-
exitCodeForError
protected int exitCodeForError()The exit code to be returned if any errors occur during execution. This method is needed to account for the inconsistency in the exit codes returned by various commands.- Returns:
- a non-zero exit code
-
processOptions
Must be implemented by commands to process the command line flags and check the bounds of the remaining arguments. If an IllegalArgumentException is thrown, the FsShell object will print the short usage of the command.- Parameters:
args- the command line arguments- Throws:
IOException- raised on errors performing I/O.
-
processRawArguments
Allows commands that don't use paths to handle the raw arguments. Default behavior is to expand the arguments viaexpandArguments(LinkedList)and pass the resulting list toprocessArguments(LinkedList)- Parameters:
args- the list of argument strings- Throws:
IOException- raised on errors performing I/O.
-
expandArguments
Expands a list of arguments intoPathDataobjects. The default behavior is to callexpandArgument(String)on each element which by default globs the argument. The loop catches IOExceptions, increments the error count, and displays the exception.- Parameters:
args- strings to expand intoPathDataobjects- Returns:
- list of all
PathDataobjects the arguments - Throws:
IOException- if anything goes wrong...
-
expandArgument
Expand the given argument into a list ofPathDataobjects. The default behavior is to expand globs. Commands may override to perform other expansions on an argument.- Parameters:
arg- string pattern to expand- Returns:
- list of
PathDataobjects - Throws:
IOException- if anything goes wrong...
-
processArguments
Processes the command's list of expanded arguments.processArgument(PathData)will be invoked with each item in the list. The loop catches IOExceptions, increments the error count, and displays the exception.- Parameters:
args- a list ofPathDatato process- Throws:
IOException- if anything goes wrong...
-
processArgument
Processes aPathDataitem, callingprocessPathArgument(PathData)orprocessNonexistentPath(PathData)on each item.- Parameters:
item-PathDataitem to process- Throws:
IOException- if anything goes wrong...
-
processPathArgument
This is the last chance to modify an argument before going into the (possibly) recursiveprocessPaths(PathData, PathData...)->processPath(PathData)loop. Ex. ls and du use this to expand out directories.- Parameters:
item- aPathDatarepresenting a path which exists- Throws:
IOException- if anything goes wrong...
-
processNonexistentPath
Provides a hook for handling paths that don't exist. By default it will throw an exception. Primarily overriden by commands that create paths such as mkdir or touch.- Parameters:
item- thePathDatathat doesn't exist- Throws:
FileNotFoundException- if arg is a path and it doesn't existIOException- if anything else goes wrong...
-
processPaths
Iterates over the given expanded paths and invokesprocessPath(PathData)on each element. If "recursive" is true, will do a post-visit DFS on directories.- Parameters:
parent- if called via a recurse, will be the parent dir, else nullitems- a list ofPathDataobjects to process- Throws:
IOException- if anything goes wrong...
-
processPaths
protected void processPaths(PathData parent, RemoteIterator<PathData> itemsIterator) throws IOException Iterates over the given expanded paths and invokesprocessPath(PathData)on each element. If "recursive" is true, will do a post-visit DFS on directories.- Parameters:
parent- if called via a recurse, will be the parent dir, else nullitemsIterator- a iterator ofPathDataobjects to process- Throws:
IOException- if anything goes wrong...
-
isSorted
protected boolean isSorted()Whether the directory listing for a path should be sorted.?- Returns:
- true/false.
-
getListingGroupSize
protected int getListingGroupSize()While using iterator method for listing for a path, whether to group items and process as array? If so what is the size of array?- Returns:
- size of the grouping array.
-
isPathRecursable
Determines whether aPathDataitem is recursable. Default implementation is to recurse directories but can be overridden to recurse through symbolic links.- Parameters:
item- aPathDataobject- Returns:
- true if the item is recursable, false otherwise
- Throws:
IOException- if anything goes wrong in the user-implementation
-
processPath
Hook for commands to implement an operation to be applied on each path for the command. Note implementation of this method is optional if earlier methods in the chain handle the operation.- Parameters:
item- aPathDataobject- Throws:
RuntimeException- if invoked but not implementedIOException- if anything else goes wrong in the user-implementation
-
postProcessPath
Hook for commands to implement an operation to be applied on each path for the command after being processed successfully- Parameters:
item- aPathDataobject- Throws:
IOException- if anything goes wrong...
-
recursePath
Gets the directory listing for a path and invokesprocessPaths(PathData, PathData...)- Parameters:
item-PathDatafor directory to recurse into- Throws:
IOException- if anything goes wrong...
-
displayError
Display an exception prefaced with the command name. Also increments the error count for the command which will result in a non-zero exit code.- Parameters:
e- exception to display
-
displayError
Display an error string prefaced with the command name. Also increments the error count for the command which will result in a non-zero exit code.- Parameters:
message- error message to display
-
displayWarning
Display an warning string prefaced with the command name.- Parameters:
message- warning message to display
-
getName
The name of the command. Will first try to use the assigned name else fallback to the command's preferred name- Returns:
- name of the command
-
setName
Define the name of the command.- Parameters:
name- as invoked
-
getUsage
The short usage suitable for the synopsis- Returns:
- "name options"
-
getDescription
The long usage suitable for help output- Returns:
- text of the usage
-
isDeprecated
public final boolean isDeprecated()Is the command deprecated?- Returns:
- boolean
-
getReplacementCommand
The replacement for a deprecated command- Returns:
- null if not deprecated, else alternative command
-